I was trying to program a Mario half pyramid in C but my code is not doing anything.
I originally messed up with loop breakers with it putting everything upside down and when I fixed it it is simply asking for an input and not doing anything else.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
int space;
int rows;
int hashes;
// The code below decides if the users input meets the guide lines
do
{
printf("Enter the heigth of the pyramid here");
height = GetInt();
}
while (height <= 0 || height > 23);
for(rows = 1 ;rows > height ;rows++)
{
// the code gives the number of spaces per row
for(space = height - 1;space >= 1;space--)
{
printf(" ");
};
//The code below gives the number of hashes that have to be printed
for(hashes = height + 1 - space; hashes> 0; hashes--)
{
printf("#");
};
height = height + 1
printf("\n");
}
};
Your for loop is not going to iterate for a single time if height is not less than 1.
for(rows = 1 ;rows > height ;rows++)
{
//....
I modified your code like the following for height = 5
for (rows = 1; rows <= height; rows++) {// iterate up to height times
for (space = height - rows; space >= 1; space--) { // at first print spaces height -1 times. then spaces will be reduced by 1 from previous.
printf(" ");
};
// at first print hashes 2 times. then hashes will be increased by 1 from previous.
for (hashes = rows + 1; hashes > 0; hashes--) {
printf("#");
};
printf("\n");
}
and get output:
##
###
####
#####
######
Related
This question already has answers here:
How do I check out a remote Git branch?
(42 answers)
Closed 1 year ago.
I just started with C programming and have some difficulty implementing a program which is giving a staircase with 'Height' amount of steps.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do
{
height = get_int("Height: ");
}
while(height > 8 || height == 0 || height < 0);
int width = 0;
int length = height;
while(width < height)
{
printf(" ");
printf("#");
for(width = 0; width < height; width++)
{
printf("\n");
}
}
}
The first lines with the Height are working, but I have difficulties with actually writing a staircase. I wanted something like this or similar to this.
Height: 3
#
#
#
I just want to learn how to implement something like this if I face a problem like this in the future. If somebody could help me further I would really appreciate it!
This works:
#include <stdio.h>
int main() {
// gets height input - replace with your get_int method
int height;
printf("Height: ");
scanf("%i",&height);
// loop over all the steps: 0 - height
for (int i = 0; i < height; i++) {
// add a space i number of times (where i is our current step number and so equal to width)
// notice that if we take top left as (0,0), we go 1 down and 1 right each time = current step
for (int j = 0; j < i; j++) {
printf(" ");
}
// finally, after the spaces add the character and newline
printf("#\n");
}
return 0;
}
I see three issues here:
You're printing newlines (\n) instead of spaces ( ).
Why print the single space character?
You're printing the "#" before (what should be) the spaces.
Print a newline after the spaces and the #.
Also... the staircase's width is always equal to its height; it's just the line you're printing that's advancing... that's a bit confusing.
#include <stdio.h>
int main(void)
{
int height = 5;
for(int i=0; i<height; printf("%*s\n", ++i, "#"));
}
Output:
Success #stdin #stdout 0s 5572KB
#
#
#
#
#
I am currently doing the Mario problem (less comfortable) for Week 1 of CS50. So far, I can print the hashes of the pyramid, but I am having trouble right-justifying it with the spaces. Would someone be able to pinpoint where I am going wrong? Thank you so much in advance.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int hashes;
int space;
int height;
do
{
height = get_int("height: ");
}
while (height < 0 || height > 5);
{
for (int i = 0; i <= height; i++)
{
for (space = (height - 1); space <= 0; space++)
{
printf(" ");
}
for (hashes = 1; hashes <= i; hashes++)
{
printf("#");
}
printf("\n");
}
}
}
As Antti said, in your 'spaces' loop, height - 1 on the first iteration of i (so where i = 0), will never be < or = to 0 (unless height is set to 1).
How many spaces should you print before the # on the first line if your height is set to 5? Is it 4?
So if on the first line int Space = 0, height = 5, what loop code would achieve a result of 4? But consider on the next iteration of Space if int Space = 1, Height = 5 what loop code could achieve a result of 3?
for (space = 0; "this needs to to = 4" ; space++)
{
printf(" ");
}
for (space = 1; "this needs to to = 3" ; space++)
{
printf(" ");
}
The value of 'spaces' changes on every iteration, could you use this?
When I run this code, I do get the desired result of a right-aligned pyramid that varies by user-input height. However, when I run the CS50 check function, it tells me the following:
:( handles a height of 1 correctly
\ expected output, but not " ##\n"
:( handles a height of 2 correctly
\ expected output, but not " ##\n ###\n"
:( handles a height of 23 correctly
\ expected output, but not " ##\n ..."
:( rejects a height of 24, and then accepts a height of 2
\ expected output, but not " ##\n ###\n"
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
int row;
int space;
int hash;
// declare variables
do
{
printf("Height:\n");
height = GetInt();
}
while (height < 0 || height > 23);
// build pyaramid if acceptable height entered
for (row = 0; row < height; row++)
{
for (space = 0; space < (height - row); space++)
{
printf(" ");
}
for (hash = 0; hash < 2 + row; hash++)
{
printf("#");
}
printf("\n");
}
}
Are you sure you're getting the right output of spaces? I remember Mario.c is supposed to output a pyramid that's right aligned, but also has the bottom-left aligned with the terminal side. It may be hard to tell since it's empty space. I suggest testing it by printing a character in its place
For example
for (space = 0; space < (height - row); space++)
{
printf("b");
}
That should give you an idea of what you need to change.
This question already has answers here:
Making a Hash Pyramid
(2 answers)
Closed 6 years ago.
I'm going through the Harvard CS50 online course and one of the problems is to create a "mario style pyramid" using spaces and hashes. I've got the spaces solved but the hashes are giving me trouble. Here's the code:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
//get height between 1 and 23
int height;
do
{
printf("Please enter a height: ");
height = GetInt();
}
while (height < 1 || height > 23);
//build pyramid
for (int i = 0; i < height ; i++)
{
//add spaces
for (int space = height - 1 - i; space >= 0; space--)
printf(" ");
//add hashtags
for (int hash = 2 + i; hash <= height; hash++)
printf("#");
printf("\n");
}
}
When i run it in the terminal with a height of 5 i'm getting this:
####
###
##
#
<-- space here also
when i want this:
##
###
####
#####
######
Any feedback would be appreciated, thanks
Just try it with the following code:
int main(void)
{
int height;
printf("Please enter a height: ");
scanf("%d", &height);
//build pyramid
for (int i = height; i >= 1; i--)
{
//add spaces
for (int space = 1; space < i; space++)
printf(" ");
//add hashtags
for (int hash = height; hash >= i-1; hash--)
printf("#");
printf("\n");
}
}
when the value of height is 5, you get the desired output:
##
###
####
#####
######
See the Working Fiddle.
In your code, when the value of i is 0 in:
for (int i = 0; i < height ; i++)
^^^^^^
the other loops executes as follows:
for (int space = height - 1 - i; space >= 0; space--)
printf(" ");
here, the loop initializes space = 4 (when height is 5) and the loop condition is valid till space >= 0, so it prints the first 4 characters as " ".
And, Finally when it comes to this loop:
for (int hash = 2 + i; hash <= height; hash++)
printf("#");
here, the loop initializes hash = 2 (i was 0 in the first loop, remember that?) and the loop conditions continues till hash <= height. So, it prints the next 4 characters as "#" as the above condition evaluates to 2,3,4,5 in:
(int hash = 2; hash <= 5; hash++)
^^^ ^^^
and the rest of the code carries on and produces the output as:
####
###
##
#
If you are able to understand the above logic, then you'd be able to decode my solution as well :)
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! :)
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("\n");
for (i = 0; i < height; i++) {
for (j = 0; j < height - i - 1; j++)
printf(" ");
for (j = 0; j < i + 2; j++)
printf("#");
printf("\n");
}
}
For more clarification on whats going on, and why each loop is necessary:
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
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
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
Here is a version that may offer some insight:
#include <stdio.h>
#include <cs50.h>
int main(void) {
//initialize variables
int height, n, j, k, i;
printf("Height: \n");
// Get user input
height = GetInt();
n = height;
for(i = 0; i < height; i++) {
// create n spaces based off height
for(k = n; k > i; k--)
printf("%c", ' ');
// create hash tags
for(j = 0; j < i+2; j++)
printf("#");
printf("\n");
}
return 0;
}
Result if user entered a height of 5:
Height:
##
###
####
#####
######
The 1st for loop essentially prints the number of rows matching the height entered
The 2nd for loop involves printing the number of spaces based on the height entered
The 3rd for loop involves printing the number of hashes (with respect to the height value) after the amount of spaces on the same line
Cheers