0

for the past few weeks I've been struggeling with making the half-piramid in pset1.

I managed to come up with this code that prints the height of the piramid according to the user input, but after that the code keeps printing more rows of hashes.

I can kind of understand why it's logical to the computer to execute the code like this, because it's literally doing what I'm telling it to do, but I can't figure out a way on how to fix it.

Also I'm sure my code is very inefficient, but I still seem to get really close to the solution using it. It would be greatly appreciated If anyone could give me some tips on how to wrap my mind around it :).

Here's my code:

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

int main(void)
{
//prompt user for positve number
int n;
do
{
  n = get_int("Positive Number: ");
}
while (n <= 0 || n > 23);


// print out that many bricks for line 1 if user input is N
for (int i = 1; i < n; i++)
{
printf(" ");
}
  for (int j = n - 2; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}


// print out that many bricks for line 2 if user input is N
for (int i = 2; i < n; i++)
{
printf(" ");
}
  for (int j = n - 3; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

    // print out that many bricks for line 3 if user input is N
for (int i = 3; i < n; i++)
{
printf(" ");
}
  for (int j = n - 4; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}


        // print out that many bricks for line 4 if user input is N
for (int i = 4; i < n; i++)
{
printf(" ");
}
  for (int j = n - 5; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 5 if user input is N
for (int i = 5; i < n; i++)
{
printf(" ");
}
  for (int j = n - 6; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 6 if user input is N
for (int i = 6; i < n; i++)
{
printf(" ");
}
  for (int j = n - 7; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 7 if user input is N
for (int i = 7; i < n; i++)
{
printf(" ");
}
  for (int j = n - 8; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 8 if user input is N
for (int i = 8; i < n; i++)
{
printf(" ");
}
  for (int j = n - 9; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 9 if user input is N
for (int i = 9; i < n; i++)
{
printf(" ");
}
  for (int j = n - 10; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 10 if user input is N
for (int i = 10; i < n; i++)
{
printf(" ");
}
  for (int j = n - 11; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 11 if user input is N
for (int i = 11; i < n; i++)
{
printf(" ");
}
  for (int j = n - 12; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 12 if user input is N
for (int i = 12; i < n; i++)
{
printf(" ");
}
  for (int j = n - 13; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 13 if user input is N
for (int i = 13; i < n; i++)
{
printf(" ");
}
  for (int j = n - 14; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 14 if user input is N
for (int i = 14; i < n; i++)
{
printf(" ");
}
  for (int j = n - 15; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 15 if user input is N
for (int i = 15; i < n; i++)
{
printf(" ");
}
  for (int j = n - 16; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 16 if user input is N
for (int i = 16; i < n; i++)
{
printf(" ");
}
  for (int j = n - 17; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 17 if user input is N
for (int i = 17; i < n; i++)
{
printf(" ");
}
  for (int j = n - 18; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 18 if user input is N
for (int i = 18; i < n; i++)
{
printf(" ");
}
  for (int j = n - 19; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 19 if user input is N
for (int i = 19; i < n; i++)
{
printf(" ");
}
  for (int j = n - 20; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 20 if user input is N
for (int i = 20; i < n; i++)
{
printf(" ");
}
  for (int j = n - 21; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 21 if user input is N
for (int i = 21; i < n; i++)
{
printf(" ");
}
  for (int j = n - 22; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 22 if user input is N
for (int i = 22; i < n; i++)
{
printf(" ");
}
  for (int j = n - 23; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}

            // print out that many bricks for line 23 if user input is N
for (int i = 23; i < n; i++)
{
printf(" ");
}
  for (int j = n - 24; j < n; j++)
{
printf("#");
}
  for (int k = n - 1; k < n; k++)
{
printf("\n");
}
}
0

0 is a valid height.

You should nest loops.

Have an outer loop counting the rows.

Inside that, have one or more loops printing the spaces and hashes, and finally something printing a newline (one newline per row). The number of spaces and hashes to print can be calculated from height and row number, or you could determine initial number of spaces/hashes, and decrement/increment those at the end of the outer loop.

You must log in to answer this question.

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