Beginning C Program

冷暖自知 提交于 2019-12-12 15:44:34

问题


I was working though some beginning problem sets with Harvard's online CS50 class. I got the problem to work correctly but I was wondering if there would possibly be a cleaner or better way to get the program to work.

The goal of the program is to print a right-aligned pyramid comprised of hash-tags and space characters. Any guidance in regards to style or tricks would be very welcome.

/* Creating the mario program, whose goal is to create a 
*  pyramid by accepting input from the user to get the 
*  height then aligning the pyrimid to the right.
*
*/

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

int main(void)
{

    // get user input and set to variable
    printf("Height: ");
    int height = GetInt();

    int i, j, k;
    for(i = 1 ; i < height; i++)
    {

        // create n-1 spaces
        for(k = (height - 2); k > (i-1); k--)
        {
            printf("%c", ' ');      
        }

        // create n+1 hash tags
        for(j = 0; j < (i+1); j++)
        {
            printf("#");
        }

        printf("\n");
    }
    return 0;
}

回答1:


I'm assuming by cleaner you mean "spiffy and fancyer".

This looks spiffy to me:

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

int main(void) {
    // get user input and set to variable
    printf("Height: ");
    int height = GetInt();
    int hm2 = height - 2;

    int j, k;
    for(int i = 1 ; i < height; i++) {
        // create n-1 spaces
        for(k = hm2; k > (i-1); k--)
            printf("%c", ' ');      

        // create n+1 hash tags
        for(j = 0; j < (i+1); j++)
            printf("#");

        printf("\n");
    }
    return 0;
}

However, don't get too caught up in making your code fancy. Although it's nice if you're working with others, or yourself really. Your example looked fine.

Now, optimization-wise, that's something to worry about. Just remember that too much optimization can potentially break your program.




回答2:


For everyone's consideration: this is what "all style and no readability" looks like :)

i = 0;
while (i++ < height*height)
    printf ("%c%s", (i-1)/height < height-(i-1)%height-1 ? ' ' : '#',
    i % height ? "" : "\n");

It is nigh on impossible to see what the code does without running it. If there is to be a follow-up exercise, this is hard to re-write to form, say, an even-sided pyramid. I'd probably throw this away and start again with the basics, before concatenating it again into a little monster such as this.


(later) Ever so slightly more neat to put the i++ at the end, so two times (i-1) gets traded for a slightly more complicated end-of-line test:

i = 0;
do
    printf ("%c%s", i/height < height-i%height-1 ? ' ' : '#',
    i % height==height-1 ? "\n" : "");
while (++i < height*height);



回答3:


I think by cleaner and better way you mean to be a perfect shaped right angled triangle pyramid.
For this you should do as
Change

printf("Height: ");  

to

printf("Height: \n\n");  

and

for(i = 1 ; i < height; i++)  

to

for(i = 0 ; i < height; i++)   

And see the sample output.




回答4:


Here's a suggestion:

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

int main(void) {
    //initialize variables
    int height, hm2, j, k, i;

    printf("Height: \n");
    // Get user input
    height = GetInt();
    hm2 = height - 1;
    for(i = 0; i < height; i++) {
        // create n spaces
        for(k = hm2; k > i; k--)
            printf("%c", ' ');      

        // create n+1 hash tags
        for(j = 0; j < i+1; j++)
            printf("#");

        printf("\n");
    }
    return 0;
}

Result if the user entered a 5 for the height:

Height: 
    #
   ##
  ###
 ####
#####

A couple things I considered with this version of the code:

-In C, it's good practice to declare all variables separately from giving them a value and assign values later. Some compilers may bring up this error if you to declare and assign a value in a for loop: "error: ‘for’ loop initial declarations are only allowed in C99 mode". These changes are considered with what I have provided.

//initialize variables                                                            
int height, hm2, j, k, i;

-I added a newline here

printf("Height: \n");

-Instead of hm2 = height - 2 I changed it to:

hm2 = height - 1;

-First loop, now we give i a value and set it to 0 to meet the other changes that were made:

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

-For the loop creating n spaces I changed it to:

for(k = hm2; k > i; k--)

-Finally removed parenthesis (no need in this case) in last for loop:

for(j = 0; j < i+1; j++)

Cheers



来源:https://stackoverflow.com/questions/20182057/beginning-c-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!