Home » » how to make building pattern program in c

how to make building pattern program in c

This program output will display building pattern. This question was asked to me in one of the interview. It was not the easy one. Logic is needed to do this. First of all, every should be aware of basic concepts of programming arrays, numbers and loops.

Loops and if-else is very important as programming perspective. Revise let us c before going for any interview.


Program - Create Building Pattern and Building floors is decided by array index. Suppose we have array a[5].

a[0]=1
a[1]=4
a[2]=5
a[3]=0
a[4]=2

Output must like that in * pattern.





I did this program with matrix Logic. Look at the program and read comment beside every statements.

#include<conio.h>
#include<stdio.h>


void printBuilding(int a[]);  /* function to build pattern*/


void main()
{
   int array[]={1,4,2,3,2};
   printBuilding(array);
   getch();

}


void printBuilding(int a[])
{
   int max=0,b[12][12];
  for(int t=0;t<5;t++)     /* Loop to find out maximum element in the Array */  {
     if(a[t]>max)
    {
      max=a[t];
  } 

}
    max++;   /* increment max element for creating (max+1) X (max+1) Matrix*/ 

for(int x=0;x<max;x++)
{
   int y=0;
  for(y=max;y>4-a[x];y--)  /* Insert 0 in the array element where * pattern exists*/  {
    b[x][y]=0;   
  }
 
  for(int h=0;h<=y;h++)  /* Insert 1 at the remaining positions of the array */  {
    b[x][h]=1;
  }
}

 for(int k=0;k<max;k++)
  /* Print Matrix Or Pattern*/ {
   for(int l=0;l<max;l++)
   {
     if(b[l][k]==0)
     {
       printf("*");
     }
   else
   printf(" ");
  
    printf(" "); 
   }
  printf("\n");

  }
}


I think you must understood what i wanna to explain U. It was really gud program. It is only one way you can do it in simpler manner. If you find it tough comments us.
Share this article :

0 comments:

Post a Comment

 
My Other Websites : Ad Networks | Games Copyright © 2014. Study Depth - All Rights Reserved