formated output excer (help)

S

sathyashrayan

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.
 
P

Pedro Graca

sathyashrayan said:
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.
 
C

CBFalconer

Pedro said:
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/>
 
P

Peter Shaggy Haywood

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!
puts("*****\n"
"*\n
^
Missing "
"*\n"
"*****\n"
"*\n
^
Missing "
^
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"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top