This is my code for pset1's mario problem but everytime I enter a height, the output of the pyramid always has the first row row with an extra space? What am I doing wrong? Could someone please guide me to see what I am doing wrong! This would be much appreciated thank you

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

/* ...
rows are < the height bc you start at 0. So rows is rows=0, then rows<height, rows++

// get the user input
// print the right number of rows
// print the right number of spaces. 
// print the right number of hashes on each row
// new line

int main (void)
int height, rows, spaces, hashes;
        printf("Height of pyramid: ");
        height= GetInt();
    while ((height > 23) || (height < 0));

    for (rows=0; rows < height; rows++) //makes rows

        for (spaces=0; spaces < height - hashes ; spaces++)
            printf(" "); //prints a space 
        for (hashes=0; hashes < rows + 2; hashes++)

        printf("\n"); // new line


    return 0;

you have all the work done, but in your code, you have the variable hashes uninitialized

for (spaces = 0; spaces <height - hashes; spaces ++)

after an innermost loop hashes is initialized to zero, but this does not affect the outermost variable, since you have most of the written code, the problem is in the second loop FOR, the spaces decrease as much as grow row. I hope it helps (although it seems that lately I have not much success)

  • Hey, when I do this, the pyramid faces the wrong way :(
    – rdp0821
    Jun 12 '16 at 19:16
  • parrafo (Espacios = 1; <Espacios de Altura - filas; Espacios ++), that is all, the program works properly myself have compiled, How can work the other way round on your computer?
    – MARS
    Jun 12 '16 at 19:51
  • ohh ok!! i thought the space had to = 0
    – rdp0821
    Jun 12 '16 at 19:51
  • thank you! for your help
    – rdp0821
    Jun 12 '16 at 19:52
  • happy to help, go ahead with the course!
    – MARS
    Jun 12 '16 at 20:14

You must log in to answer this question.

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