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.



Monday 16 April 2012

Recursion

Recursion is a concept in which a function calls itself till a certain condition is satisfied, the function call states are stored on a stack.(ie the local variables,registry values etc)
Once the required condition is satisfied, the control is returned back in order the functions were pushed into stack.

sample program:


#include<stdio.h>
void TrialRecursion(int count);
void main()
{
 int count=5;
 TrialRecursion(count);
}
//this function prints the welcome message 5times or equivalent to count
void TrialRecursion(int count)
{
 //required condition for final return
 if(count==0)
    return;
//else print welcome message and call itself with count-1 as parameter
 else
  {
   printf("Welcome msg no:%d\n",count);
//Recursive Call
   TrialRecursion(count-1);
   return;
   }
}

Output:

1
2
3
4
5
Welcome msg no:5
Welcome msg no:4
Welcome msg no:3
Welcome msg no:2
Welcome msg no:1


Control Flow and stack during Recursion:



Function  Control   Flow
count
Stack
void TrialRecursion(int count)
{
if(count==0)
   return;
else{
 printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}

5
Action:  push function state
TrialRecursion()count=5
void TrialRecursion(int count)
{
if(count==0)
   return;
else{
 printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}

4
TrialRecursion()count=4
TrialRecursion()count=5
Action:   push function state
void TrialRecursion(int count)
{
if(count==0)
   return;
else{
 printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}

3

TrialRecursion()count=3
TrialRecursion()count=4
TrialRecursion()count=5
Action:  push function state
void TrialRecursion(int count)
{
if(count==0)
   return;
else{
 printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}

2

TrialRecursion()count=2
TrialRecursion()count=3
TrialRecursion()count=4
TrialRecursion()count=5
Action:  push function state
void TrialRecursion(int count)
{
if(count==0)
   return;
else{
 printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}

1
TrialRecursion()count=1
TrialRecursion()count=2
TrialRecursion()count=3
TrialRecursion()count=4
TrialRecursion()count=5

Action:  push function state






void TrialRecursion(int count)
{
if(count==0)
   return;
else{
 printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}

0
TrialRecursion()count=1
TrialRecursion()count=2
TrialRecursion()count=3
TrialRecursion()count=4
TrialRecursion()count=5

Action:  pop all till control returns to main function

void main()
{
 int count=5;
 TrialRecursion(count);
}


 Control returns to main function.







Advantages:

  1. Can simplify a complex task by breaking into smaller repeatable tasks.
  2. Provides Alternative to complex looping.
  3. Simple code.

Disadvantages:

  1. More Memory is utilised to store the function state onto the stack.
  2. May lead to stack overflow in case of large functions with lots of variables.
  3. Debugging the program is difficult.
  4. Decreases the time efficiency of program.