CS50 Mario I'm stuck doing that pyramid [closed] - c

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 6 years ago.
Improve this question
Well, I've like a week reading all kind of material regarding to C programming to make this pyramid works and finally I decided to ask for help, I've improve a lot, my code now takes the input (0 to 23) correctly and print a new line, but now that I'm trying to add the # into the code, the code make an infinite loop :(.
#include <stdio.h>
#include <cs50.h>
/* PSET1 Mario */
int main (void)
{
int height;
do
{
printf("Give me an int between 0 or 23 \n");
height = GetInt();
}while((height < 0)||(height > 23));
//The Pyramid output
int row, space;
space = height - 1;
for (row = 0 ; row <= height; row++)
{
for (space = 0; space < row; space--)
{
printf("#");
}
return 0;
}
The main point it is do a pyramid with ''#'' and I edited the post this was my previous code with the #, it was doing a infinite loop.

Is this what you want?
#include <stdio.h>
#include <cs50.h>
/* PSET1 Mario */
int main (void)
{
int height;
do
{
printf("Give me an int between 0 or 23 \n");
height = GetInt();
}while((height < 0)||(height > 23));
//The Pyramid output
int row, space;
space = height - 1;
for (row = 0 ; row <= height; row++)
{
int simbol=0;
for (space = height-row; space > 0; space--)
{
printf(" ");
}
for (simbol = 0; simbol < row; simbol++)
{
printf("##");
}
printf("\n");
}
return 0;
}
This give you this pyramid:
##
####
######
########
##########
############
##############
################
##################
####################
######################
########################

Related

CS50 Pset 1 Mario More

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.

Implementing a staircase within a C program [duplicate]

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
#
#
#
#
#

In C - How would I use scanf to store a random amount of ints to a 2d array [closed]

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 3 years ago.
Improve this question
I was wondering how you would do the following:
I provide the width and height of a grid as command line arguments, and I now need to read the grid from standard input and store it into a 2d array. if the command line arguments are just standard integers and the grid is in the following format:
1 2 3 4 5 6 ... x
1 2 3 4 5 6 ... x
1 2 3 4 5 6 ... x
. . . . . . ... x
y y y y y y y y
how would I read this? I'm getting really confused because I don't have a static number of rows and columns since the values of each can change based off user input.
If your question is about creating your grid using command line arguments, indeed, as JohnG said, arguments are static, so your width and height are static and you can simply create an array.
Say your program is run by using the format ./program heigth width, you just have to use:
int tab[atoi(argv[1])][atoi(argv[2])];
However, if you want to dynamically create a two-dimensional array (meaning with a width and heigth you don't know beforehand), here is an example, using malloc and pointers, which are two very importants parts of the C language.
Note that this isn't exactly a two-dimensional array, but a 1D array of pointers and several 1D arrays of int.
#include <stdio.h>
#include <stdlib.h>
int main () {
printf("Height: ");
int height = 0;
scanf("%d", &height);
printf("Width: ");
int width = 0;
scanf("%d", &width);
int** tab = malloc(height*sizeof(int*)); // Take the space for height * pointers of integer that points towards the first values of each line
for (int i = 0; i < height; i++) {
tab[i] = malloc(width*sizeof(int)); // take the space for width * integers, where we will stock our values
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
tab[i][j] = j + 1; // assign the value 1, 2, 3... to each line
}
}
// Print array:
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
printf("%d ",tab[i][j]);
}
printf("\n");
}
// Free memory allocated for each malloc
for (int i = 0; i < height; i++) {
free(tab[i]);
}
free(tab);
return 0;
}

Cs50 Mario: How can I improve this code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
It took me all day to figure this out but even though I completed it I don't fully understand how it works and I feel there is a better or cleaner way of writing it.
Can some one explain how I can improve my code?
#include<cs50.h>
#include<stdio.h>
int main(void)
{
int rows, height, spaces, hashes;
do
{
printf("Height: ");
height = get_int();
}
while(height < 0 || height > 23);
for(rows = 0 ; rows < height; rows++)
{
for(spaces = height - 1; spaces > rows; spaces--)
{
printf(" ");
}
for(hashes = 0; hashes < spaces + 2; hashes++)
{
printf("#");
}
printf("\n");
}
return 0;
}
It is best if you first try to understand what the code is doing. Work it through on paper, starting with the input 1.
If you are keen you might like to explore the printf width and precision formatting, where each * in the format specifier is replaced by the corresponding function argument. But beware - printf is a complicated and detailed function. This also introduces the array.
char hatch[] = "##############################";
for(rows = 0; rows < height; rows++) {
printf("%*.*s\n", height + 1, rows + 2, hatch);
}
For the input of 1 your program prints
##
For the input of 5 it prints
##
###
####
#####
######
and so does this. The loop contains a single instruction.

Creating a "Mario Style Pyramid" [duplicate]

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 :)

Resources