0

I recently got to trying to solve problem set 1, where I am required to print a pyramid with a user inputted value between 1 and 8 (inclusive). After a while, I got this code:

#include <cs50.h>
#include <stdio.h>

int get_positive_int(string prompt);
int y;
int n;

int main(void)
{
    int i = get_positive_int("Height: ");
    for (int x = 0; x < n; x++)
    {
        for (y = x; y < n; y++)
        {
            printf("#");
        }
        printf("\n");
    }
}

// Ask for integer between 1 and 8
int get_positive_int(string prompt)
{
    do
    {
        n = get_int("%s", prompt);
    }
    while (n > 8 || n <= 0);
    return n;    
}


The pyramid prints upside down, so is it possible for anyone to provide me with some insight on how to make it print properly?

Thanks!

0

A loop

for (y = x; y < n; y++)

would run n - x times (end minus start value). How many spaces and hashes would the row with index x have to consist of?

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .