Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
has been doing pset1 "Mario Pyramid" and got stuck with the part about print "Hashes", already tried different ways but... nothing. In specific the problem is where it defines the value of "hs". https://docs.cs50.net/problems/mario/more/mario.html Here is about the problem pset1
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int h, s, hs, i;
do {
h = get_int("Height: ");
} while (h < 0 || h > 23);
for (i = 0; i < h; i++) {
/* ignore 1 */
if (i < 1) {
printf("");
} else {
for (s = (h - i); s > 1; s--) {
/* Spaces */
printf(" ");
}
for (hs = 2; hs < h; hs++) {
/* Hashes */
printf("#");
}
/* Jump Line */
printf("\n");
}
}
}
Output:
Height: 6
####
####
####
####
####
When its supposed to look like a half pyramid
Before you start coding, try to understand how to solve this problem with pencil
and paper.
Specially when you do a course on computer science, it's more important to
understand the underlying principle more than the programming itself. Very often
you will need to figure out the pattern. Once you've done that, translating it
into a program is much easier.
So let us try to figure out the pattern here.
This is the pyramid you want
# #
## ##
### ###
#### ####
What is the first thing that you can observe? We only have to look at the left
half of the pyramid, because the right half is the same only mirrored.
So the problem becomes easier:
# | #
## --> | ##
### | ###
#### |####
I added | to make it clear where each line begin. So for a pyramid of 4:
we need 4 lines
every line has the same length, 4 characters
the number of spaces decrease and the number of hashes increase from top to
bottom:
for the first line we need 4-1=3 spaces and 1 hash
for the second line we need 4-2=2 spaces and 2 hashes
for the third line we need 4-3=1 spaces and 3 hashes
for the fourth line we need 4-4=0 spaces and 4 hashes
Are you beginning to see the pattern here?
So in computing we start counting at 0. Beginners often make the mistake to
start counting by 1. So let's repeat these sentences from 0:
for the 0th line we need 4-1-0=3 spaces and 0+1=1 hashes
for the 1st line we need 4-1-1=2 spaces and 1+1=2 hashes
for the 2nd line we need 4-1-2=1 spaces and 2+1=3 hashes
for the 3rd line we need 4-1-3=0 spaces and 3+1=4 hashes
thus for a pyramid of size n:
for the 0th line we need n-1-0 spaces and 0+1=1 hashes
for the 1st line we need n-1-1 spaces and 1+1=2 hashes
· · ·
for the n-1th line we need n-1-(n-1) spaces and (n-1)+1 hashes
Do you see the pattern again?
for the i th line we need n - 1 - i spaces and i+1 hashes.
We have the pattern for the left half, the right half is the same, just
mirrored. So now that we know the numbers of spaces and hashes, writing a loop
is very easy, you almost only have to plug in the formulas:
#include <stdio.h>
int main(void)
{
int n = 10, i, j;
for(i = 0; i < n; ++i)
{
// left half
for(j = 0; j < n-1-i; ++j)
printf(" ");
for(j = 0; j < i + 1; ++j)
printf("#");
// 2 spaces in the middle
printf(" ");
// right half, we swapped the order
for(j = 0; j < i + 1; ++j)
printf("#");
for(j = 0; j < n-1-i; ++j)
printf(" ");
printf("\n");
}
return 0;
}
Note that the right half reverses the order of the printing hashes and spaces.
But trailing spaces at the right can be omitted entirely, so the last for loop
can be removed:
// this for loop before the printf("\n");
// can be removed, not needed.
for(j = 0; j < n-1-i; ++j)
printf(" ");
When solving these kind of problems, try using this method, pick a
pencil and paper and solve it by hand. Doing
this we've figured out the patter and translating it into code was a piece of
cake. Notice that we even saw that the problem could be "cut in half" because
of the symmetry. Now an exercise for you: print the same pyramid but upside down.
Related
I'm solving Mario More comfortable in CS50 Pset 1, I did like most of it, and this is my code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height, i2, i;
do
{
height = get_int("Height: ");
}
while(height < 1 || height > 8);
for(i = 0; i < height; i++)
{
printf("\n");
for(int o = 0; o < height - i - 1; o++)
{
printf(" ");
}
for(int j = 0; j <= i; j++)
{
printf("#");
}
printf(" ");
for(i2 = 0; i2 < height; i2++)
{
//printf("\n");
for(int j2 = 0; j2 <= i2; j2++)
{
printf("#");
}
}
}
printf("\n");
}
It draws the first pyramid well and puts two spaces, but instead of a pyramid, it draws something like a rectangle. I searched the internet for answers but all of them were just solving the whole thing and that spoils the learning process, so can you please give me some hints about this? I really appreciate any help you can provide.
If I am reading your question correctly, you want to produce a pyramid (or isosceles triangle) on the terminal like the following example (FYI, I have just one space).
Height: 8
# #
## ##
### ###
#### ####
##### #####
###### ######
####### #######
######## ########
If that is the case, I believe you complicated it a bit. In effect, you just want to repeat the pattern you have created in a symmetrical fashion. For that, you would only need to repeat the same print loop that you used to produce the left half of the pattern. If you need some more hints, let me know.
By the way, I believe that you can remove the "while" statement.
while(height < 1 || height > 8);
As it exists right now, if a value is entered that is less than one or greater than eight, that while loop will just become an endless loop.
Hope that helps.
Here is a copy of my code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height = get_int("Please enter a height between 1 and 8 (inclusive)\nHeight: ");
if(0 < height && height < 9)
for(int i = 0; i <= height; i++)
{
for(int s = (height - 1); s >= 1; s--)
{
printf("a");
}
for(int h = 1; h <= i; h++)
{
printf("#");
}
printf("\n");
}
else
printf("Please try again.\n"),
main();
}
The part I cannot seem to get right is printing the correct number of spaces as it does not seem to reduce the variable "s" after each loop. (I have replaced the spaces with the letter "a" so I can see where the code has gone wrong.)
The aim of the code is to print a pyramid of #'s that is right-aligned.
Where have I gone wrong here?
So for example, if the user inputs a height of 3, the output will be:
aa
aa#
aa##
aa###
however, I would like the output to be:
aa#
a##
###
The main issue is the 2nd for loop in your code when you print "a". Everytime you hit in this particular loop, you basically print (height - 1) times "a". In fact, you should print every time one less "a" (and one more hashes). To achieve this, we need to tweak condition of s.
Instead of this
int s = height - 1; s >= 1
Use this
int s = height - i; s > 1
Since your main loop has iterator i, we can use it here.
Second issue is that according to CS50 problem set description, you should actually print " " (spaces), not "a". For this, you should replace "a" with " ".
Third issue is your code prints an extra line with full of space in the begining. You need to tweak the main for loop with this condition to remove one extra line.
i < height
And in the for loop where you print # symbol, you need to start iterator h from zero, so that it prints right away with the first line.
int h = 0
The better solution is to use the built-in functionality of printf that is already in place, reducing the problem to a single, simple loop.
#include <stdio.h>
int main(void) {
int height = get_int("Please enter a height between 1 and 8 (inclusive)\nHeight: ");
unsigned char blocks[height];
memset(blocks, '#', height);
for(int i=0; i<height; ++i)
{
printf("%*.*s\n", height, i+1, blocks);
}
return 0;
}
Output
Success #stdin #stdout 0s 4212KB
#
##
###
####
#####
I'm currently going through CS50 through edx and doing problem set 1, Mario.
The objective is to create a print out using pound signs. With the help of some videos I got the code for the first one but I don't understand fundamentally how the math works/ what the computer is understanding.
So I figure if I don't learn I'm crippling myself later.
if n= 5
Then i has 1 added to it until it is not less than 5 which means 5 times yes?
Take a look at this line for the space loop >
for (int j = 0; j < n -1 - i; j++)
If n is 5, then it ends up being j(0) < 3...
So why on the first line are there four spaces and not three spaces?
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n;
do
{
n = get_int("Pyramid Height: ");
}
while (n < 0 || n >= 24);
//print out this many rows
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n -1 - i; j++)
{
printf(" ");
}
// print out this many columns
for (int j = 0; j < i + 2; j++)
{
printf("#");
}
printf("\n");
}
}
I get the correct pyramid yet i don't understand the logic behind the spacing and prints
if n=5 then n-1-j would be equal 5-1-0 i.e. 4 for the first time executing the loop that is the reason why you are seeing four spaces. The first loop condition should be n-2-j if you want the number of spaces to be three because total no of columns is 5 and the pounds expected in the first row are 2, therefore you should be subtracting 2 from n.
That looks way too complicated.
Here's a simple version I whipped up:
#include <stdio.h>
int main(void) {
int height = 5;
char blocks[height];
memset(blocks,'#',height);
for(int i=0; i<height; ++i)
{
printf("%*.*s\n", height, i+1, blocks );
}
return 0;
}
Output:
Success #stdin #stdout 0s 9424KB
#
##
###
####
#####
Let us try to figure out the pattern here. Like for the left pyramid if the height of the pyramid is 8, check the pattern of spaces and hashes from top to bottom. In this case we need 8 lines, every line has same characters and no of spaces decreases and no of hashes increases from top to bottom.
Now we have the pattern for the left half, the right half is the same, mirror image. So now we can write down the loop as we know the no of spaces and hashes from top to bottom. In programming we need to understand the underlying principle. Plug in the code afterwards becomes easy.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int h;
do
{
h = get_int("Pyramid height: ");
}
while (h<1 || h>8);
int n = 8, i, j;
for (i=0; i<h;++i)
{
// left half
for (j=0;j<h-1-i;++j)
printf(" ");
for (j=0;j<i+1;++j)
printf("#");
// two spaces in middle
printf(" ");
// right half, we have omitted the space code as it is not required.
for (j=0;j<i+1;++j)
printf("#");
printf("\n");
}
return 0;
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