Hey I'm working on the CS50 more comfortable problem and I can't figure out how to print the second mario pyramid on the same line. In my code it already prints out, but it's not on the same line.
It doesn't matter if you guide me or show me how to do it. I'm using CS50 as a practice, I'm not turning anything in. So this wouldn't be cheating.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int height = 0;
// left pyramid variables
int i = 0;
int j = 0;
int k = 0;
// variable for gap
int g = 0;
// right pyramid variables
int l = 0;
int m = 0;
int n = 0;
//do - while loop -- works
do
{
printf("Height: ");
scanf("%d", &height);
}
while (height < 0 || height > 23);
// Print pyramids
// print spaces for left pyramid (less spaces needed with time) ✓
// print hashes for left pyramid ✓
// print gap (2)
// print hashes for right pyramid
// print new line - for next row
// Left Pyramid
// Rows -- determines the height
for (i = 0; i < height; i++)
{
// Cols -- in this one we are doing the spaces
// the -i makes it left aligned -- to make it right aligned remove the "-1"
for (j = 0; j < height-i; j++)
{
// Printing Spaces
printf(" ");
}
// "i+1" - we want i to be 1 whenever height is 0, and we want i to increase by one
// whenever the height increases, so that's why we add + 1 to it
// if I don't add 1 to it what it does is that prints a new line, and then it prints
// 4 things instead of 5 for example.
for (k = 0; k < i + 1; k++)
{
printf("#");
}
// Print new line
printf("\n");
}
// Gap -- fix gap, the rest works how it should -- I think I need to make everything
// inside one loop
// for (g = 0; g < height; g++)
// {
// printf(" ");
// }
// Right Pyramid
// Rows -- determines the height
for (l = 0; l < height; l++)
{
// Cols -- in this one we are doing the spaces
// right aligned
for (m = 0; m < height; m++)
{
// Printing Spaces
printf(" ");
}
// "i+1" - we want i to be 1 whenever height is 0, and we want i to increase by one
// whenever the height increases, so that's why we add + 1 to it
// if I don't add 1 to it what it does is that prints a new line, and then it prints
// 4 things instead of 5 for example.
for (n = 0; n < l + 1; n++)
{
printf("#");
}
// Print new line
printf("\n");
}
return 0;
}
Why not just do something like into the first loop:
for (k = 0; k < i + 1; k++)
{
printf("#");
}
/* this is new */
/* Draw the gap */
for (k = 0; k < gap; k++) {
printf(" ");
}
/* Draw the left part*/
for (k = 0; k < i + 1; k++)
{
printf("#");
}
Related
in the cs50 project, we have to make a Mario hash pyramid then right align it to the left and I'm having a pretty hard time doing it and I hope someone could give me some advice
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int h = 0;
int l = 0;
int c = 0;
do {
h = get_int("Height: ");
} while (h > 8 || h < 1);
{
for (int w = 0; w < h; w++)
{
for (int j = -1; j < w; j++)
{
printf("#");
}
printf("\n");
}
}
}
output:
what is needed:
try to make two-loop one for the empty space you can also use - and second for #..
after the align become to left del the -
Before checking the code that I'll provide, I strongly advise you to do hard coding for every single line of pyramide. This is the only way you will see and understand the design pattern of the following algorithm. Then, feel free to check the following code snippet.
Here is the code that builds pyramid right-aligned.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height = 0;
do
{
height = get_int("Please enter a height between 1 and 8 (inclusive)\nHeight: ");
}
while (height > 8 || height < 1);
for(int i = 0; i < height; i++)
{
for(int j = height - i; j > 1; j--)
{
printf(" "); // print empty spaces.
}
for(int k = 0; k <= i; k++)
{
printf("#"); // print hashes.
}
printf("\n"); // go to next line.
}
}
I am working on this problem from the CS50 class. I am still a beginner. What I need to program is this:
Toward the end of World 1-1 in Nintendo’s Super Mario Brothers, Mario
must ascend right-aligned pyramid of blocks, a la the below.
screenshot of Mario jumping up a right-aligned pyramid
Let’s recreate that pyramid in C, albeit in text, using hashes (#) for
bricks, a la the below. Each hash is a bit taller than it is wide, so
the pyramid itself is also be taller than it is wide.
#
##
###
####
#####
######
#######
########
The program we’ll write will be called mario. And let’s allow the user
to decide just how tall the pyramid should be by first prompting them
for a positive integer between, say, 1 and 8, inclusive.
However I have tried many ways, two of which are these:
code mariov1
After looking at some Stack Overflow attempts, it now looks like this:
#include <cs50.h>
#include <stdio.h>
string hash(int);
int main(void)
{
int n;
do
{
n = get_int("Height: ");
}
while (n < 0 || n > 8);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-1-i; j++)
{
for(int j = 0; j < i+1; j++)
{
printf(".");
}
printf("#");
}
printf("\n");
}
}
What can I try next?
Suriyu, to add to what Weather Vane said. To pass it through Check50, you'll still need to make small tweaks to the code so that it passes through all CS50 tests.
For the do-while loop, n <=0 instead of n < 0 to ask for an input when n = 0, because the specification demands a minimum of one brick (1 to 8 both inclusive).
You need only the two loops, don't print extra characters not specified in the problem set, ex: printf(".");
All the best with CS50, it's going to be a fun experience!
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n;
do
{
n = get_int("Height: ");
}
while (n < 1 || n > 8);
// this for loop makes new lines
for (int i = 0; i < n; i++)
{
// here I have two for loops nested inside the above for loop,
// I previously made the mistake of having two inner loops nested.
// this 2nd for loop prints n-1-i spaces
// because if n=5, then in the 4th row, there will be 5-1-3 spaces/dots
for (int j = 0; j < n - 1 - i; j++)
{
printf(" ");
}
// this 3rd for loop prints i+1 hashes
// because if n=5, then in the 4th row, there will be 3+1 hashes.
// (3 because you count from 0)
for (int j = 0; j < i + 1; j++)
{
printf("#");
}
printf("\n");
}
}
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n;
do
{
n = get_int("Height of the pyramid is:\n");
}
while (n < 1 || n > 8); //condition to get a number from 1-8 from the user
for (int i = 0; i < n; i++) //loop for height
{
for (int j = 0; j < n - 1 - i; j++) //loop for spaces on left pyramid
{
printf(" ");
}
for (int k = 0; k < i + 1; k++) // loop for hashes on left pyramid
{
printf("#");
}
printf(" "); // spacing between pyramids
for (int p = 0; p <= i; p++) //loop for right pyramid
{
printf("#");
}
printf("\n");
}
}
This is the advanced version of the problem if you decide to try it.
Here's a different approach. Instead of iteratively printing blanks, followed by iteratively printing number signs, this version creates a buffer (size defined by a precompiler constant - currently set to 8, change it if you want to allow bigger pyramids), then for each row in the pyramid it first fills the buffer with number signs, then overlays the beginning of the line with the proper number of spaces, and then prints it:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#define MAXSIZE 8
int main(void)
{
int size, spaces;
char buf[MAXSIZE+1];
do
size = get_int("Height: ");
while (size < 0 || size > MAXSIZE);
buf[size] = '\0';
for(spaces = size-1 ; spaces >= 0 ; --spaces)
printf("%s\n", (char *)memset(memset(buf, '#', size), ' ', spaces));
}
EDIT
And here's yet another approach which builds the entire output block in an array in memory and then prints it using a single call to puts:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#define MAXSIZE 8
#define TOTSIZE ((MAXSIZE+1) * MAXSIZE)
int main(void)
{
int size, spaces;
char buf[TOTSIZE+1];
do
size = get_int("Height: ");
while (size < 0 || size > MAXSIZE);
memset(buf, '\n', (size+1)*size);
buf[((size+1)*size)] = '\0';
for(char *p = buf, spaces = size-1 ; *p != '\0' ; p += size+1, --spaces)
memset(memset(p, '#', size), ' ', spaces);
puts(buf);
}
This is an option that likely works best:
from cs50 import get_int
while True:
n=get_int("Enter Height: ")
if n>=1 and n<=8:
break
for i in range(0, n-1):
print(" " * (n - (i+1)) + "#" * (i+1))
I thought long and hard before asking this in here but I've spent too much time now trying to figure this one out without cheating.
The CS50 mario ps1 (less comfortable) asks for a *simple left align (at first) pyramid, but my code is giving me it upside down and I can't figure why.
#include <cs50.h>
#include <stdio.h>
int main (void)
{
int n;
do
{
n = get_int("Pyramid Height: ");
}
while (n < 1 || n > 8);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i ; j++)
printf("#");
for (int j = 0; j < n - i; j++)
{
printf(" ");
}
printf("\n");
}
}
I'm sorry if this type of questioning shows up regularly here but I really do need your help.
Thanks in advance.
edit:
expected result:
........#
.......##
......###
.....####
....#####
...######
..#######
.########
I can change the dots to spaces afterwards, this is just for visualisation;
the restriction for height is 8, so I guess that each line has always eight characters;
I actually added trailing spaces so that the pyramid could be right aligned, I've metioned wrong before;
I'm going to check the How to debug small programs?;
Sorry, I'm new to this, I didn't know there was a difference between here and stack exchange, gonna look into that.
*Sorry for the "meh" english, it is not my native language.
See what is the difference between my and your code (especially how to count):
void draw(int n, int align, int dir)
{
for (int i = 1; i <= n; i++)
{
if(align)
{
for(int s = 0; s < (dir ? (n - i) : i - 1); s++)
{
printf(" ");
}
}
for (int j = 0; j < (dir ? i : (n - i + 1)) ; j++)
{
printf("#");
}
printf("\n");
}
}
int main (void)
{
draw(8,1,0);
printf("\n");
draw(8,1,1);
printf("\n");
draw(8,0,0);
printf("\n");
draw(8,0,1);
}
https://godbolt.org/z/7YT16j
my code is giving me it upside down and I can't figure why
Let's see what the code looks like
// There's a loop executed n times. The body prints a line, so n lines are printed.
// In case you have doubts, the characters are normally printed top to bottom and
// left to right.
for (int i = 0; i < n; i++)
{
// The following loop prints (n - i) characters '#' at the beginning
// of each line. That's NOT what you are supposed to do, kind the opposite.
for (int j = 0; j < n - i ; j++)
printf("#");
// You should first print the spaces, then the '#'s, starting from 1 '#' at
// the first line and increasing the number by one at each line (so you have
// to change the condition in the loop accordingly).
// This loop prints the right amount of spaces, but only after
// all the '#'s and just before the end of the line, so that you just
// can't see them (change the printed char to '.' to visualize those).
for (int j = 0; j < n - i; j++)
{
printf(" ");
}
// Note that you could use putchar('\n'), here and previously, to print
// only one char, instead of using printf() to print string literals.
printf("\n");
}
This is my current code
void printer(int input)
{
int star;
int space;
for (int i = 1; i <= input; i++)
{
star = i;
space = i + input - star - 1;
for (int j = 0; j < space; j++)
{
printf(" ");
}
for (int s = 0; s < star; s++)
{
printf("*");
}
printf("\n");
}
}
I'm trying to print it out like
How tall 3?
*
* *
* * *
_| |_
\___/
and this is what i get. What is wrong with my code? and how will I fix the code?
How Tall? 3
*
**
***
Essentially, this snippet of code here that you use:
star = i;
space = i + input - star - 1; // or, space = input - 1 + i - star;
is the same as writing:
star = i;
space = input - 1;
This is because i and star have the same value (quoth star = i), and therefore, negate each other.
Now, can you see a constant here? Yes, the value of input is never changed anywhere, and therefore your code is always what 1 less than the input is. (In this case, its always preceded by 2 (=3-1) spaces) like this:
*
**
***
^^ mark two spaces
And you also forgot to add a trailing space after the asterix and hence there is no spacing between them.
Therefore, to solve your problem, you could store the value of input temporarily and reduce it by 1 in every iteration so it appears like a pyramid.
Example:
void printer(int input)
{
int star;
int space;
// Store the original length of the space
int space_length = input;
for (int i = 1; i <= input; i++)
{
star = i;
// Get the number of spaces for the current iteration
space = space_length - 1;
for (int j = 0; j < space; j++) {
printf(" ");
}
for (int s = 0; s < star; s++) {
printf("* ");
// ^ note this space after the asterisk
}
// Decrease the length of the space every step
// So that it appears like a slope
// Note how we are using 'space_length' instead of input
// This is because if we decrement 'input', this loop
// will get affected, which is not what we want
space_length--;
printf("\n");
}
///////// bottom part of the tree /////////
// number of spaces needed = input - length of "_| |_" - 1
for (int i = 1; i <= input - (4 - 1); i++)
printf(" ");
printf("_| |_\n");
// number of spaces needed = input - length of "\\___/" - 1
for (int i = 1; i <= input - (4 - 1); i++)
printf(" ");
printf("\\___/\n");
}
which gives the output:
*
* *
* * *
_| |_
\___/
your format is the issue. as you already know the size of the tree, you can deduct the size of the first set of whitespace (the first spaces on each lines), being spaces = (size - 1) - i. it should be, for each i:
i1 = 2
i2 = 1
i3 = 0
which seems to be what the result gives. additionally, between all asterix, just insert one space until the last character, and that's the idea
There are too many things gone wrong with this. I'll not post the correct code, but can guide you through it.
Since you want space after stars, print "* " instead of "*"
You have to use decremental space in beginning, before printing star, there's some logical fault in that, which is, you are adding and subtracting i and star, which are same thing. You need to give (input - star) spaces for the loop.
For the trunk, you have to develop some symmetrical logic.
I hope it helps you.
You can do it like this:
#include <stdio.h>
void printer(int input)
{
// for stars
for (int i = 1; i <= input; i++) {
for (int j = 1; j <= input - i; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
if (k == 1) printf("*");
else printf(" *");
}
printf("\n");
}
// for base
int base_half_len = input - 1;
for (int i = 1; i < base_half_len; i++) {
printf("_");
}
printf("| |");
for (int i = 1; i < base_half_len; i++) {
printf("_");
}
printf("\n\\");
for (int i = 1; i < 2 * input - 2; i++) {
printf("_");
}
printf("/\n");
}
int main(void) {
// your code goes here
printer(10);
return 0;
}
Here's the link where I tested it: https://ideone.com/7zpNeW
I am attempting to create a diamond in c with the constraints of only 3 printfs and 3 n\t. this requires me to use loops. I know how to make an upside down triangle and a triangle but cant use that because there are too many printfs. i will attach my code so far. I am aware it does not make a diamond, and some awfully strange shape, but that it what i'm trying to work off and edit to make into a diamond, I just haven't been able to figure it out.
if (type_of_shape == 5)
{
for (i = 0; i < width; i++)
{
for (j = 0;j < ((width - 1) / 2) - i ||(width -1)/2 < i && j + (width-1)/2 < i; j++)
{
printf(" ");
}
for (k = 0;k<width && k < (j*2+1) ; k++)
{
printf("*");
}
printf("\n");
}
}
//int width = 5;
int row, col;
int spaces, stars;
int half, rate;
half = width / 2 + 1;
rate = 1;
for(row = 1; 0 < row && row <= half; row += rate) {
spaces = half - row;
stars = row * 2 -1;
printf("%*s", spaces, "");
for (col = 0; col < stars; col++)
printf("*");
printf("\n");
if(row == half)
rate = -rate;
}
I got it down to a single line which has a single loop, with a single printf statement.
It involved some tricky use of abs.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int w = 9;
for(int l=0; l < w; ++l) printf("%*.*s\n", abs(w/2 - l)+abs((2*l+1)-(2*l+1>w)*2*w), abs((2*l+1)-(2*l+1>w)*2*w), "**************");
return 0;
}
2 loops (one for, one while).
2 printf statements.
Note:
This works with odd Widths.
An even width produces a diamond with Width+1
My IDEOne code
int main(void)
{
int width = 9;
int layer;
width+=2;
for(layer=0; layer<width/2; ++layer)
{
printf("%*.*s\n", width/2+layer + 1,layer*2 + 1, "**************************");
}
layer--;
while (layer --> 0)
{
printf("%*.*s\n", width/2+layer + 1,layer*2 + 1, "**************************");
}
return 0;
}
Output
Success time: 0 memory: 2168 signal:0
*
***
*****
*******
*********
*******
*****
***
*
Here's a solution with no loops at all. (looping accomplished via recursion), and 3 printf statements:
#include <stdio.h>
void drawDiamond(int width, int stars)
{
static const char* txt = "*****************************";
if (stars == width) {
printf("%*.*s\n",width, width, txt);
return;
}
printf("%*.*s\n", (width+stars)/2, stars, txt);
drawDiamond(width, stars+2);
printf("%*.*s\n", (width+stars)/2, stars, txt);
}
int main(void)
{
drawDiamond(9, 1);
return 0;
}