-
Creating a half pyramid
Hello all!
I have a problem I need help with. I am tasked with creating a half pyramid like below. First I prompt user to enter an integer less than 23 to dictate how many asterisks in height the pyramid will be. So let's say they entered 8. The half pyramid would need to look like this (ignore the dashes; I had to use them instead of spaces to format here)
---------**
--------***
------****
-----*****
----******
---*******
--********
-*********
Here is the code I have so far.
Code:
#include <stdio.h>
#include <cs50.h>
int main(int argc, char *argv[])
{
int maxSize;
int xAxisCounter;
int yAxisCounter;
do
{
printf("Enter an integer less than 23: ");
maxSize=GetInt();
}
while(maxSize > 23);
for (yAxisCounter = 0; yAxisCounter < maxSize; yAxisCounter++)
{
for (xAxisCounter = 0; xAxisCounter < maxSize; xAxisCounter++)
{
printf(" %i ",xAxisCounter);
}
printf("\n");
}
system("PAUSE");
return 0;
}
This code will draw a square like this if the user entered 8 (but with numbers because I have code set to %i right now)
********
********
********
********
********
********
********
********
On top of all this, the whole pyramid has to be on the right side of the console.. right justified.
I am losing hope I can be a programmer because I just don't get it. I have been at this four days and tried many things and nothing works
-
Code:
prompt for size of half-pyramid base:
for each line
spaces = base_size - 1 - current line
stars = 1 + current line
output spaces
output stars
output newline
Something like that.
Quzah.
-
I think of it as you have a total width to cover, either with spaces (which will always come first), or stars.
With each line you print, the number of stars to print, increases. Total width - number of stars == number of spaces to print.
Do that calculation before each line, you can use a for or a while loop, and just stop when the number of stars equals the number you need.
-
Sorry I don't follow either reply.
-
Well then programming isn't the thing for you. The only way to make that clearer is to write it for you, which I practically did.
Quzah.
-
"Well then programming isn't the thing for you.."
Ironic; I'd say given your lack of patience, TEACHING isn't for YOU.
In any case, I got it...
Code:
#include <stdio.h>
#include <cs50.h>
int
main(int argc, char *argv[])
{
int maxSize;
int xAxisCounter;
int yAxisCounter;
do
{
printf("Enter an integer less than 23: ");
maxSize=GetInt();
}
while(maxSize >23);
for (yAxisCounter = 2; yAxisCounter < maxSize; yAxisCounter++)
{
for (xAxisCounter = 0; xAxisCounter < 80; xAxisCounter++)
{
if(xAxisCounter<80-yAxisCounter)
printf(" ");
else
printf("O");
}
}
printf("\n\n");
system("PAUSE");
return 0;
}
keywords: harvard university cs50, half pyramid, malan, mario
-
You didn't tell us you were attending Harvard!
There's the problem. You're an over-educated idiot.
We would have lowered our expectations, if only we'd known. :)
Since we're a little over-geeked, and our posters tend to be a little over-geeked, as well, it's a great fit. We're the same kind of idiots - very important for good communication.
We have much less useless baggage and a bit more common sense, than what you must be carrying around.
You have our *deepest* sympathies. :p :p :p
-
Okay, we can stop the personal attacks now. *thread closed*
rightbrainer, next time state what exactly were you unable to understand. Just saying "I don't follow either reply" makes it seem like you made no attempt to understand and merely wanted the answer to be handed to you on a silver platter.