-3

Please help me create the pyramid with height "n" print correctly using hashes and spaces that is right-aligned. I have posted the code itself below. The program correctly asks for the user input, but doesn't build the pyramid right-aligned. If anyone can fix this, please help.

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

int main(void)
{
    int i=0;
    int n=0;

    do
    {
        printf("Height:");
        n=GetInt();
    } while (n<0 || n>23);

    for (i=0; i<n; i++)
    {
        printf(" ");
        for (int x=0; x<i+2; x++)
        {
            printf("#");
        }
        printf("\n");
    }

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
SKammala
  • 33
  • 1
  • 1
  • 3
  • 2
    I wonder what happens when you search the site for C questions containing "pyramid"... http://stackoverflow.com/search?q=%5Bc%5D+pyramid – paddy Aug 31 '15 at 04:16

19 Answers19

4

Here's one of the correct solutions!

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

void buildPyramid(int height);

int main(void)
{
    // Initialize the variable height
    int height;

    // Run the loop to get a value of height between 1 and 8, inclusive, from the user
    do
    {
        height = get_int("Height: ");
    }
    while (height < 1 || height > 8);

    // Call the function and pass height to it as a parameter
    buildPyramid(height);
}

// Declare the function buildPyramid
void buildPyramid(int height)
{
    // Loop to add a new line
    for (int i = 0; i < height; i++)
    {
        // Loop to add spaces
        for (int k = height - i; k > 1; k--)
        {
            printf(" ");
        }
        // Loop to add hashes
        for (int j = 0; j <= i; j++)
        {
            printf("#");
        }
        printf("\n");
    }
} 
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
2
#include<cs50.h>
#include<stdio.h>
#include<stdio.h>
void half_pyramid(int n);

int main(void){
    int height;
    do{
        height = get_int("Height: ");
    }while(height<=0||height>23);
    half_pyramid(height);
}
void half_pyramid(int n)
   {
    int spaces;     
    int dashes;
    for(int i = 2; i<=n+1;i++){
        for(spaces = (n-i); spaces>=0;spaces--){
            printf(" ");
        }
        for (dashes = 1; dashes <= i; dashes++){
            printf("#");
        }
        printf("\n");
    }
}
Community
  • 1
  • 1
Michael Elimu
  • 61
  • 2
  • 7
1
int main(void){
    int height;
    int spaces;
    int dashes;

    do 
    {
        printf("Height:");
        height = GetInt();
    }
    while (height <= 0 || height >= 23);
    //loop for the rows
    for (int i = 1; i <= height; i++){ //fixed the <= part of the operator
        //loop for the spaces and dashes per row
        for (spaces = (height - i); spaces >= 0; spaces--){
            printf(" ");
        }
        for (dashes = 1; dashes <= (i + 1); dashes++){
            printf("#");    
        }
        printf("\n");
    }
    return 0;
}
Community
  • 1
  • 1
Palaniichuk Dmytro
  • 2,943
  • 12
  • 36
  • 66
  • Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Jonathan Leffler Jul 30 '16 at 14:43
  • @JonathanLeffler there isn't any context because that solution is _potentially_ copy and paste from a [github repository from 2014...](https://github.com/Dnld/solutions-to-CS50/blob/master/mario.c) – rags2riches-prog May 10 '20 at 12:54
1
#include <cs50.h>
#include <stdio.h>

int main(void)
{
  int height;  
  int spaces;
  int hash;

do
{
    height = get_int("Height: ");
}
while (height < 0 || height > 23);


for (int i = 0; i < height; i++)
{
    // For loop to print out the spaces
    for (spaces = (height - i); spaces >= 2; spaces--)
    {
        printf(" ");

    }
    // For loop to print out hashes
    for (hash = 0; hash <= (i + 1); hash++)
    {
        printf("#");
    }
    printf("\n");
}
}
Amoury
  • 123
  • 1
  • 9
0

Inside your main loop you have:

// Instruction for printing 1 space
// Sub loop for printing x+1 number of hashes
// Instruction for printing a new line

So each time it loops round it's correctly drawing the hashes and a new line but your not telling it to draw any more than 1 space each time as unlike the subloop for the hashes the instruction doesn't increase with each pass.

0
#include<stdio.h>
#include<cs50.h>
int main (void)
{
    int height;
    int c = 0;
    do 
    {
        printf("Height?\n");
        height = GetInt();                                   

    }while(height < 0 || height > 23); 

    for(int a = height; a > 0; a--)               
    {
        for(int b = 0; b < a - 1; b++)                  
        {
            printf(" ");

        }
        printf("#");
        c++;
        for (int d = 0; d < c; d++)
        {
            printf("#");
        }
        printf("\n");
    }
}
0

Here is how I solved the version of this problem with both sides, but you can make it work with only one, just use 2 for-loops instead of three.

If we have to take a number 1 – 8 as an input which is the height of our pyramid, we can use one for loop to print out each row of the pyramid.

Inside this loop, we will need another two for loops that print spaces and hashes.

And since we need the other side of the pyramid as well, we will use three loops in total within our main loop.

Why three loops and not four? Because we don’t actually need to print space on the right side of the pyramid.

Beside the height variable we need another one that will work as a counter, since we cannot manipulate our height variable.

These for loops work in opposite direction, i.e. the more spaces we have, less hashes we print and vice versa.

So, the final CS50 Pset 1 Mario More solution looks exactly like this:

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

int main(void)
{
int height;

do {
    height = get_int("Height: ");
}
while (height < 1 || height > 8);

if (height > 0 || height < 9) {
    int counter = 0;
    for (int row=0; row<height; row++) {
        if (counter != height) {
            for (int spaces = (height-1) - counter; spaces > 0; spaces--) {
                printf(" ");
            }
            for (int hashes = 0; hashes <= counter; hashes++) {
                printf("#");
            }

  //ignore this block below if you want only one side

            printf("  "); 

            for (int hashes = 0; hashes <= counter; hashes++) {
                printf("#");
            }

  //##################################################

            printf("\n");
            counter++;
        }
    }     
}       
}

I actually made a blog post about this since I like to keep notes in case that you need more information.

0

This is how I got it to work for mario more comfortable.

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

int main(void)
{
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 1 || n > 25);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (i + j < n - 1)
                printf(" ");
            else
                printf("#");  
        }
            printf("  ");
            for (int j = 0; j < n; j++)
                {
                    if (n - i < j + 2)
                        printf("#");
                }       
                printf("\n");
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
james
  • 1
