Currently doing the CS-50 course and was wondering if anyone could help me with this. I'm supposed to create a program which will ask a user for a height between 1-23 (and continuously prompt the user until a valid answer is given) --- I was able to code that part.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do
{
printf("please give me a height between 1-23: ");
height = GetInt();
}
while (height < 1 || height > 23);
}
The do while loop seems to do what its intended. Now, the program, given the variable "height" now needs to create a pyramid of that height. The bottom of the pyramid is to be aligned with the bottom left hand of the terminal and its last "row" is to finish with 2 hashes as such:
Sample pyramid of height 4:
##
###
####
#####
But the code needs to be generic for any height of pyramid 1-23.
This is where I'm having a hard time (in actually making a code to draw this).
Ive noticed that for each row, the number of hashes needed (if we call the top row "row 1" and the subsequent "row 2" and so on... is
row number+1. As for the amount of spaces that are needed, can be represented by height-row number.
If someone would be able to explain to me how I could write this program using C, it would be much appreciated! :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…