Skip to main content
added 1474 characters in body
Source Link

EDIT: UPDATED CODE BELOW

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

int main(void)
{
    
    printf("How tall is the pyramid?: ");
    int pyramidHeight = GetInt(); //get height of pyramid from user
    
    while(pyramidHeight <= 0 || pyramidHeight > 23) //if the user inputs a number less than or equal to 0 or greater than 23, prompt for a number again
    {
        printf("Please enter a number between 1 and 23: ");
        pyramidHeight = GetInt();
    }

    int space = pyramidHeight - 1; //initial amount of spaces to print
    int hash = 2; //initial amount of hashes to print

    for(int currentHeight = 0; currentHeight != pyramidHeight; currentHeight++) //while current pyramid height is less than final height
    {
        for(int spaceCurrent = space; spaceCurrent != 0; spaceCurrent--) //spaceCurrent = number of spaces currently being printed
        {
            printf(" ");
        }
        
        space--; //print one less space next time
        
        for(int hashCurrent = 0; hash != hashCurrent; hashCurrent++) //hashCurrent = number of hashes currently being printed
        {
            printf("#");
        }
        
        hash++; //print one more hash next time
        
        printf("\n"); // move to next line
    }
    
    
}

EDIT: UPDATED CODE BELOW

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

int main(void)
{
    
    printf("How tall is the pyramid?: ");
    int pyramidHeight = GetInt(); //get height of pyramid from user
    
    while(pyramidHeight <= 0 || pyramidHeight > 23) //if the user inputs a number less than or equal to 0 or greater than 23, prompt for a number again
    {
        printf("Please enter a number between 1 and 23: ");
        pyramidHeight = GetInt();
    }

    int space = pyramidHeight - 1; //initial amount of spaces to print
    int hash = 2; //initial amount of hashes to print

    for(int currentHeight = 0; currentHeight != pyramidHeight; currentHeight++) //while current pyramid height is less than final height
    {
        for(int spaceCurrent = space; spaceCurrent != 0; spaceCurrent--) //spaceCurrent = number of spaces currently being printed
        {
            printf(" ");
        }
        
        space--; //print one less space next time
        
        for(int hashCurrent = 0; hash != hashCurrent; hashCurrent++) //hashCurrent = number of hashes currently being printed
        {
            printf("#");
        }
        
        hash++; //print one more hash next time
        
        printf("\n"); // move to next line
    }
    
    
}
Source Link

How can I improve my code for pset1 mario?

This is my code for pset1 mario problem. It works but it looks really messy and inefficient. I think I could use less variables and use the while loops better but I'm not sure where to begin. I'd be happy if someone could point out where and why my code is inefficient so I can fix it

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

int main(void)
{
    int pyramidHeight;
    
    printf("How tall is the pyramid?: ");
    pyramidHeight = GetInt();
    
    while(pyramidHeight <= 0 || pyramidHeight > 23)
    {
        printf("Please enter a number between 1 and 23: ");
        pyramidHeight = GetInt();
    }
    
    int spaces = pyramidHeight - 1; //amount of spaces(should end up at 0)
    int spacesHolder = pyramidHeight -1; //place holder for holding spaces
    int hashes = 2; //how many hashes to print
    int hashesHolder = 2; //counts how many hashes we're supposed to print next
    int finalHashes = pyramidHeight + 2; //number of hashes it should end up at (It should be pyramidHeight + 1 but when I put 1 it ends too early)
    
    while((hashes != finalHashes)) //run this while final amount of hashes has not been printed
    {
        while(spaces != 0)
        {
            printf(" ");
            spaces--;
        }
        
        spacesHolder--; //print -1 space next line
        spaces = spacesHolder; 
        
        while((hashes != 0))
        {
            printf("#");
            hashes--;
        }
        
        hashesHolder++; //print +1 hash next line
        hashes = hashesHolder; 
        
        printf("\n"); //move to next line
        
    }
    
}