5. Initialing Array Variables
Data type array name [size];
Example:
int num[5] = { 1, 2, 3, 4, 5};
int id[0] = 13;
int id[1] = 14;
int id[2] = 15;
int id[3] = 16;
int id[4] = 17;
Num [0] references the first element in the array.
Num[4] references the last element in the array.
6. Simple Array Program
#include<stdio.h>
int main() {
int i,sum=0;
int marks[5];
for (i=0; i<5; i++)
{
printf (“Enter marks”);
scanf (“%d”,&marks[i]);
}
for (i=0; i<5; i++)
sum = sum+marks[i];
printf(“n summation marks=%d”,sum);
return 0;
}
7. Two dimensional Array
A two dimensional array has two indexes. The first index
refers to the row, and the second to the column.
8. Declaring two dimensional Array Variables
data_type array_name [row size] [column size]
Example:
int a[3][4];
char swe[15][8];
float num[2][3];
9. Initialing Two-Dimensional Arrays
Multidimensional arrays may be initialized by specifying bracketed
values for each row. Following is an array with 3 rows and each
row has 4 columns.
int a[3][4] = {
{0, 1, 2, 3} ,
{4, 5, 6, 7} ,
{8, 9, 10, 11}
};
The following initialization is equivalent to the previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
10. Two-Dimensional Array Program
#include <stdio.h>
int main () {
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %dn", i,j, a[i][j] );
}
}
return 0;
}