C Arrays with Examples
C Arrays are convenient way of grouping lot of variables under single variable name. Arrays they can be one dimensional, two dimensional or more dimensional! An array is defined using square brackets []. For example: an array of three integers called “triplet” would be declared like this:
int triplet[3];
The number in the square brackets of the declaration is referred to as the ‘index’ (plural: indicies) or ‘subscript’ of the array and it must be an integer number between 0 and (in this case) 2. The three integers are called elements of the array and they are referred to in a program by writing:
triplet[0] triplet[1] triplet[2]
An C arrays are fixed number of data items that are all of the same type. The data items in an array are referred to as elements. The elements in an array are all of type int, or of type long, or all of any type you choose. The following array declaration is similar to a declaration for a normal variable that contains a single value, except that you’ve placed number between square brackets [] following the name:
long numbers[10];
The number between square brackets defines how many elements the array contains and is called the array dimension. An array has a type, which is a combination of the element type and the number of elements in the array. Thus two arrays are of the same type if they have the same number of elements of the same type.
Every one want to know Why use C arrays?
C Arrays are most useful when they have a large number of elements: that is, in cases where it would be completely impractical to have a different name for every storage space in the memory. It is then highly beneficial to move over to arrays for storing information for two reasons:
- The storage spaces in arrays have indicies. These numbers can often berelated to variables in a problem and so there is a logical connection to be made between an array program.
- In C arrays can be initialized very easily indeed. It is far easier to initialize an array than it is to initialize twenty or so variables. The first of these reasons is probably the most important one
C Arrays with Examples
Here can see the example, how to do and design Averaging ten grades – storing the values the easy way.
#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) { 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 start off the program with the ubiquitous #include directive for stdio.h because you want to use printf() and scanf(). At the beginning of main(), you declare an array of ten integers and then the other variables that you’ll need for calculation:
The above Example output looks
Enter the ten grades: 1> 450 2> 765 3> 562 4> 700 5> 598 6> 635 7> 501 8> 720 9> 689 10> 527 Average of the ten grades entered is: 614.70
You’ve calculated the average by dividing sum by count, the number of grades. Notice how you convert sum (which is type long) to type float in the call to printf().