Handbook of Computer Science(cs) and IT

Array

It is a collection of similar elements (having same data type). For an array

of 100 elements, the first element’s index is zero ‘0’ and the last index will

be 99. This indexed access makes it very convenient to loop through each

element of array. Array elements occupy contiguous memory locations.

 

A [0] = a

A [1] = b

A [7] = h

Multi Dimensional Array

In C language, one can have arrays of any dimensions. Let us consider a

3 x 3 matrix.

3 x 3 matrix for multi dimensional array

To access the particular element from the array, we have to use two subscripts; one for row number and other for column number. The notation is of the form a [i] [1], where i starlets for row subscripts and j stands for column subscripts.

We can also define and initialize the array as follows i nt values [3] [4] = (

11, 2, 3, 4,1

( 5 , 6, 7, 81

(9, 10, 11, 121

);

OR

int values [3] [4]

= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

A simple program using array

# include  <stdio.h> void main ( )

{

a [ i ]

Subscripted           Subscript

Variable

int ave, sum= 0;

int i;

int marks [10];                                   // array declaration

for (i =0; i <= 9; i + +)

{

printf (“\n Enter marks”);

scanf (“%d”, &marks [1]); // store data in array

}

for (i = 0; i <= 9; i + +)

sum= sum+ marks [I]; avg= sum/30;

printf (“1n avg-marks= %d”, avg);

}

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

Leave a Reply

Your email address will not be published. Required fields are marked *