Ive been struggling with this problem for quite some time now and im finally caving and asking for help. first ill show you my code and explain my thought process.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height, rows, spaces, hashes;
do
{
printf("How high will mario climb? Please pick a number between 1-23: ");
height = GetInt();
}
while (height <= 0 || height >= 24);
for (rows = 0; rows <= height; rows++) //print user generated amount of rows
{
for (spaces = height - rows - 2 ; spaces == 0; spaces --) //print spaces
{
printf(" ");
}
for (hashes = rows + 2; hashes <= height; hashes++) //print hashes
{
printf("#");
}
printf("\n");
}
return 0;
}
this is printing: usuing user input (10)
##########
#########
########
#######
######
#####
####
###
##
#
at first I thought I did something backwards so i switched space to print "s" and hashes to print "h" expecting
hhhhhs
hhhhss
hhhsss
hhssss
shssss
but got
hhhhhh
hhhhh
hhhh
hhh
hh
h
So I believe the problem lies somewhere in the for loops, but I can not figure out what it is. (I'm also aware the final row is messed up but Im trying to fix the pyramid first)
my logic behind: (rows = 0; rows <= height; rows++) //print user generated amount of rows
is that the rows will increase by one until it reaches the height specified by the user (I believe everything up to and including this line of code is correct)
...
my logic behind : (spaces = height - rows - 2 ; spaces == 0; spaces --) //print spaces
is that the amount of spaces should = the height (10) - rows (0) - 2 and then decrease by 1 each time through the loop until eventually there is only hashes. this way the first row should leave room for 2 hashes(top of the pyramid)
...
logic behind (hashes = rows + 2; hashes <= height; hashes++) //print hashes
is that the amount of hashes should be = to the row (0) + 2 increasing by 1 each time though the loop until it is greater than or equal to the user input height. This way the first row will include 2 hashes (0 + 2 = 2)
any insight as to where the problem is and tips or advise on how to come up with the solution would be greatly appreciated !