0
#include<cs50.h>
#include<stdio.h>

int main()
{

    int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 1);

    for (int i = 0; i<n; i++)
  {
      for (int j = 0; j < i+1; j++)
      {
          printf("#");
      }
      printf("\n");
  }

}
Nand
  • 611
  • 6
  • 12
0

I think your code won't be working properly in the last raw, for that the second loop starts with adding an empty block automatically for each i-iteration, however the last i-iteration to build the base raw doesn't have any empty block, but rather only #s. The code should look more like this:

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

int main(void)
{   int height;
    do {
    height = get_int("height: ");
}
    while (height<1 || height>8);
int i, j;
for ( i = 0; i < height; i++) {
    for ( j = 0; j < height; j++) {
        if (j + i < height - 1) {
            printf(" ");
         } else {
            printf("#");
         }         
    } 
    printf("\n");
}
}
Sal
  • 1
  • 1
0
    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
        // Get positive integer from user
        int height;
        do
        {
            height = get_int("Height: ");
        }
        while (height < 1 || height > 8);
    
        //for loop for next line
        for (int hash = 1; hash < height + 1; hash++)
        {
            for (int space = height - hash; space > 0; space--) // for loop to add spaces
            {
                printf(" ");
            }
            for (int hashwidth = 0; hashwidth < hash; hashwidth++)// for loop to add hashes
            {
                printf("#");
            }
            printf("\n");
        }

}
Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
0

Can the above problem be solved using so-called width.c program in the book C Primer Plus by Stephen Prata?

/* width.c -- field widths */
#include <stdio.h>
#define PAGES 959
int main(void)
{
printf("*%d*\n", PAGES);
printf("*%2d*\n", PAGES);
printf("*%10d*\n", PAGES);
printf("*%-10d*\n", PAGES);
return 0;
}
0
#include<cs50.h>
#include<stdio.h>

int main()
{

    int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 1 || n > 8);

    for (int i = 0; i<n; i++)
  {
      for (int k=n-i; k>1 ; k--)
      {
          printf(" ");
      }
      for (int j = 0; j < i+1; j++)
      {
          printf("#");
      }
      printf("\n");
  }

}
Suman
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 26 '21 at 10:20
0

After banging my head for awhile, I came across this, and it works.

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

int main(void)
{
    int height = 0;
    int line = 0;
    int column = 0;

    while (true)
    {
        height = get_int("Height: ");

        if (height >= 1 && height <= 8)
        {
            break;
        }
    }

    /* Chooses a row: */
    for ( line = 1 ; line <= height ; line++ )
    {
        /* Chooses a column: */
        for ( column = 1 ; column <= height ; column++ )
        {
            /* Prints an hash character or a space character: */
            if ( column >= height + 1 - line )
            {
                printf("#");
            }
            else
            {
                printf(" ");
            }
        }

        printf("\n");
    }
}
Okumu
  • 1
  • 1
