On Thu, 2006-11-02 at 20:57 -0800, bluepgt wrote:
I'm trying to write a stairs program. It's supposed to print stairs.
This is what I got:
+---+
| |
+---+---+
| | |
+---+---+---+
| | | |
+---+---+---+---+
| | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+---+
| | | | | | |
+---+---+---+---+---+---+
Now I can't figure out how to get it to print the inverse to the left
so it looks like a pyramid.
Who's your teacher? Tell him that he's doing a great job: this is the
most well-written "hobby program" I've seen in a long time. If you're
self-taught, then congratulations. (The only issue I see is that you
shouldn't use tabs on Usenet: they may not display correctly or at all,
and some newsservers filter them out.)
The code:
#include <stdio.h>
void makeTread (int n_tread)
{
int i;
printf("+");
for (i = 0; i < n_tread; i++)
{
printf("---+");
}
printf("\n");
}
void makeRise (int n_rise)
{
int i;
for (i = 0; i < n_rise; i++)
{
printf("| ");
}
printf("\n");
}
void makeStairs (int N)
{
int k;
for (k=1; k <= N; k++)
Note: It's best to start with 0 and go up to one less than N:
for(k = 0; k < N; k++)
This is a more common idiom.
{
makeTread (k);
makeRise (k + 1);
}
makeTread (N);
}
int
main(void)
{
makeStairs (6);
return (0);
The return statement doesn't need parentheses, just so you know.
}
Can somebody give me a hint?
In order to draw a pyramid properly, you'll need to know the base size,
in order to center the top correctly. From an algorithmic perspective,
this could be tricky. You'll need to pass a base_size value to your
makeTread() and makeRise() functions, and then add spacing
appropriately.
If you want to print both sides at the same time, and want your top
level to have one box instead of two (so that it isn't mirrored
boxwise, but instead half-boxwise), you'll have some more problems.
Take a pencil and paper, and write out equations for:
1) The number of boxes at each level.
2) The amount of spaces needed.
3) The most height you can get before exceeding 80 characters (which
is the maximum width of many terminals).
Finally, I recommend that you also post your question in
comp.programming; you'll probably get some more help there.
--
Andrew Poelstra <http://www.wpsoftware.net>
For email, use 'apoelstra' at the above site.
"You're only smart on the outside." -anon.