OLevel 2 B4 CLang 20apr SS
OLevel 2 B4 CLang 20apr SS
O Level / A Level
Chapter -5 : Arrays
Array
An array is a collection of data storage locations, each having the same data type and the
same name.
An array can be visualized as a row in a table, whose each successive block can be thought of
as memory bytes containing one element.
Each storage location in an array is called an array element.
When the above code is compiled and executed, it produces the following result:
Element[0] = 100.
Element[1] = 101.
Element[2] = 102.
Element[3] = 103.
Element[4] = 104.
Element[5] = 105.
Element[6] = 106.
Element[7] = 107.
Element[8] = 108.
Element[9] = 109.
Example : Calculate Average
// Program to find the average of n numbers using arrays
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
average = sum/n;
printf("Average = %d", average);
return 0;
}
Output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
Find the Largest/Smallest of the Elements of an Array
We set largest to the value of the first element of the array.
Then we compare this value to each of the other elements in the array.
If one is larger, we replace the value in largest with the value and continue to check the rest
of the array.