Introduction of Array



Arrays

The simplest form of the array is a one-dimensional array that may be defined as a finite ordered set of homogeneous (similar) elements, which is stored in contiguous memory locations.

The general form for declaring a single-dimensional array is:

data_type array_name[expression];
 
int t a[4] = {34,60,93,2};
int b[] = {2,3,4,5};
float c[] = {-4,6,81,− 60};


One of the most common arrays is a string, which is simply an array of characters terminated by a null character. The value of the null character is zero. A string constant is a one-dimensional array of characters terminated by a null character(\0)

Consider the following string which is stored in an array:
 “sentence\n”

the way a character array is stored in memory. Each character in the array occupies one byte of memory and the last character is always ‘\0’. Note that ‘\0’ and ‘0’ are not the same. The elements of the character array are stored in contiguous memory locations.

    s     e     n     t     e     n     c     e     \n     \0

Note that, in this declaration ‘\0’ is not necessary. C inserts the null character automatically



The format of declaration of a multidimensional array in C is given below:
 data_type array_name [expr 1] [expr 2] …. [expr n];
 int a[2][2][2]


The schematic of a two-dimensional array of size 3 × 5 is following:

Row 0     a[0][0]     a[0][1]     a[0][2]     a[0][3]     a[0][4] 
Row 1     a[1][0]     a[1][1]     a[1][2]     a[1][3]     a[1][4] 
Row 3     a[2][0]     a[2][1]     a[2][2]     a[2][3]     a[2][4]


 Consider the following array:
 char p[10];
 p and &p[0] are identical because the address of the first element of an array is the same as the address of the array

    int *x, a [10];
    x = a;
    x[5] = 100;
   * (x+5) = 100

Both assignment statements place the value 100 in the sixth element of a

Sparse Matrices


Matrices with a good number of zero entries are called sparse matrices
 

3-tuple representation

Representation of a sparse matrix of order 7 × 6

0 1 2 3 4 5
0 0 0 0 5 0 0
1 0 5 0 0 0 0
2 0 0 0 0 9 0
3 0 3 0 2 0 0
4 1 0 2 0 0 0
5 0 0 0 0 0 0
6 0 0 8 0 0 0

3-tuple representation of the above matrix
7, 6, 8        (first row represent the rows, columns and non-zero element of the matrix)
0, 3, 5   (from 2nd represent the row, column, and value of  non-zero elements of the matrix)
1, 1, 4
2, 4, 9
3, 1, 3
3, 3, 2
4, 0, 1
4, 2, 2
6, 2, 8

The program accepts a matrix as input and prints the 3-tuple representation of it

#include <stdio.h>
void main() {
	int a[5][5], rows, columns, i, j, nz;
	printf("enter the order of the matrix. The order should be less than 5 × 5:\n");
	scanf("%d %d", &rows, &columns);
	printf("Enter the elements of the matrix:\n");
	for (i = 0; i < rows; i++)
			for (j = 0; j < columns; j++) {
		scanf("%d", &a[i][j]);
	}
	printf("The 3-tuple representation of the matrix is:\n");
	for (i = 0; i < rows; i++)
			for (j = 0; j < columns; j++) {
		if (a[i][j] != 0) {
			nz = nz + 1;
		}
	}
	printf("%d %d %d\n", rows, columns, nz);
	for (i = 0; i < rows; i++)
			for (j = 0; j < columns; j++) {
		if (a[i][j] != 0) {
			printf("%d %d %d\n", (i), (j), a[i][j]);
		}
	}
}

Output:-

enter the order of the matrix. The order should be less than 5 × 5:
3
3

Enter the elements of the matrix:

1
2
0
1
0
2
0
0
0

The 3-tuple representation of the matrix is:

3 3 4
0 0 1
0 1 2
1 0 1
1 2 2


APPLICATIONS

Arrays are simple, but reliable to use in more situations than you can count. 
Arrays are used in those problems when the number of items to be solved is fixed.
They are easy to traverse, search and sort. 
It is very easy to manipulate an array rather than other subsequent data structures. 
Arrays are used in those situations where in the size of array can be established before            hand. 
Also, they are used in situations where the insertions and deletions are minimal or not            present. 
Insertion and deletion operations will lead to wastage of memory or will increase the time                        complexity of the program due to the reshuffling of elements.

0 Comments