By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Stairs program

P: n/a
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.
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++)
{
makeTread (k);
makeRise (k + 1);
}
makeTread (N);
}

int
main(void)
{
makeStairs (6);

return (0);
}

Can somebody give me a hint?

Nov 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Nov 3 '06 #2

P: n/a
2006-11-03 <1162531994.6788.11.camel@abacus>,
Andrew Poelstra wrote:
(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.)
I've never heard of a newsserver doing that. Is that even allowed by the
nntp/usenet standards?
Nov 3 '06 #3

P: n/a
Jordan Abel wrote:
2006-11-03 <1162531994.6788.11.camel@abacus>,
Andrew Poelstra wrote:
>(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.)

I've never heard of a newsserver doing that. Is that even allowed by the
nntp/usenet standards?
Whether it is allowed and whether it is the server, the posting client,
or the receiving client, we have seen it happen here many times.
--
Flash Gordon
Nov 3 '06 #4

P: n/a
In article <sl*******************@rlaptop.random.yi.org>,
Jordan Abel <ra*******@gmail.comwrote:
>2006-11-03 <1162531994.6788.11.camel@abacus>,
Andrew Poelstra wrote:
>(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.)

I've never heard of a newsserver doing that. Is that even allowed by the
nntp/usenet standards?
nntp requires 7 bit ASCII (the 8th bit is explicitly zeroed.)
The message body definition falls through to the Usenet
message definition, which in turn for the body falls through to RFC822,
the email definition. And RFC822 indicates that message bodies
consist of streams CRLF delimited lines of any ascii character
(0-127) [except for CRLF].

Which is to say, you are correct that news servers -should- be passing
tabs through unchanged. But posting clients and reading clients
mess tabs up more often than not.
--
All is vanity. -- Ecclesiastes
Nov 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.