6. Arrays

Introduction to Array
An array in C  is a collection of similar type items stored at contiguous memory locations and elements can be accessed randomly using index of an array. They are used to store similar type of elements as in the data type must be the same for all elements. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type.


Advantages of an Array in C:

  1. Random access of elements using array index.
  2. Use of less line of code as it creates a single array of multiple elements.
  3. Easy access to all the elements.
  4. Traversal through the array becomes easy using a single loop.
  5. Sorting becomes easy as it can be accomplished by writing less line of code.

Disadvantages of an Array in C:

  1. Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic.
  2. Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation.

Types of Array:-

1.Single Dimensional Array :

  1. Single or One Dimensional array is used to represent and store data in a linear form.
  2. Array having only one subscript variable is called One-Dimensional array
  3. It is also called as Single Dimensional Array or Linear Array
Syntax:-
        <data-type> <array_name> [size];

Example:-
#include<stdio.h>
#include<conio.h>
int main()
{
   int i;
   int value[6] = {5,10,15,20,25,30}; // declaring and Initialising array in C
    for (i=0;i<6;i++)
   {
      // Accessing each variable using for loop
      printf("Position : [%d] , Value : %d \n", i, value[i]);
   }
    getch();  // Wait For Output Screen
    return 0; //Main Function return Statement
   }

Sample Output:
                        Position : [0] , Value : 5 
                        Position : [1] , Value : 10 
                        Position : [2] , Value : 15 
                        Position : [3] , Value : 20 
                        Position : [4] , Value : 25 
                        Position : [5] , Value : 30 

2. Multi Dimensional Array :

  1. Array having more than one subscript variable is called Multi-Dimensional Array.
  2. Multi Dimensional Array is also called as Matrix.
Syntax:-
       <data-type> <array_name> [row_subscript][column-subscript];

Example:-
#include <stdio.h>
 int main ()
 {
      int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; // an array with 5 rows and 2 columns
      int i, j;
      for ( i = 0; i < 5; i++ ) // output each array element's value 
      {
          for ( j = 0; j < 2; j++ ) {
          printf("a[%d][%d] = %d\n", i,j, a[i][j] );
      }
   }
   return 0;
}

Example:-
                    a[0][0]: 0
                    a[0][1]: 0
                    a[1][0]: 1
                    a[1][1]: 2
                    a[2][0]: 2
                    a[2][1]: 4
                    a[3][0]: 3
                    a[3][1]: 6
                    a[4][0]: 4
                    a[4][1]: 8





Post a Comment

0 Comments