So, I am trying now to make a pyramid of hashtags which in every row has a number of spaces before from the left side of the hashtag. In the program it starts with a prompt from the user between 1-8 , and then that´s the height of the pyramid. Like this:
#
##
###
####
#####
######
#######
To start with, as a first step, I am putting dots (.) where the spaces should go, to make it visible to me. And then comes the hashtag. I am wondering why it only gets printed dots when I programmed like this?
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int height, rows, width, space;
//Get prompt, number of rows from user
do
{
height = get_int("How many rows? ");
}
while(height < 1 || height > 8);
//Number of rows
for(rows = 0; rows < height; rows++)
{
//For number of dots (width)
for(space = 1; space < (height - rows); space++)
{
printf(".");
}
//For number of hashtags
for(width = 0; width > (height - space); width++)
{
printf("#");
}
printf("\n");
}
}
Actually now I think I solved it! But please help me to formulate the logic. Because I am limping in my thought to explain it clearly in my head... Anyway I solved it like this:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int height, rows, width, space;
//Get prompt, number of rows from user
do
{
height = get_int("How many rows? ");
}
while(height < 1 || height > 8);
//Number of rows
for(rows = 0; rows < height; rows++)
{
//For number of dots (width)
for(space = 1; space < (height - rows); space++)
{
printf(".");
}
//For number of hashtags
for(width = (height - rows); width <(height + 1); width++)
{
printf("#");
}
printf("\n");
}
}