Arrays in c

 

Arrays in c
Arrays in c

ARRAY

An Array is a collection of elements of same data type,these elements are stored sequentially one after the other in the memory.

Array is a derived datatype ,any element can be accessed by using name of the array and position of element in the array and index of array starts with zero. 

Example : int age[4];  //In this example the array name is age and its size is 4.

Array representation for int age[4] :

             


Array elements are classified into 2 types :

 1.Single Dimensional Array [1D]     

  2.Multi Dimensional Array [2D]

1.Single Dimensional Array [1D]

 A Single Dimensional [1D]Array is a linear list which consists of related elements of same data type and in memory all the elements are stored in continuous memory-location one after the other.

Declaration of 1D Arrays :

  A 1D Array can be declared using the following 

Syntax:data_type array_name[array_size]

 here, data_type can be int,float or char.array_name is name of the array. array_size indicates the number of elements which can be stored in that array.

  Example : int age[4] 

 Here the data_type is  int,array_name is age and arary_size is 4

Initialisation of 1D Array is of 3 types :-

i)Intialising all the specified memory locations.

ii)Initialising the individual elements of the array.  

iii)Partial array initialisation.

i)Initialising all the specified memory locations:

Syntax:data_type array_name[array_size]={v1,v2};

Eample : int age[4]={1,2,3,4};



ii)Initialising the individual elements of the array:

Syntax: array_name[index]=value;

Example: int age[2];      

                   age[0]=1;     

                  age[1]=2;    

                   age[2]=3;

iii)Partial Array intialisation : This is allowed in C Language , if the number of values initialised is less than the size of the array the remaining locations will be automatically initialised to zero by the compiler.

Example : int age[4]={2,3};


Reading and Writing from an 1D Array

Below is an example on how you can read and write from an 1D array in c language

Example : int age[4];

Reading: for(int i=0;i<4;i++)     

                   scanf("%d",&age[i]);

Writing : for(int i=0;i<4;i++)      

                     printf("%d",age[i]);

Below is a program in c programming language to find the sum of N numbers using One Dimensional Array.


#include <stdio.h>
void main()
{
    int num[100],sum=0,n;
    printf("Enter the number of elements \n");
    scanf("%d",&n);
    printf("Enter the elements one by one\n");
    for(int i=0;i<n){
        scanf("%d",&num[i]);
    }
    for(int j=0;j<n;j++)
    {
        sum=sum+num[j];
    }
    printf("The sum of the elements is = %d",sum);

    return 0;
}

 Output:-
 Enter the number of elements 
 5
 Enter the elements one by one
 1 2 3 4 5
 The sum of the elements is = 15

2.Multi Dimensionl Array

Arrys with two or more dimensions are known as multi dimensional arrays

Example : int matrix[2][3];


A 2D Array is used when elements are arranged in a tabular pattern,to identify a particular element in this table we need to specify 2 indices 1st index identifies the row number of the element and 2nd index identifies the column number of the element,

Declaration of a 2D Array :

Syntax : data_type array_name[size 1][size 2];

here,data_type represents the data type of array to be declared ,  array_name represents the name of the arrray , size 1 represents the number of rows and size 2 represents the number of columns.

Example : int matrix[2][3];

the above example can be represented as 


Initialisation of Two Dimensional Arrays

Syntax:data_type array_name[size1][size2]{value 1,.......,value n};

Example : int matrix[2][3]={1,2,3,4,5,6};

Initialisation of above example is equivalent to initialising each array element as follows :

matrix[0][0] = 1;

matrix[0][1] = 2;

matrix[0][2] = 3;

matrix[1][1] = 4;

matrix[1][2] = 5;

matrix[1][3] = 6;

         


Partial Array Initialisation of 2D Array      

If the number of values to be initialised is less than the size of the array,then the remaining locations will be initialised to zero automatically by the compiler

Example : int matrix[2][3] ={1,2,3,4};


Reading and Writing 2D Arrays

Example : int matrix[2][3]

Reading : for(int i=0;i<2;i++)

                    for(int j=0;j<3;j++)

                        scanf("%d",&matrix[i][j]);

Writing : for(int i=0;i<2;i++)

                    for(int j=0;j<3;j++)

                        printf("%d",matrix[i][j]);

C program to display the elements of a matrix


#include <stdio.h>

int main()
{
    int i, j, m, n;
    int matrix[10][20];

    printf("Enter number of rows : ");
    scanf("%d", &m);
    printf("Enter number of columns : ");
    scanf("%d", &n);

    /* Input data in matrix */
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("Enter data in [%d][%d]: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }

    /* Display the matrix */
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}
 Output :
 Enter number of rows : 2
 Enter number of columns : 3
 Enter data in [0][0]: 1
 Enter data in [0][1]: 2
 Enter data in [0][2]: 3
 Enter data in [1][0]: 4
 Enter data in [1][1]: 5
 Enter data in [1][2]: 6
 1       2       3
 4       5       6


Advantages of Arrays


1. It is used to represent multiple data items of same type by using only single name and N number of elements can be created in an easy way. 
2. It can be used to implement other data structures like linked lists, stacks, queues, trees, graphs etc.
3. 2D arrays are used to represent matrices which help in matrix multiplication, addition and subtraction. 


Disadvantages of Arrays


1. We must know in advance that how many elements are to be stored in array before declaring the array. 
2.An  Array is static structure which means that the size of the array fixed ,that is the memory which is allocated to the  array can not be increased or reduced.
3. Since array is of fixed size, if we allocate more memory than required memory space the space will be wasted and if we allocate less memory than requirement, then it will create problem.
4. The elements of array are stored in consecutive memory locations, so insertion and deletion is very difficult and time consuming.

Post a Comment

0 Comments