Help with C
-
I have been taking the free CS50x course from Harvard and just completed the Mario more comfortable problem. This is my code and I wanted to know how the right side of hashes are left aligned even though the code is the same for both left and right hashes.? To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between. // Prints bricks, sized as specified by user #include #include int main(void) { // Prompt user for a positive number to use as height of pyramid int h; do { h = get_int("Height: "); } while (h < 0 || h > 23); // Print out this many rows for (int i = 0; i < h; i++) { // Print out this many spaces for (int j = 0; j < h - i; j++) { printf(" "); } // Print left hashes for (int j = 0; j < i + 2; j++) { printf("#"); } // Print gap printf(" "); // Print right hashes for (int j = 0; j < i + 2; j++) { printf("#"); } printf("\n"); } }
-
I have been taking the free CS50x course from Harvard and just completed the Mario more comfortable problem. This is my code and I wanted to know how the right side of hashes are left aligned even though the code is the same for both left and right hashes.? To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between. // Prints bricks, sized as specified by user #include #include int main(void) { // Prompt user for a positive number to use as height of pyramid int h; do { h = get_int("Height: "); } while (h < 0 || h > 23); // Print out this many rows for (int i = 0; i < h; i++) { // Print out this many spaces for (int j = 0; j < h - i; j++) { printf(" "); } // Print left hashes for (int j = 0; j < i + 2; j++) { printf("#"); } // Print gap printf(" "); // Print right hashes for (int j = 0; j < i + 2; j++) { printf("#"); } printf("\n"); } }
Faith Burnett wrote:
To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between.
Those two
for()
loops are printing the same NUMBER of hashes from where thestdout
stream happens to be at the point thefor()
loop starts printing. Since thestdout
stream is at "column" 0 before the firstfor()
loop, the first set of hashes is right justified (preceded with spaces), and since thestdout
stream is at "column"H+2
after the first two innerfor()
loops run, the second set of hashes always starts in the same "column." You might want to consider changing yourfor()
loop boundaries so that the pyramid has a "pointed" top at level 1, and no leading/trailing spaces at levelh
:for (int i = 0; i < h; i++)
{
...
// Print out this many spaces
for (int j = 0; j < h - i - 1; j++)
...
// Print left hashes
for (int j = 0; j < i + 1; j++)
...
// Print right hashes
for (int j = 0; j < i + 1; j++)
...
}"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Faith Burnett wrote:
To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between.
Those two
for()
loops are printing the same NUMBER of hashes from where thestdout
stream happens to be at the point thefor()
loop starts printing. Since thestdout
stream is at "column" 0 before the firstfor()
loop, the first set of hashes is right justified (preceded with spaces), and since thestdout
stream is at "column"H+2
after the first two innerfor()
loops run, the second set of hashes always starts in the same "column." You might want to consider changing yourfor()
loop boundaries so that the pyramid has a "pointed" top at level 1, and no leading/trailing spaces at levelh
:for (int i = 0; i < h; i++)
{
...
// Print out this many spaces
for (int j = 0; j < h - i - 1; j++)
...
// Print left hashes
for (int j = 0; j < i + 1; j++)
...
// Print right hashes
for (int j = 0; j < i + 1; j++)
...
}"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Thanks for the help David!