Array in c programming

By | May 14, 2019

Array:

An array is a set of similar data elements which are stored in consecutive memory locations under a common variable name.” Some programs may need to handle the same type of a different number of data having the same characteristics. In such a situation, it will be easier to handle such data in Array, where the same name is shared for all data with different subscripts. In an array, it is necessary that all the data items must be of the same type and same storage class. Eg. either int, floating-point or characters

Each array element is denoted with a name followed by one or more subscripts (where each subscript must be non-negative integers within a pair of square bracket).

The number of subscripts depends on the dimensionality of the array. Eg. a[i] refers to an element of a one-dimensional array. Whereas a[i][j] refers to the two-dimensional array. In the same manner B[i][j][k] refers three dimensional array.

note: to be an array, two condition must be noted:

  1. All elements must be of a similar data type.
  2. All elements must be stored in a consecutive memory location.

Array declaration:

Declaration of an array is done in the same manner as ordinary variables, but it should also include the size of the specification that shows the maximum size of elements in that array. The size of an array must be defined with a constant ie. we can not use any variable as subscript while defining an array. But we can use a symbolic constant while defining the size of an array.

It is defined such as follows:

Storage_class data_type array[expression];

e.g.      int x [5];

float y[3][4];           

char name [MAX];      where MAX is symbolic constant

Index of an array:

The index of an array always starts with 0 and ends with n-1 where as n is the size of an array. for example: if the size of an array is 5 then its index will be 0,1,2,3,4. for example an array x[5] will have the blocks named with the index 0,1,2,3 and 4 as x[0], x[1], x[2], x[3], and x[4].

initialization of an array:

we can initialize an array in the following ways:

i. assign the elements in individual blocks as

int x[5]

  • x[0]=1; //put 1 to the first block x[0]
  • x[1]=3; //put 3 to the second block x[1]
  • x[2]=7; //put 7 to the third block x[2]
  • x[3]=5; //put 5 to the fourth block x[3]
  • x[4]=5; //put 5 to the fifth block x[4]

ii. we can also initialize an array as:

int x[5]= {1,3,7,5,5}

this statement declares an integer array of 5 blocks named x[0],x[1],x[2],x[3] and x[4] and put 1,3,7,5,5 in those block respectively.

Size of an array:

The size of an array is sum of the size of individual elements. the size is measured in byte. for example the size of an integer array of 5 elements is 10 bytes, the size of a float array of size 5 is 20 bytes, a character array of 5 elements is 5 bytes. the sizeof operator can be used to returned the size of an array in bytes. consider following example,

int x[5]={1,3,4,5,4};

printf(“the size of array is %d bytes”, sizeof(x))

output: the size of array is 10 bytes.

programming example:

Prog 1: To store integer nos in an array and print them (1 dim)

#include<stdio.h>

void main()

{

            int i, x[10]={4,5,2,8,4,7,9,6,5};

            printf(“\n The stored numbers in the array are:”);

            for(i=0;i<10;i++)

            {

                        printf(“\n %d”, x [ i ] );

            }

}

Prog 2: To sort a list of integer numbers in ascending order.

#include<stdio.h>

void main()

{

            int i,k, temp, x[10];

            printf(“\n Enter numbers to be sorted :”);

            for(i=0; i<10; i++)

            {

                        scanf(“%d”, &x [ i ] );

            }

            for(i=0;i<9;i++)

            {

                        for(k=i+1;k<10;k++)

                        {

                                    if(x [ i ] > x [ k ]  )

                                    {

                                                temp = x [ i ];

                                                x [ i ]= x [ k ];

                                                x [ k ]= temp;

                                    }

                        }

            }

            printf(“\n The sorted numbers are:”);

            for(i=0;i<10;i++)

            {

                        printf(“\n %d”, x [ i ] );

            }

}

prog3:To store numbers in a two dimensional array and print them

#include<stdio.h>

#include<conio.h>

void main()

{

            int i,k,x[2][3]={1,2,3,4,5,6};

            for(i=0;i<4;i++)

            {

                        for(k=0;k<4;k++)

                        {

                                    printf(“%d\t”,x[i][k]);

                        }

                        printf(“\n”);

            }

}

Prog 4:Write a program to find out sum of two matrices of size 2×3

#include<stdio.h>

#include<conio.h>

void main()

{

            int i,k,x[2][3],y[2][3],z[2][3];

            for(i=0;i<4;i++)

            {

                        for(k=0;k<4;k++)

                        {

                                    scanf(“%d”,&x[i]);

                        }

            }

            for(i=0;i<4;i++)

            {

                        for(k=0;k<4;k++)

                        {

                                    scanf(“%d”,&y[i]);

                        }

            }

            for(i=0;i<4;i++)

            {

                        for(k=0;k<4;k++)

                        {

                                    z[i][k]=x[i][k] + y[i][k];

                                    printf(“%d\t”,z[i][k]);

                        }

                        printf(“\n”);

            }

}

Leave a Reply

Your email address will not be published. Required fields are marked *