Having trouble right-aligning pyramid for Mario problem set - c

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?

Related

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

What is the math behind For loops in this CS50 problem Set with C

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;

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

Where did I go wrong in my loops in my code?

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

Finding square made of 1's in a 2D array/matrix

Thanks for helping. I really appreciate it. I Have been searching for solution on SO, but nothing is exactly what I need. I need it in C.
My task is to find "largest square" of 1's in an array. The array consists of only 0's and 1's and looks, for example, like this:
4 4
0 1 1 1
0 1 0 1
0 1 1 1
1 0 1 0
Output should print [row][col] of "upper left corner" 1, and [row][col] of "lower right corner", So it should be, for my example, [0][1] and [2][3].
I am using my getcolor() function to get value on [row][col] spot.
Also, I have these functions to get longest horizontal and vertical lines. For some reason they only work with arrays with the same number of columns and rows. When I use, for example, an array with 4 cols and 5 rows, it does not work right. Aan you help me please?
void getHline(Bitmap *arr)
{
int i, j, k;
int line, line_start, line_end, line_max = 0;
// Horizontally
for (k = 0; k < arr->rows; k++)
{
for (i = 0; i < arr->rows; i++)
{
if(!getcolor(arr, k, i))
{
continue;
}
for(j = i; j < arr->cols; j++)
{
if(!getcolor(arr, k, j))
{
break;
}
}
j--;
if(j - i + 1 > line_max)
{
line = k;
line_start = i;
line_end = j;
line_max = line_end-line_start+1;
}
}
}
printf("horizontally\n");
printf("start: [%d][%d]\n", line_start, line);
printf("end: [%d][%d]\n", line_end, line);
}
void getVline(Bitmap *arr)
{
int i, j, k;
int col, col_start, col_end, col_max = 0;
for(k = 0; k < arr->cols; k++)
{
for (i = 0; i < arr->rows; i++)
{
if (!getcolor(arr,i,k)) continue;
for (j = i; j <arr->cols; j++)
{
if (!getcolor(arr,j,k)) break;
}
j--;
if (j - i + 1 >col_max)
{
col = k;
col_start = i;
col_end = j;
col_max = col_end-col_start+1;
}
}
}
printf("\nverticaly\n");
printf("start: [%d][%d]\n", col, col_start);
printf("end: [%d][%d]\n", col, col_end);
}
If you're trying to get the largest square this has noting to do with the longest horizontal and vertical lines, because they could be separated and no square associated with them.
When trying to solve a complex problem, don't try to solve it all at once.
The first thing we have, is that each point of the array is associated with a square (the largest one for each point). So we have to find that square: We take a point of the array, then we move by steps through a continuous horizontal and vertical lines. For each step we check if we get a square and repeat the process until we get the largest square associated with that single point.
Each time we get the largest square associated with a point we check if it's largest than the last largest square associated with some previous point.
After connecting these parts we get our final program.
Explanation of the variables used in the program:
Link to the program http://pastebin.com/Yw05Gbtg or view it here:
EDIT:
#include <stdio.h>
main()
{
int lines=4, cols=4;
int arr[4][4] = {
{0,1,1,1,},
{0,1,0,1,},
{0,1,1,1,},
{1,0,1,0,}
};
int x_start, y_start, x_end, y_end, d_max=0;
int i, j, k, l;
int col_start, line_start, col_end, line_end, checker;
for (y_start=0; y_start<lines; y_start++){
for (x_start=0; x_start<cols; x_start++){
x_end = x_start;
y_end = y_start;
for (i=x_start, j=y_start; i<cols && j<lines; i++, j++){ // moving horizontally and vertically
if (!arr[y_start][i] || !arr[j][x_start]){ // checking if the horizontal or vertical lines are not continuous
break;
}
else {
checker = 1;
for (k=x_start, l=y_start; k<i+1 && l<j+1; k++, l++){ // check if square
if (!arr[j][k] || !arr[l][i]){
checker = 0;
break;
}
}
if (checker){ // if square then
x_end = i;
y_end = j;
}
}
}
if ((x_end-x_start)>d_max){
col_start = x_start;
line_start = y_start;
col_end = x_end;
line_end = y_end;
d_max = col_end-col_start;
}
}
}
printf("The largest square is:\n[%d][%d] x [%d][%d]\n", line_start, col_start, line_end, col_end);
// this is only to check if the program is working properly
for (y_start=line_start; y_start<line_end+1; y_start++){
printf("\n ");
for (x_start=col_start; x_start<col_end+1; x_start++){
printf("%d ", arr[y_start][x_start]);
}
}
printf("\n");
}
There is confusion between rows and cols somewhere in your code. To find it, rename your variables i, j and k to something more meaningful, like row, col_start and col_end.
As for finding maximal square, you might want to use prefix sums.

Resources