Introduction to Arrays in C

Introduction to Arrays in C Language

 
Arrays are the kind of data structure which can be defined as the collection of similar type of elements arranged in contiguous memory allocation.
 
  • Arrays are the derived data types in C programming Language.
  • Array can store the elements of all primitive data type such as int, char, float, double etc.
  • Arrays can also store the collection of derived data types such as pointers, structures etc.
  • While Storing the elements in contiguous fashion the lowest address corresponds to the first element of array.
Introduction of array in C

Why should we use array in Programming

Let us consider a scenario where you have to store the marks of 5 students, you can use 5 float variables as student1, student2, and so on. But, if you have to store the marks of 100 students this method would takes a lot of time.
The most efficient way to get this task done is to create an float array with name as student and size as 100.

arrays in c

Declaration of Arrays in C Programming Language

 Declaration of Array can be done by the following way in C Languge:

    Syntax : Data_type  array_name  [Array_size] ;

Example :
    integer Array : int student [50];
    char Array : char student [50]; (Character Array is also known as String in C Programming Language)
    float Array : float student [50];
    double Array : double student [50];

Initialization of Arrays in C Programming Language

 Array can be initialized in C programming language in the compile time or in the run time.

Initialization of Array in Compile Time

Initialization of Array at compile means declaring the elements of array before running the code at the time of declaration which can be done as follow:

#include <stdio.h>
int main()
{
    //integer array;
    int student[5] = {6070658085};
    //float array;
    float student[5] = {60.075.580.067.083.5};
    return 0;
}

Initialization of Array in Run Time

Initialization of Array at Run Time means declaring the elements of array after running the code. Here, elements are taken from user.
we can also set the the size of array when we initialize array at run time.

#include <stdio.h>
int main()
{
    int n;              //size of array
    scanf("%d",&n);
    int student[n];     //array
    for(int i=0; i<n; i++)
        scanf("%d",&student[i]);
    return 0;
}

Accessing the elements of Arrays in C Programming Language

The elements of array can be accessed by programmer with the help of index number of the element.
Note: 1st element is present at the 0th index, 2nd element is present at the 1st index, and so on.

Let us consider another scenario where teacher has to input marks of 5 students, and has to calculate the average of marks, but before calculating the average teacher notice that the marks of 3rd and 5th student needs to incremented by 7.

This can be solved in C programming Language as follows

#include <stdio.h>
int main()
{
    int n;                              //size of array
    scanf("%d",&n);
    int student[n];                     //decleration of array
    int average, sum=0;
    for(int i=0; i<n; i++)
    {
        scanf("%d",&student[i]);        //initialization of array
    }
    student[2] = student[2] + 7;        //accessing 3rd element
    student[4] = student[4] + 7;        //accessing 5th element
    
    for(int i=0; i<n; i++)
    {
        sum  = sum + student[i];        //accessing elements via loop
    }
    average = sum / n;                  //calculating average
    
    for(int i=0; i<n; i++)
    {
        printf("Marks of student %d : %d\n",i+1,student[i]);        
    }
    printf("Sum is : %d\n",sum);
    printf("Average is : %d",average);
    return 0;
}

Output

5
60 70 83 65 88
Marks of student 1 : 60
Marks of student 2 : 70
Marks of student 3 : 90
Marks of student 4 : 65
Marks of student 5 : 95
Sum is : 380
Average is : 76
accessing elements of array

Proving Array follows Contagious Memory Allocation

 The program given below proves that the Array follows contagious memory allocation

#include <stdio.h>
int main()
{
    int student[5] = {6070836588}; 
    printf("Size of integer in this compiler is %d\n",sizeof(int));
    for(int i=0 ;i<5 ;i++)
    {
        printf("Address student[%d] is %d\n"i, &student[i]);
    }                   
    return 0;
}

Output

Size of integer in this compiler is 4
Address student[0] is 6422280
Address student[1] is 6422284
Address student[2] is 6422288
Address student[3] is 6422292
Address student[4] is 6422296