Sunday, 22 December 2013

Program to initialize the array as a pyramid of alphabets (Asked in APPLIED MATERIALS coding round)

#include<stdio.h>
void print_in(int,int,char);
void show(int,int);
void initialize(int,int);
char a[4][9];
int main()
{
char ch='A';
initialize(5,9);
print_in(0,4,ch);
show(5,9);
}
void print_in(int row,int col,char ch)
{
int count=1;
while(count<=25) // till Y
{
if(count==1)
{
a[row][col] = ch;
ch++;
}
if(count>1 && count<=4)
{
a[row][col] = ch;
col++;
ch++;
}
if(count>4 && count<=9)
{
a[row][col] = ch;
col++;
ch++;
}
if(count>9 && count<=16)
{
a[row][col] = ch;
col++;
ch++;
}
if(count>16 && count<=25)
{
a[row][col] = ch;
col++;
ch++;
}
if(count==1)
{
row++; // 1
col--;  // 3
}
if(count==4)
{
row++; // 2
col = col-4;// 2
}
if(count==9)
{
row++; // 3
col = col-6;// 1
}
if(count==16)
{
row++; // 4
col = col-8;// 0
}

count++;
}
}
void show(int row,int col)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
printf("%c ",a[i][j]);
}
printf("\n");
}
}
void initialize(int row,int col)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
a[i][j] = ' ';
}
}
}


Sample Output:
A
BCD
EFGHI
JKLMNOP
QRSTUVWXY

No comments:

Post a Comment