You are on page 1of 6

UNIT- 2.

3
Introduction
An array is a collection of similar data items that are stored under a common name. A value in an
array is identified by index or subscript enclosed in square brackets with array name.
The individual data items can be integers, floating point numbers, and characters and so on, but
they must be the same type and same storage class.
Arrays can be classified into
(i)

One-Dimensional arrays

(ii)

Two-Dimensional arrays

(iii) Multi-Dimensional arrays


Array Declaration: Arrays are declared in the same manner as an ordinary variable except that
each array name must have the size of the array i.e., number of elements accommodated in that
array.
Example: int a[5];
Where, 'a' is the name of the array with 5 subscripts of integer data types and the computer
reserves five storage locations.
Processing An Array: The entire array cannot be accessed with single operation. So, the array
elements must be accessed on an element-by-element basis. This can be usually done with the
help of the loop, when each pass of the loop is used to access an array element, thus the number
of passes through the loop will therefore equal the number of array elements to be processed.

One-Dimensional Array
The general form is data-type array-name[size];
Initialization of One-Dimensional Array:
The values can be initialized to an array, when they are declared like ordinary variables,
otherwise they hold garbage values.
The array can be initialized in the following two ways:
i)
At compile time
Example: int a[9]={1, 2, 3, 4, 5, 6, 7, 8, 9};
Char ch[]={V,A,H,I,N,I};
1
a [0 ]

a [1 ]

a [2 ]

a[3 ]

5
a [4 ]

a [5 ]

a [6 ]

a[7 ]

a [8 ]

ii)
At run time.
Char ch[10]; gets(ch);
1. If the number of values in initializer-is less than the size of an array, only those many
first locations of the are assigned the values. The remaining locations are assigned
zero.
1

Example: int a[9]={1, 2, 3, 4};


1
a [0 ]

a [1 ]

a [2 ]

a[3 ]

a [4 ]

a [5 ]

a [6 ]

a[7 ]

a [8 ]

2. If the number of values listed within initialize- list for an array is greater than the size
of the array, compiler raises error.
Example: int a[5]={1, 2, 3, 4, 5, 6, 7, 8, 9};
3. If the static array is declared without initialize- list then all locations are set to zero.
Example: static int a[9];
0
a [0 ]

a [1 ]

a [2 ]

0
a[3 ]

a [4 ]

a [5 ]

a [6 ]

0
a[7 ]

0
a [8 ]

4. If the size is omitted in a 1-d array declaration, which is initialized, the compiler will
supply this value by examining the number of values in the initialize-list.
Example: int a[]={1, 2, 3, 4, 5, 6, 7, 8, 9};
1
a [0 ]

a [1 ]

a [2 ]

a [3 ]

5
a [4 ]

a [5 ]

a[6 ]

a [7 ]

a [8 ]

