Groovy hepcat sathyashrayan was jivin' on Thu, 13 Apr 2006 17:37:44
+0530 in comp.lang.c.
simple exercise:Printing E with '*' 's a cool scene! Dig it!
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("*");
10??? Tell me, what part of "a height of seven characters and a
width of five characters" are you having trouble comprehending?
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("*");
It's not ugly. It's not what the exercise said to do. But it's
pefectly clear code.
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.
What does that mean? Define "not working".
#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("*");
}
Now *this* is ugly! It isn't immediately obvious what it is meant to
do. It is poorly formatted. It's overly convoluted. It contains errors
in the most basic logic. (Eg., neither the second if(temp ==h) nor the
second if(temp == w) can possibly evaluate true, since they are within
the else clause of the first.)
}
}
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.
What's wrong with the most straight-forward approach? You haven't
tried that yet. Just output whatever characters are needed to make an
E 7 characters high and 5 characters wide. You can do that in a single
line, with a single output statement. That's all it takes.
OK, lets get your mind on track. Get all notions of doing "clever"
things with loops out of your head. Simplify, m'boy, simplify! How
would you do it by hand? Get a piece of paper and a pencil, and draw
an E in asterisks. Remember, it has to be 7 high and 5 wide. How would
you do it?
You'd no doubt start by making 5 asterisks at the top. Then you'd
begin a new line, and put one asterisk there, right? And then you'd
begin a new line, and put another asterisk there. Then you'd begin a
new line, and put 5 more asterisks down. (Although some people like to
shorten the middle prong. In that case, just put 3 or 4 asterisks
here.) Then begin another new line, and another asterisk. And the same
again. Then on the next new line, finish off with 5 more asterisks.
Well, it's no different doing it on a computer. You just need to
output the right characters, asterisks and newlines, in the correct
sequence. As I said, you don't need to get caught up in loops; you can
just output the sequence as a single unit.
--
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?