FoodForFutureGeeks

Saturday 21 April 2012

Arrays, 2D arrays, 3D arrays.

Array:

An array is a collection of homogeneous variables, all the variables in the array should be of same datatype.
declaration of an array:an array is declared like a normal variable but it is followed by a pair of square braces.
#include<stdio.h>
void main()
{

//the array size is specified in square braces
int a[10];
//we can assign values during declaration itself using flower braces.
float f[5]={0,1,2,3,4};
}
Sample program:
//This program demonstrates 
//1.How to store values in an array
//2.How to access values stored in array
#include<stdio.h>
void main()
{

//Array declaration
int array[10];
int i=0;
//storing values in the array
printf("enter the values of 10 nos:\n");
for( i=0;i<10;i++ )
{
 scanf("%d",&array[i]);
}
//accessing the values stored in array
for( i=0;i<10;i++ )
{
printf("array location:%d has value:%d\n",i,array[i]);
}
}

Two dimensional arrays:

A two dimensional array can be considered as a matrix like storage structure, elements are stored in rows and columns.

general format:
variablename[m][n];
m is the number of rows
n is the number of columns.

ex:
int a[2][4]={1,2,3,4,5,6,7,8};

logical storage 
[ 1  2  3  4
  5  6  7  8]
Note:
The matrix like storage structure is only logical assumption, but the actual storage of variables in memory need not be so.

Sample program:
//This program demonstrates 
//1.How to store values in a 2Darray
//2.How to access values stored in a 2D array
#include<stdio.h>
void main()
{
 
//Array declaration
int array[2][2];
int i=0;
int j=0;
//storing values in the array
printf("enter the values of 4 nos:\n");
for( i=0;i<2;i++ )//row traversal
{
 for( j=0;j<2;j++)//column traversal
  {
   scanf("%d",&array[i][j]);
  }
}
//accessing the values stored in array
for( i=0;i<2;i++ )//row traversal
{
 for(j=0;j<2;j++)//column traversal
   {
    printf("array location:%d has value:%d\n",i,array[i][j]);
   }
}
}
enter the values of 4 nos:
1 2 3 4
array location:0 has value:1 array location:0 has value:2 array location:1 has value:3 array location:1 has value:4

Three Dimensional Arrays:

Three dimensional arrays are like multiple copies of the same matrix.
Standard format:
variablename[no of matrix][no of rows][no of columns]
ex: int a[2][3][3]; two matrices with 3 rows and 3 columns.

sample program:
//This program demonstrates 
//1.How to store values in a 3Darray
//2.How to access values stored in a 3D array
#include<stdio.h>
void main()
{
 
//Array declaration
int array[2][2][2];
int i=0;
int j=0;
int k=0;
//storing values in the array
printf("enter the values of 8 nos:\n");
for(k=0;k<2;k++)//traverse the matrix copy
{
for( i=0;i<2;i++ )//row traversal
{
 for( j=0;j<2;j++)//column traversal
  {
   scanf("%d",&array[k][i][j]);
  }
}
}
//accessing the values stored in array
for( k=0;k<2;k++)//traverse the matrix copy
{
printf("Matrix no:%d\n",k);
for( i=0;i<2;i++ )//row traversal
{
 for(j=0;j<2;j++)//column traversal
   {
    printf("array location:%d has value:%d\n",i,array[k][i][j]);
   }//end of j loop
 }//end of i loop
}//end of k loop
}

enter the values of 8 nos:
1 2 3 4 5 6 7 8
Matrix no:0 array location:0 has value:1 array location:0 has value:2 array location:1 has value:3 array location:1 has value:4 Matrix no:1 array location:0 has value:5 array location:0 has value:6 array location:1 has value:7 array location:1 has value:8

By using three dimensional array, the elements were stored in two 2*2 matrices in the above program.



No comments:

Post a Comment