5. Array elements cannot be initialized selectively. An attempt initialize only 2nd location
is illegal.
Example: int a[6]={, 6]; is illegal.
Example : /* Program to read 5 data and store them in an array and display their sum */
#include<stdio.h>
#include<conio.h>
main() {
int a[5],sum=0,i;
clrscr();
printf("Enter 5 integer numbers\n");
for(i=1;i<=5;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("The sum of given numbers is : %d\n",sum);
getch();
}

Two Dimensional Arrays


*. The array with two sub scripts termed as two dimensional array.
*. 2-d arrays are used in situation where a table of values needs to be stored in an array.
2

*. These can be defined in the same fashion as in one dimensional array, except a separate pair of
square brackets is required for each subscript.
*. Two pairs of square brackets required for two dimensional array and three pairs required for
three dimensional arrays and so on.
*. Two dimensional arrays are stored in a row-column matrix, where the left index indicates the
row and the right indicates the column.
The general form is data-type array-name[rowsize][colsize];
Example: int a[3],[3];
1

C o lu m n n u m b e rs
2

a [0 ][0 ]

a [0 ][1 ]

a [0 ][2 ]

a [1 ][0 ]

a [1 ][1 ]

a [1 ][2 ]

a [2 ][0 ]

a [2 ][1 ]

a [2 ][2 ]

- a[0][0] refers to data item in the first row and first column.
-a[0][2] refers to data item in the first row and third column.
-a[2][3] refers to data item in the second row and third column.
-a[3][3] refers to data item in the third row and third column.
Initializing a 2-d array.
data-type array-name[rowsize][colsize]={initialize-list};
1. Since colsize is 4, the first 4 values of the initialize- list are assigned to the first row of a
and the next 4 values are assigned the second row of a.
Example: int a[2] [4]= { 1, 2, 3, 4, 5, 6, 7, 8};
1

If the number of values in the initialize-list is less than the product of rowsize and colsize only
the first few matching locations of the array would get values from the initializer-list row wise.
The trailing unmatched locations would get zeros.
Example: int a[2] [4]= { 1, 2, 3, 4};
1

2. If the number of values specified in any initialize-list is less than colsize of a, only those
may first locations in the corresponding row would get these values. The remaining
locations in that row would get 0.
Example: int a[2][4]={ { 1, 2, 3}, { 4, 5, 6, 7}};
Since the first initialize-list has only three values, a[0][0] is set to 1, a[0][1] is set to 2,
3

a[0][2] is set to 3 and the fourth location in the first row is automatically set to 0.
1

3
6

3. If the number of values specified in any initialize-list is more than colsize of a, compiler
reports error.
int a [2][4]={{1, 2, 3, 4, 5}, { 6, 7, 8, 9}};
4. It is the responsibility of the programmer to ensure that the array bounds do not exceeds
the rowsize and colsize of the array. If they exceed irrespective results may be produced
and even the program can result in system crash sometimes.
Example: Processing of Two-dimensional array: To accept and display a matrix
#include<stdio.h>
main()
{
int a[3][3], I, j;
printf(Enter the elements of matrix a of order 3*3 \n);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(%d, &a[i][j]);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf( Matrix a is%3d, a[i][j]);
printf(\n);
}
}
Input-Output:
Enter the elements of matrix a of order 3 * 3
10 20 30 40 50 60 70 80 90 Matrix a is
10 20 30
40 50 60
70 80 90

Multi-Dimensional Arrays
Similarly, like one and two dimensional arrays. 'C' language allows multidimensional arrays. The
dimension with three or more sub scripts called multi dimensional arrays.
Declaration of three dimensional arrays:
The general form of declaration of a 3-d array is as follows:
data type array-name[size1][size2][size3];
4

Example: int a[2][3][4]; a is declared to be a 3-d array;


a[0][0][0] indicates data item in first table, first row, first column.
a[1][1][2] indicates data item in second table, second row, third column.
a[1][2][3] indicates data item in second table, third row, fourth column.
1. A three dimensional array is used to store logically related group of tables of values.
2. Three loops (preferably, for loops) are used to access each element in the tables.
3. First loop is to select each table; second loop is to select each row of the selected table.
Third loop is used to access each value in the row and table selected.
Processing three-dimensional arrays:
/* Program to accept the elements of a 3-d array and display them */
#include<stdio.h>
main()
{
int a[2][3][3], i, j k;
printf(Enter the elements of matrix a of order 2*3*3(two tables of sizes 3*3) \n);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
for(k=0;k<3;k++)
scanf(%d, &a[i][j][k]); }
printf( The 3-d array a \n);
for(i=0;i<2;i++) {
printf(a-table %d \n, i+1);
for(j=0;j<3;j++) {
for(k=0;k<3;k++)
printf(%3d, a[i][j][k]);
printf(\n); }
printf(\n);
}
getch();
}
/* Program to check a square matrix if it is symmetrical*/
#include<stdio.h>
main()
{
int a[10][10],i,j,size;
printf(Enter the size of the array);
scanf(%d, &size);
5

printf(Enter the elements of the matrix (row wise): \n);


for(i=0;i< size;i++)
for(j=0;j< size;j++)
scanf(%d, &a[i][j]);
printf(Given matrix is\n);
for(i=0;i< size ;i++) {
for(j=0;j< size ;j++)
printf(%d, a[i][j]);
printf(\n);
}
for(i=0;i<size-1;i++)
{
for(j=i+1;j< size;j++)
{
if((a[i][j]!=a[j][i])) {
printf(The Matrix is not symmetrical\n);
getch();
return;
}
}
}
printf(Matrix is symmetrical\n);
getch();
}
Input-Output Enter the size of the matrix 3
Enter the elements of the matrix (row wise):
1 2 3 2 4 5 3 5 6
Given Matrix is:
1 2 3
2 4 5
3 5 6
The matrix is symmetrical

You might also like