Hi everyone,
I'm taking a course online, one of my projects is to build a half pyramid to user specified dimensions.
I've only been using C 4 days, watched all the teaching vids on the course, read an ebook and watched some youtube tutorials, so please be gentle!.
my code:
Code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
int h = 0; //height integer preset
do
{
printf("Please enter a height between 1 and 23: ");
h = GetInt();
}
while (h <0 || h >23); /**
checks if value given is between the desired integers,
if not user will be prompted for input again
*/
if (h == 0 )
{
printf("");
}
else
printf("\n");
int l =1, w = 1; //l = length of pyramid, w = width of pyramid
/**
The following code nest runs though checks and prints the correct
number of spaces and "#", also placing the next line command.
The loop will also make sure a block is deleted on each line to
allow the pyramid to be the correct dimensions
*/
for (l = 2; l <= h; l++) // provides the code for the length
{
for (w = 1; w <= h; w++) //provides the code for the length
{
if (w >= h +1 -l) //handles the removal of the troublesome block
{
printf ("#");
}
else
{
printf (" ");
}
}
printf("\n");
}
}
my problem is, my pyramid is always one layer short, if I ask for a 5 layer pyramid it gives me 4?
the pyramid has to start with a double block on the top i.e
##
###
####
#####
and be aligned to the right, which i've managed and got working after some fiddling and brain re-wiring.
can anyone see why i'm a layer short please?