S
sathyashrayan
Dear group,
Following is a exercise from a book called "Oreilly's
practical C programming". I just wanted to do a couple of C
programming exercise. I do have K and R book, but let me try
some simple one.
Exercise 4-2:
Write a program to print a block E using asterisks
(*), where the E has a height of seven characters
and a width of five characters.
Solution 1:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i;
for(i=0;i<10;i++)
printf("*");
for(i=0;i<5;i++)
printf("*\n");
for(i=0;i<10;i++)
printf("*");
for(i=0;i<5;i++)
printf("*\n");
for(i=0;i<10;i++)
printf("*");
return 0;
}
Though the above print the E with '*', it uses 5 loops and
too ugly.
So I made a second try which is not working.
solution-2:
#include<stdio.h>
#include<stdlib.h>
void foo(int w,int h)
{
int temp;
for(temp = 0 ; temp < (w + h) ; temp++)
{
printf("*");
if( temp == h)
printf("*\n");
else if( temp == w)
printf("*");
else
{
printf("*");
if( temp == h)
printf("*\n");
else if( temp == w)
printf("*");
}
}
}
int main(void)
{
foo(5,7);
return 0;
}
The Idea for the above is to iterate the desired number of
'*'
in one go and branching where ever needed. It prints two
lines of
'*'. Can any one suggest some solution for this simple
problem.Don't
simply write the code instead give me some hint.
Thanks.
Following is a exercise from a book called "Oreilly's
practical C programming". I just wanted to do a couple of C
programming exercise. I do have K and R book, but let me try
some simple one.
Exercise 4-2:
Write a program to print a block E using asterisks
(*), where the E has a height of seven characters
and a width of five characters.
Solution 1:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i;
for(i=0;i<10;i++)
printf("*");
for(i=0;i<5;i++)
printf("*\n");
for(i=0;i<10;i++)
printf("*");
for(i=0;i<5;i++)
printf("*\n");
for(i=0;i<10;i++)
printf("*");
return 0;
}
Though the above print the E with '*', it uses 5 loops and
too ugly.
So I made a second try which is not working.
solution-2:
#include<stdio.h>
#include<stdlib.h>
void foo(int w,int h)
{
int temp;
for(temp = 0 ; temp < (w + h) ; temp++)
{
printf("*");
if( temp == h)
printf("*\n");
else if( temp == w)
printf("*");
else
{
printf("*");
if( temp == h)
printf("*\n");
else if( temp == w)
printf("*");
}
}
}
int main(void)
{
foo(5,7);
return 0;
}
The Idea for the above is to iterate the desired number of
'*'
in one go and branching where ever needed. It prints two
lines of
'*'. Can any one suggest some solution for this simple
problem.Don't
simply write the code instead give me some hint.
Thanks.