468,241 Members | 1,601 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,241 developers. It's quick & easy.

formated output excer (help)

I have got a book called "Practical C Programming", ISBN:1-56592-306-5
Publisher: O'Reilly.In that book I am doing some programming exercises.
Below is a one which I have got stuck.

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.

The solution which I have tried is

#include<stdio.h>
int main(void)
{
unsigned short i;
for(i=0;i<8;i++)
{
printf("*");
if((i == 1) && (i < 5))
{
printf("*\n");
}
if((i == 5) && (i < 9))
{
printf("*");
}
if(i < 7)
{
printf("*\n");
}
}
return 0;
}
not correct. Can any one give me the logic(*dont give out the solution

or the coding*). Thanks.
Feb 7 '06 #1
3 1389
sathyashrayan wrote:
I have got a book called "Practical C Programming", ISBN:1-56592-306-5
Publisher: O'Reilly.In that book I am doing some programming exercises.
Below is a one which I have got stuck.

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.
Do you mean

***** <== line 1 (or 0)
* <== line 2 (or 1)
* <== 3
***** <== 4
* <== 5
* <== 6
***** <== 7
The solution which I have tried is

#include<stdio.h>
int main(void)
{
unsigned short i;
for(i=0;i<8;i++)
/*
* this loop will execute 8 times:
* first with i == 0, then 1, then 2, ...
* and finally 7. Count them: 01234567
* */
{
printf("*");
/*
* start the loop by printing a '*'. Sounds good
* */
if((i == 1) && (i < 5))
/*
* if we're on the second row (remember you started i with 0?)
* and the second row is before the sixth ...
* */
{
printf("*\n");
/*
* ... print another '*' and a newline.
* This doesn't sound so good.
* */
}
if((i == 5) && (i < 9))
/* more if confusion */
{
printf("*");
}
if(i < 7)
{
printf("*\n");
}
}
return 0;
}
not correct. Can any one give me the logic(*dont give out the solution

or the coding*).


You're printing too many '\n'. You want one for each time through the
loop.
for (i = 0; i < 7; ++i)
{
printf("*");
/* deal with more '*'s for i==0, i==3 and i==6 */
/* WOW I just noticed something interesting (*) */
printf("\n");
}


(*) the long lines of the E can be recognized with one instruction.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Feb 7 '06 #2
Pedro Graca wrote:
sathyashrayan wrote:
I have got a book called "Practical C Programming", ISBN:1-56592-306-5
Publisher: O'Reilly.In that book I am doing some programming exercises.
Below is a one which I have got stuck.

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.


Do you mean

***** <== line 1 (or 0)
* <== line 2 (or 1)
* <== 3
***** <== 4
* <== 5
* <== 6
***** <== 7


puts("*****\n"
"*\n
"*\n"
"*****\n"
"*\n
"*\n
"*****");

seems to meet the requirements :-)

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Feb 7 '06 #3
Groovy hepcat CBFalconer was jivin' on Tue, 07 Feb 2006 00:45:38 -0500
in comp.lang.c.
Re: formated output excer (help)'s a cool scene! Dig it!
Pedro Graca wrote:
sathyashrayan wrote:
I have got a book called "Practical C Programming", ISBN:1-56592-306-5
Publisher: O'Reilly.In that book I am doing some programming exercises.
Below is a one which I have got stuck.

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.
Do you mean

***** <== line 1 (or 0)
* <== line 2 (or 1)
* <== 3
***** <== 4
* <== 5
* <== 6
***** <== 7


puts("*****\n"
"*\n

^
Missing "
"*\n"
"*****\n"
"*\n ^
Missing "
"*\n ^
Missing "
"*****");

seems to meet the requirements :-)


In a strange, mysterious other dimension in which quotes are not
required at the end of strings, maybe. :)

--

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"?
Feb 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Norbert Thek | last post: by
3 posts views Thread by Emil Dotchevski | last post: by
3 posts views Thread by sb Luis | last post: by
5 posts views Thread by Elhanan | last post: by
1 post views Thread by =?Utf-8?B?R3JlZyBH?= | last post: by
2 posts views Thread by Nikola Skoric | last post: by
1 post views Thread by vova777 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.