Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

c - Making a Hash Pyramid

Currently doing the CS-50 course and was wondering if anyone could help me with this. I'm supposed to create a program which will ask a user for a height between 1-23 (and continuously prompt the user until a valid answer is given) --- I was able to code that part.

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

int main(void)
{ 
    int height;
    do
    {
        printf("please give me a height between 1-23: ");
        height = GetInt();
    }    
    while (height < 1 || height > 23);
}

The do while loop seems to do what its intended. Now, the program, given the variable "height" now needs to create a pyramid of that height. The bottom of the pyramid is to be aligned with the bottom left hand of the terminal and its last "row" is to finish with 2 hashes as such:

Sample pyramid of height 4:

    ##
   ###
  ####
 #####

But the code needs to be generic for any height of pyramid 1-23. This is where I'm having a hard time (in actually making a code to draw this).

Ive noticed that for each row, the number of hashes needed (if we call the top row "row 1" and the subsequent "row 2" and so on... is row number+1. As for the amount of spaces that are needed, can be represented by height-row number. If someone would be able to explain to me how I could write this program using C, it would be much appreciated! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a way you can implement this. Basically, you need to build the pyramid from the bottom up. The task is easy once you see the loop structure, its just tricky to get the math down for printing the correct number of spaces and hash symbols:

#include <stdio.h>

int main(void)
{ 
    int height, i, j;
    do
    {
        printf("please give me a height between 1-23: ");
        height = GetInt();
    }    
    while (height < 1 || height > 23);

    printf("
");    
    for (i = 0; i < height; i++) {

        for (j = 0; j < height - i - 1; j++)
            printf(" ");
        for (j = 0; j < i + 2; j++)
            printf("#");

        printf("
");
    }
}

For more clarification on whats going on, and why each loop is necessary:

  1. Outer for loop: the variable i corresponds to a row in the pyramid. the value of i will remain constant for each of the second two loops

  2. First inner for loop: for any row, there needs to be height - i - 2 spaces. You can figure this out because the total row width will be height, and any row has i + 2 hash symbols, so there needs to be height - (i + 2) = height - i - 1 spaces. So basically, this loop just prints the required spaces. You can track this with the variable j

  3. Second inner for loop: This loop is similar to the first inner loop, but you need to now print the hash marks. At the beginning of the loop you reset j and count up to the required number of hash marks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...