Array

Array: An array is a data strucutre which contain a set of same type variables and each variable has distinct index number and index number start from 0 to N, here N is how many number you want to store. Maybe you thinking why array is need? Let's discover why array is needed in our program.

Assume that your teacher order you to do such a program that can store and print 2 roll number of your class mate.What you are thinking to do ? I think you are thinking to declare two variable store and print then right? Now just think instead of 2 you need to store 1000 roll number. For that you need to declare 1000 of variables . I think that very difficult to manage 1000 of variable . To get of that kind of situation we need array. In C programming there are two types of array and these are --

  1. 1- Dimentional Array
  2. 2-D or Multi Dimentional Array


1- Dimentional Array Declaration:
    Data_Type array_name[n];//here n is how many number you want to store.
Let's see a sample and assume you need to store 5 integer numbers so you need an int type array.
      #include<stdio.h>
      int main(){
        int num[5]; //you can choose name as your own
     }

When we declare an array int num[5]; where int is the type of this array that means in this array we can store int number only. num is name of this array you choose as your own/ [5] means there are 5 index it starts from 0 to 4.Each index act like an individual variable you can think there are 5 variables these are num[0], num[1], num[2], num[3], num[4] .After declare the array it looks like ---

array example

Generally there are two common way to insert the value in an array-

  1. num[5]= { 10, 20, 30, 40, 50 } in this way 10 will assign in index 0 or num[0] in the same way 20, 30, 40, 50 will assign following index.

  2.     #include<stdio.h>
        int main(){
          int num[5]= { 10, 20, 30, 40, 50 };
        return 0;
       }
  3. In the other hand you can assign value the following this way.

  4. #include<stdio.h>
      int main(){
         int num[5];
         num[0] = 10;
         num[1] = 20;
         num[2] = 30;
         num[3] = 40;
         num[4] = 50;
         return 0;
      }
Except the above two way you can take user input using loops.See the example:
  #include<stdio.h>
 
  int main()
  {
   int i, num[5];
   for( i=0; i<=4; i++ ){
     scanf("%d",#[i]);
   }
    return 0;
  }
After assign the value it looks like this
array example
Let's do a full program you need to take 5 number from user store it in an array and print this array using loop.
  #include<stdio.h>
 
  int main()
  {
   int i,j num[5];
   for( i=0; i<=4; i++ ){
     scanf("%d",#[i]);
   }
   for( j=0; j<=4; j++ ){
    printf("%d ",num[i]);
  }

    return 0;
  }

Input:
    10
    20
    30
    40
    50
  
Output:
    10 20 30 40 50 
2-D Array Declaration:
    Data_Type array_name[n][n];//here n is how many number you want to store.
Let's see a sample .
      #include<stdio.h>
      int main(){
        int num[5][5]; //you can choose name as your own
     }

When we declare an array int num[5][5]; where int is the type of this array that means in this array we can store int number only. num is name of this array you choose as your own. And[5][5] means there are 5 rows and each of rows and each rows have 5 index/columns,As you know the array index starts form 0 so, it starts from 0 to 4 columns and rows.Each index act like an individual variable you can think there are 25 variables these are num[0][0], num[0][1], num[0][2], num[0][3], num[0][4] following that up to num[4][4] and one thing you must remember tha first 3rd braket [] is for rows and 2nd is for columns fter declare the array it looks like ---

array example
Lets see the implementation:
#include<stdio.h>
int main(){
     int num[5][5];
     //input value
     for(int i=0; i<=4;i++){
      for(int j=0; j<=4;i++){
       scanf("%d",#[i][j]);
      }
    }
    //Output value
    for(int i=0; i<=4;i++){
      for(int i=0; i<=4;i++){
       printf("%d ", num[i][j]);
      }
      printf("\n");
    }
}
Input:
10 11 12 13 14 
20 21 22 23 24
30 31 32 33 34
40 41 42 43 44
50 51 52 53 54
 
Output:
10 11 12 13 14 
20 21 22 23 24
30 31 32 33 34
40 41 42 43 44
50 51 52 53 54