An array is a fixed number of data items of the same type.
The data items in an array are referred to as elements.
The following code is an array declaration.
long numbers[10];
The number between square brackets sets how many elements the array contains.
Each of the data items from an array is accessed by the array name plus index.
The index values in array starts from 0, so the index value 0 refers to the first element.
You access the elements in the numbers array as numbers[0], numbers[1], numbers[2], and so on.
The following code stores grade value into array.
#include <stdio.h> int main(void) { int grades[10]; // Array storing 10 values unsigned int count = 10; // Number of values to be read long sum = 0L; // Sum of the numbers float average = 0.0f; // Average of the numbers printf("\nEnter the 10 grades:\n"); // Prompt for the input // Read the ten numbers to be averaged for (unsigned int i = 0; i < count; ++i) {//from w ww . jav a2 s .c o m printf("%2u> ", i + 1); scanf("%d", &grades[i]); // Read a grade sum += grades[i]; // Add it to sum } average = (float)sum / count; // Calculate the average printf("\nAverage of the ten grades entered is: %.2f\n", average); return 0; }
You used a loop to read the values and accumulate the sum:
for(unsigned int i = 0 ; i < count ; ++i) { printf("%2u> ",i + 1); scanf("%d", &grades[i]); // Read a grade sum += grades[i]; // Add it to sum }