Array
Array
By using array,
int aiNum[5];
PGT 106 : C PROGRAMMING 4
What is an Array? (Example)
aiNum 5 components or
elements
5 aiNum[0]
10 aiNum[1] Elements are referred to
index.
15 aiNum[2]
20 aiNum[3] Element aiNum[2] has
index 2 and value 15.
25 aiNum[4]
printf (“Enter first value: “); printf (“Enter fifth value: “);
scanf (“%d”, &iValue1); scanf(“%d”, &iValue5)
UniMAP Sem II -
09/10 PGT 106 : C PROGRAMMING 10
Arrays - Memory Allocation
Arrays are allocated int aiValue[8]; index aiValue
aiValue[0]=23; 0 23
bulk memory
aiValue[1]=56; 1 56
Single reference used aiValue[2]=100; 2 100
for multiple locations aiValue[3]=0; 3 0
int aiN[ 5 ] = { 0 } ;
▪ If size is omitted, initializers determine the size
int aiN[ ] = { 1, 2, 3, 4, 5 };
5 initializers, therefore 5 element array
for (iLoop=0;iLoop<10;iLoop++)
{ Using a loop to fill all the
adY[iLoop]= iLoop*100.0; elements of the adY[] array.
printf(“adY[%1d]=%.2lf\n", iLoop, adY[i]);
}
return 0;
}
adY[0]=0.00
adY[1]=100.00
adY[2]=200.00
adY[3]=300.00
adY[4]=400.00
adY[5]=500.00
adY[6]=600.00
adY[7]=700.00
adY[8]=800.00
adY[9]=900.00
void main()
{
int iLoop, iTotal = 0; // variable declaration
int aiY[n]={9,6,20,5,12}; // array declaration and
// initialization
for (iLoop=0;iLoop<n;iLoop++)
iTotal = iTotal + aiY[i];
printf ("\nTotal = %d\n”, iTotal);
}
Printing an array
for (iIndex = 0; iIndex < 10; iIndex++)
printf (“%d ”, aiSale[iIndex]);
PGT 106 : C PROGRAMMING 21
Parallel Arrays
Two (or more) arrays are called
parallel if their corresponding
components hold related information.
int aiStudentId[50];
char acStudentGrade[50];
Q & A!