473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1491
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 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 <TD> that is 80% of that. In the <TD> I want to put pre-formatted text (the output from a C++ compiler, to be more precise) and I'd like the...
3
by: sb Luis | last post by:
I Use the following code to read from MS Word, but I couldent read Formated Text (RTF). How Can I read formated text from MS Word Doc without using clipboard. thanks. Word.ApplicationClass...
5
by: Elhanan | last post by:
hi.. i don't exactly how to calls this , but is there a property for xalan, to output xml with crarriage return? i'm trying out xslt and the output can't be read with a normal editor, i know...
0
southoz
by: southoz | last post by:
Good ay all , I'm fairly new to access(a little over 5 weeks now). Since I'v started I have picked up a lot of useful information from forums such as this and in doing so will share that information...
1
by: sambavikedar | last post by:
I have a query like this :- spool c:\daily.xls select message_id||chr(9)||account_name from table where <> message_id number account_name ...
1
by: =?Utf-8?B?R3JlZyBH?= | last post by:
A aspx page Label.Text contains formated currency ("0:C"). To process this data with the submit button click it needs to be converted to a decimal. For example: string strCur = "$111.11"; //...
2
by: Nikola Skoric | last post by:
I have been trying to find appropriate way to do get local time in "yyyy-mm-dd hh:mm:ss" format, but the best I got is this: datetime.datetime.fromtimestamp(time.mktime(time.localtime())) It seems...
1
by: vova777 | last post by:
On Jul 7, 6:21 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote: That is as simple as - std::ostream out(std::cout.rdbuf()); out << "state free formatted output"; Kind regards, Vladimir
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.