C Programming Cheat Sheet - 5
Tags: c
Published: 2021-04-10 05:29:48

Here is the C Programming Cheat - 5

###5. Arrays

Arrays are structures that hold multiple variables of the same data type. The first element in the array is numbered 0, so the last element is 1 less than the size of the array. An array is also known as a sub scripted variable. Before using an array its type and dimension must be declared.

####5.1 Array Declaration

Like other variables an array needs to be declared so that the compiler will know what kind of an array and how large an array we want.
```
int marks[30] ;
```
Here, `int` specifies the type of the variable, just as it does with ordinary variables and the word `marks` specifies the name of the variable. The `[30]` however is new. The number 30 tells how many elements of the type int will be in our array. This number is often called the "dimension" of the array. The bracket `( [ ] )` tells the compiler that we are dealing with an array.

Let us now see how to initialize an array while declaring it. Following are a few examples that demonstrate this.
```
int num[6] = { 2, 4, 12, 5, 45, 5 } ; 
int n[] = { 2, 4, 12, 5, 45, 5 } ; 
float press[] = { 12.3, 34.2 -23.4, -11.3 } ;
```

####5.2 Accessing Elements of an Array

Once an array is declared, let us see how individual elements in the array can be referred. This is done with subscript, the number in the brackets following the array name. This number specifies the element’s position in the array. All the array elements are numbered, starting with 0. Thus, marks [2] is not the second element of the array, but the third.
```
int valueOfThirdElement = marks[2];
```

####5.3 Entering Data into an Array

Here is the section of code that places data into an array:
```
for(i = 0;i <= 29;i++) 
{ 
    printf("\nEnter marks "); 
    scanf("%d", &marks[i]); 
}
```
The `for` loop causes the process of asking for and receiving a student’s marks from the user to be repeated 30 times. The first time through the loop, `i` has a value 0, so the `scanf()` function will cause the value typed to be stored in the array element `marks[0]`, the first element of the array. This process will be repeated until `i` becomes 29. This is last time through the loop, which is a good thing, because there is no array element like `marks[30]`.

In `scanf()` function, we have used the *"address of"* operator (&) on the element `marks[i`] of the array. In so doing, we are passing the address of this particular array element to the `scanf()` function, rather than its value; which is what `scanf()` requires.

####5.4 Reading Data from an Array

The balance of the program reads the data back out of the array and uses it to calculate the average. The for loop is much the same, but now the body of the loop causes each student’s marks to be added to a running total stored in a variable called sum. When all the marks have been added up, the result is divided by 30, the number of students, to get the average.
```
for ( i = 0 ; i <= 29 ; i++ )
    sum = sum + marks[i] ; 
avg = sum / 30 ; 
printf ( "\nAverage marks = %d", avg ) ;
```
#### 5.5 Example

Let us try to write a program to find average marks obtained by a
class of 30 students in a test.
```
#include<stdio.h>  
main() 
{ 
    int avg, i, sum=0; 
    int marks[30] ; /*array declaration */ 
    for ( i = 0 ; i <= 29 ; i++ ) 
    { 
        printf ( "\nEnter marks " ) ; 
        scanf ( "%d", &marks[i] ) ; /* store data in array */ 
    } 
    for ( i = 0 ; i <= 29 ; i++ ) 
        sum = sum + marks[i] ; /* read data from an array*/ 
    avg = sum / 30 ; 
    printf ( "\nAverage marks = %d", avg ) ; 
} 
```

Make sure to Follow me here, to get the update when i post a blog.