0

I solved it using a combination of do-while loops and for loops. Once I had the first part of the pyramid; I added another for loop to create the second part of the pyramid.

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

int main(void)
{
    int num_blocks;
    int hashes;
    int spaces;
    do
    {
        //prompt user for input
        num_blocks = get_int("Enter the number of blocks between (1 -8) inclusive: ");

        //check user input
        if (num_blocks > 8 || num_blocks <= 0)
        {
            num_blocks = get_int("Enter the number of blocks between (1 -8) inclusive: ");
        }
    }
    //build mario game
    while (num_blocks < 0);

    for (int i = 0; i < num_blocks; i++)
    {
        // print out spaces
        for (spaces = (num_blocks - i); spaces >= 2; spaces--)
        {
            printf(" ");
        }
        //loop to print out hashes
        for (hashes = 0; hashes <= i; hashes++)
        {
            printf("#");
        }
        printf("  ");
        //loop to print other hashes
        for (hashes = 0; hashes <= i; hashes++)
        {
            printf("#");
        }
        printf("\n");
    }
}
Zak
  • 9
  • 3
-1
#include <stdio.h>
#include <cs50.h>


  int main(void){
  int s=0;
  int hash=0;
  int ht;
  int h;

  do{
    printf("Height: ");
     ht=GetInt();
     for (h = ht; h > 0 ; h--){
      printf("\n");
      hash = ht - (h - 1);
      s = ht - hash;
      int i;
      for (i = 0; i <= s; i++){
        printf(" ");
      }
      for (i = 0; i <= hash; i++){
        printf("#");
       }
      printf("  ");
      for (i = 0; i <= hash; i++){
        printf("#");
      }
    }
     return 0;
  }
  while(ht > 0 || ht < 23);


  return 0;
}
  • 1
    Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Ajean May 20 '16 at 01:48
  • Doesn't solve the problem. You've left out the requirement to enter a valid number before printing anything. – James Jan 21 '17 at 16:49
-1
 //this is how you only make the right aligned pyramid
#include <stdio.h>
#include <cs50.h>


int main(void){
  int s=0;
  int hash=0;
  int ht;
  int h;

  do{
     printf("Height: ");
    ht=GetInt();
    for (h = ht; h > 0 ; h--){
      printf("\n");
      hash = ht - (h - 1);
      s = ht - hash;
      int i;
      for (i = 0; i <= s; i++){
        printf(" ");
      }
      for (i = 0; i <= hash; i++){
        printf("#");
      }
    }
    return 0;
  }
  while(ht > 0 || ht < 23);


  return 0;
}
  • Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Jonathan Leffler Jul 30 '16 at 14:44
-1
//Author: Andriyevski Serhiy
#include <stdio.h>
#include <cs50.h>

int main(void)
{

    // declaring my variables
    int height;
    int all_row;
    int space;
    int hash;

    // prompts user for integer until integer is 0 to 23
    do
    {
        printf("Please choose a number from 0 to 23:");
        height = GetInt();
    }
    while ((height < 0) || (height > 23));

    // this is the loop that will create the number of rows in the pyramid entered by the user
    for (all_row = 1; all_row <= height; all_row++) 
    {
        for (space = (height - all_row); space > 0; space--)
        {
            printf(" "); 
        }

        for (hash = 1; hash <= (all_row + 1); hash++)
        {   
            printf("#"); 
        }

        printf("\n");
    }
    return 0;
}
-2
int main(void)
{
    do
    {
        height = get_int("Height: "); //Requesting input from user for height.
    }

    while (height < 0 || height > 8); //Limits user input to between 0 and 8.

    for (int rows = 1; rows <= height; rows++)
    {
        for (int spaces = 0; spaces < (height - rows); spaces++) //Based on the row number, there will be a certain amount of spaces. E.g. If the height is 8 and it is row 1, there are 7 spaces.
        {
            printf(" ");
        }

        for (int hashes = 0; hashes < rows; hashes++) //Based on which row it is, the number of hashes printed is equal to the row number. E.g. row 1 has 1 hash.
        {
            printf("#");
        }

        printf("  ");

        for (int hashes = 0; hashes < rows; hashes++) //This prints the right side of the pyramid. No need to print the spaces.
        {
            printf("#");
        }
        printf("\n");
    }

Hopefully this code is reasonable enough to read and hope it helps!

Also, it helped me to realise that the terminal prints from top to bottom and this was a main factor in designing the algorithm for this problem.