473,513 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple exercise:Printing E with '*'

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<stdio.h>
#include<stdlib.h>
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<stdio.h>
#include<stdlib.h>

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 9353

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

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 <stdio.h>

int main(void)
{
printf("*****\n");
printf("*\n");
printf("*\n");
printf("***\n");
printf("*\n");
printf("*\n");
printf("*****\n");

return 0;
}

Or even:

#include <stdio.h>

int main(void)
{
printf("*****\n*\n*\n***\n*\n*\n*****\n");

return 0;
}

;-)

Apr 13 '06 #3
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.
<snip>
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

"sathyashrayan" <sa***********@REMOVETHISgmail.com> 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<stdio.h>
#include<stdlib.h>
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<stdio.h>
#include<stdlib.h>

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
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
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'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<stdio.h>
#include<stdlib.h>
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 and
too ugly.
So I made a second try which is not working.
What does that mean? Define "not working".
#include<stdio.h>
#include<stdlib.h>

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("*");
}
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 two
lines of
'*'. Can any one suggest some solution for this simple
problem.Don't
simply 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4695
by: Maileen | last post by:
Hi, I have 2 questions : 1. - I want to print from my ASP page directly to my network printer. For this i use the following code, but everytime, my document to print is sent to local and...
2
1543
by: Jack Russell | last post by:
Gee vb.net makes me feel dumb! My understanding is that I set up and do all of my printing in the printdocument printpage and handler using various graphics methods such as drawstring. Is...
7
2473
by: Dennis C. Drumm | last post by:
I was wondering if someone could suggest were/how I could get started with setting up printing of a collection of text that includes tables from a grid and other supporting information about the...
0
874
by: Randy | last post by:
I have a simple Porgram that prints the text in a txtbox to the printer. I used to be able to do this very easly in VB6 but not the code does not work in .net vb CD1.ShowPrinter ' CommonDialog...
4
7296
by: Noozer | last post by:
Can someone provide a simple example of how to print using VB.Net??? Basically just looking for a "HELLO WORLD" example to the printer. I'll worry about formatting, etc. later on. Right now I...
3
404
by: adam | last post by:
Is there not a way to do a simple print like in vb6? Printer.Print All I want to do is put a could of text lines on a page.
1
1499
by: mitch | last post by:
Hi, Could anyone help with the following. I have a windows form. On it I have a function (DrawCross) to draw a cross throught the centre of the form from corner to corner. The function which...
6
3226
by: Chris Dunaway | last post by:
The method for printing documents in .Net can be confusing, especially for newer users. I would like to create a way to simplify this process. My idea would be implemented using a PrintDocument...
15
3139
by: =?Utf-8?B?cGF0cmlja2RyZA==?= | last post by:
Hi everyone! Does anyone know how can I do barcode printing? First of all, I know I have to write a client executable, but how do I communicate with the thermal printer? Using software the...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
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
7380
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
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.