472,967 Members | 1,701 Online

# 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 1482
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.

--
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
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!

"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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 5 by: Norbert Thek | last post by: Hi I'm using Python 24 on Windows > (2k) Is there an easy way to convince optparse to accept newline in the helpstring? and more importand also in the 'desc' string. I tried everything (from... 3 by: Emil Dotchevski | last post by: I have a table that is at 100% width, which contains a