as

Wednesday 12 November 2014

Arrays in C

Declaring Arrays

Consider below statement.

int marks;

In above statement, marks is a integer variable which occupies just 2 bytes in the memory. We can store only single value at any point of time.

Arrays can be used to store multiple values. Arrays are used to store multiple values in the contiguous locations in memory.

Sample array representation in memory is given below.

int a[6];



In above figure, we have an array of size 6. Please note that Array index starts at 0.
So to get the first element in array we have to use below syntax.

X = a[0];

After execution of above statement, Variable X will contain 22.
The above mentioned array is 1-dimensional array.
We can also create multi dimensional arrays.
  1.  2-dimensional array
  2.  3-dimensional array and so on..

One dimensional Array


Below program demonstrates how to use 1-dimensional array in  C.

#include<stdio.h>             


void main()
{
  //Declare and initialize the array with 5 elements.
       
  int a[5]={2,4,3,6,10};      
  //Please note that we can not store more than 5 elements in above array.
       
  int i=0;

  for(i=0;i<=4;i++)      // for loop is used to iterate through the array from 0 to 4
   {
     printf("\n %d",a[i]);          
      // printing value of ith location of an array
   }

 getchar();
}


Please note that we can store only similar data type values in the array. In above example, we can store only integer values in array a. We can not store floating numbers like 12.4 in above array. To store such number, you will have to declare the array of type float.

Program to Accept 5 element from user & Display sum of elements.

#include<stdio.h>             

void main()
{
 
       
  int a[5];      
 
  int i=0,sum=0;

  for(i=0;i<=4;i++)     
   {
     printf("\n Enter %d position Array Element :",i);
     scanf("%d",&a[i]);
     sum=sum+a[i];
   }
  printf("\n Sum=%d",sum);

 getchar();
}


Program to accept integer values in two arrays & sum them into third array.


#include<stdio.h>             


void main()
{
 
       
  int a[5],b[5],c[5]; 
 
  int i=0,sum=0;
  printf("Enter First Array element \n");
  for(i=0;i<=4;i++)     
   {
     printf("\n Enter %d position Array Element :",i);
     scanf("%d",&a[i]);
       
   }
 printf("Enter Second Array element \n");
  for(i=0;i<=4;i++)     
   {
     printf("\n Enter %d position Array Element :",i);
     scanf("%d",&b[i]);
   }
  printf("Addition stored in third array \n");
  for(i=0;i<=4;i++)     
   {
      c[i]=a[i]+b[i];
      printf(" c[%d]=%d \n",i,c[i]);
   }
getchar();
}



Program to sort given Array in Ascending  order.

#include<stdio.h>             


void main()
{
  int a[5];      
  int i=0,j=0,temp=0;

  printf("Enter Array element \n");
  for(i=0;i<=4;i++)     
   {
     printf("\n Enter %d position Array Element :",i);
     scanf("%d",&a[i]);
   }
  for(j=0;j<=4;j++)
  {
    for(i=0;i<=4;i++)
       {
        if(a[i+1]<a[i])
        {
           temp=a[i];
           a[i]=a[i+1];
           a[i+1]=temp;
  
         } 
        }
   }
printf("Array in ascending order is:\n");
for(i=0;i<=4;i++)
{
      printf("%d\n",a[i]);

}

 getchar();
}



Multidimensional Array

Multi-dimensional arrays can be used in many scenarios like matrix operations.

Program to accept elements for 3 x 3 matrix & display the contents is given below.

#include<stdio.h>             


void main()
  int a[3][3];
  int r,c;

  printf("Enter Array element \n");

  for(r=0;r<=2;r++)     
   {
     for(c=0;c<=2;c++)
      {
        printf("\n enter a[%d][%d] :",r,c);
        scanf("%d",&a[r][c]);
      }
   }
  printf("Display Array element \n");

  for(r=0;r<=2;r++)     
   {
     for(c=0;c<=2;c++)
      {
                              printf("a[%d][%d]=%d\n" ,r,c,a[r][c]);

      }
   }
  getchar();
}

Program to add 2 matrices of 3 x 3 dimensions is given below.

#include<stdio.h>             


void main()
  int a[3][3];
  int b[3][3];
  int d[3][3];   
    int r,c;
  printf("Enter First Array element \n");
  for(r=0;r<=2;r++)     
   {
        for(c=0;c<=2;c++)
        {
        printf("\n enter a[%d][%d] :",r,c);
          scanf("%d",&a[r][c]);
        }
   }
 
 printf("Enter Second Array element \n");
  for(r=0;r<=2;r++)     
   {
        for(c=0;c<=2;c++)
        {
        printf("\n b[%d][%d] :",r,c);
          scanf("%d",&b[r][c]);
        }
   }
  printf("Addition stored in third array \n");
  for(r=0;r<=2;r++)     
   {
        for(c=0;c<3;c++)
        {
     d[r][c]=a[r][c]+b[r][c];
       printf(" d[%d][%d]=%d \n",r,c,d[r][c]);
        }
   }

 getchar();
}

No comments:

Post a Comment

Leave your valuable feedback. Your thoughts do matter to us.

Sponsored Links

Popular Posts

Comments

ShareThis