429,327 Members | 2,627 Online
Need help? Post your question and get tips & solutions from a community of 429,327 IT Pros & Developers. It's quick & easy.

# simple exercise:Printing E with '*'

 P: n/a 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 #include 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 #include 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. Apr 13 '06 #1
6 Replies

 P: n/a sathyashrayan wrote: 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. [code snipped] 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: [code snipped] 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. I think that your best solution would to make an array of widths, so that: ***** * *** * ***** would become { 5, 1, 3, 1, 5 } Then iterate through that, each time printing the specified number of E's. Apr 13 '06 #2

 P: n/a Andrew Poelstra wrote: sathyashrayan wrote: 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. [code snipped] 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: [code snipped] 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. I think that your best solution would to make an array of widths, so that: ***** * *** * ***** would become { 5, 1, 3, 1, 5 } Then iterate through that, each time printing the specified number of E's. Alternatively, you can do: #include int main(void) { printf("*****\n"); printf("*\n"); printf("*\n"); printf("***\n"); printf("*\n"); printf("*\n"); printf("*****\n"); return 0; } Or even: #include int main(void) { printf("*****\n*\n*\n***\n*\n*\n*****\n"); return 0; } ;-) Apr 13 '06 #3

 P: n/a sathyashrayan said: 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. Can any one suggest some solution for this simple problem. I suggest you keep the solution as simple as the problem. Here's a solution that's as easy as A-B-C: #define a int #define b main #define c ( #define d void #define e ) #define f { #define g [ #define h 7 #define i ] #define j = #define k 1 #define l << #define m , #define n 5 #define o - #define p } #define q ; #define r while #define s '*' #define t >>= #define u '\n' #define v ++ #define w if #define x < #define y 0 #define z putchar #define become goto #define _ return a z c a e q a b c d e f a A g h i j f c k l n e o k m k m k m c k l n e o k m k m k m c k l n e o k p q a B j y q confused: r c A g B i e z c s e m A g B i t k q z c u e m B v q w c B x h e become confused q _ y q p -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at above domain (but drop the www, obviously) Apr 13 '06 #4

 P: n/a "sathyashrayan" wrote in message news:44***********************@news.sunsite.dk... 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 #include 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 #include 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. int Eshape[7] = {5, 1, 1, 3, 1, 1, 5}; /* Number of asterisks per line */ Then use two nested loops. This can be genrealized for arbitrary height/width, without using an array, similar to your second try. for ( i=0; i < height; i++ ) { /* set n to number of asterisks for line i */ .... / * add loop to print n asterisks */ .... } -- Fred L. Kleinschmidt Boeing Associate Technical Fellow Technical Architect, Software Reuse Project Apr 13 '06 #5

 P: n/a A possible solution which I did once, is to break down the 'E' into four line equations, then iterate through an imaginary x, y grid each representing a * or a space, this would allow you to specify the width and height of the 'E'. In fact it's the essential idea to draw any shape with variable width and height. However, the last thing that can be said about this solution (Compared to what others suggested), is simple :) Abdo Haji-Ali Programmer In|Framez Apr 13 '06 #6

 P: n/a 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'spractical C programming". I just wanted to do a couple of Cprogramming exercise. I do have K and R book, but let me trysome 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#include 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 andtoo ugly.So I made a second try which is not working. What does that mean? Define "not working". #include#includevoid 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 twolines of'*'. Can any one suggest some solution for this simpleproblem.Don'tsimply 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"? Apr 16 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.