Array 2004 Notes
Array 2004 Notes
char a, b, c, d, e; // there are 5 variables of type character that can be grouped into an array.
and that character array is also known as String
What is array?
array is a fixed-size, sequenced collection of
elements of the same data type.
Elements refer to list of variables of the same data type that can be grouped into
an array
Ex.
int a, b, c, d, e, f, g, h, i, j; // there are 10 variables of type integer that can be grouped into an array.
float a, b, c, d, e; // there are 5 variables of type float that can be grouped into an array.
array is a sequence of data items that
are of the same type, that are indexible,
and that are stored contiguously.
Arrays are data type that is used to
represent a large number of homogenous
values.
array is a sequenced collection, we can
refer to the elements in the array as the
first element, the second element, and so
forth until we get to the last element.
Arrays are data type that is used to
represent a large number of homogenous
values.
use loops to read and write the elements
in an array.
int X[5]; a b c d e
college[0] = ‘C’;
college[1] = ‘C’;
college[2] = ‘M’;
college[3] = ‘I’;
college[4] = ‘T’;
college[5] = ‘\0’;
index or subscript
Referencing Array Elements
27
• int X[10] = { 3, 1,7,2,6,9,4,8,5,10}; declaration with initialization
9 will be displayed
• printf(”%d”, X[5]);
• scanf(”%d”, &X[9]); 27 will replace 10
int scores[10];
int i; …
for(i=0; i<10; i++)
scanf(“%d”, &scores[i]);
Inputting Values
sample run:
Output:
3
5
7
Multidimensional
Arrays
• Multidimensional arrays are defined in much the same manner as
one-dimensional
• A two-dimensional array requires two pairs of square brackets.
• Two-dimensional arrays are also called array of arrays.
float IT[5][3];
int CS[3][3];
int ccmit[3][3] ={1,2,3,4,5,6,7,8,9};
0 1 2
0 a [0] [0] a [0] [1] a [0] [2]
1 a [1] [0] a [1] [1] a [1] [2]
2 a [2] [0] a [2] [1] a [2] [2]
example: declarations
int x[3][4]={{2,4,6,8},{10,12,1,3},{5,7,9,11}};
int x[3][4]={{2,4,6,},{10,12,1,3},{5,7}};
error
int x[3][4]={{2,4,6,8},{10,12,1,3,14},{5,7,9,11}};
int x[3][4]={{2,4,6,8},{10,12,1,3},{8,1,3,4},{5,7,9,11}};
int x[3][4]={2,4,6,8,10,12,1,3,5,7,9,11};
int a;
int b;
for (a=0;a<3;a++)
for(b=0;b<4;b++)
printf(“%d\t”,x[a][b]);
referencing:
int x[3][4]={{2,4,6,8},{10,12,1,3},{5,7,9,11}};
printf(“%d”, x[1][2]); 0 1 2 3
scanf(“%f”, &x[0]0]); 0
1
2
10
4
12
6
1
8
3
x[2][0] = x[3][2] * x[1][3]; 2 5 7 9 11
if(x[1][4]>=7)
while( 9 == x[a][b])
1 2 3 4
1 123 3 321
12 12 32 32
123 1 321 3
5 6
* ***
** **
*** *