How to make the hash pyramid to be on the right - c

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.
}
}

Related

CS50 mario pyramid upside down can't figure why

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");
}

CS50 Mario (More Comfortable) Error Messages

What is wrong with my code? I'm currently getting these messages (sad faces are the errors):
:) mario.c exists
:) mario.c compiles
:) rejects a height of -1
:( handles a height of 0 correctly
\ expected an exit code of 0, not output of " \n"
:( handles a height of 1 correctly
\ expected output, but not " \n# #\n"
:( handles a height of 2 correctly
\ expected output, but not " \n # #\n## ##\n"
:( handles a height of 23 correctly
\ expected output, but not " \n ..."
:) rejects a height of 24
:) rejects a non-numeric height of "foo"
:) rejects a non-numeric height of ""
The program seems to work fine other than being exactly the way CS50 is looking for.
#include <stdio.h>
#include <cs50.h>
int main(void) {
int height;
int i;
do {
printf("Height:");
height = get_int();
}while(height < 0 || height > 23);
int x = height;
int y = 0;
for (i = 0; i < height + 1; i++)
{
for (i = 0; i < x; i++)
{
printf("%s", " ");
}
for (i = 0; i < y; i++)
{
printf("#");
}
printf("%s", " ");
for (i = 0; i < y; i++)
{
printf("#");
}
printf("\n");
x = x - 1;
y = y + 1;
}
return 0;
}
Try including an if statement after the user inputs height. That will at least get you the proper result for a height of 0.
if (height == 0)
{
return 0;
}
Those /messages are key to fixing your code. You've got this.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int number;
do
{
number = get_int("Height:");
}
while (number < 1 || number > 8);
for (int i = 0; i < number; i++)
{
for (int j = number - i - 2; j >= 0; j--)
{
printf(" ");
}
for (int k = 0; k <= i; k++)
{
printf("#");
}
printf(" ");
for (int k = 0; k <= i; k++)
{
printf("#");
}
printf("\n");
}
}

Print out mario two half pyramid CS50

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("#");
}

how to create a diamond in c using only 3 printf and 3 n\t\

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;
}

Beginning C Program

I was working though some beginning problem sets with Harvard's online CS50 class. I got the problem to work correctly but I was wondering if there would possibly be a cleaner or better way to get the program to work.
The goal of the program is to print a right-aligned pyramid comprised of hash-tags and space characters. Any guidance in regards to style or tricks would be very welcome.
/* Creating the mario program, whose goal is to create a
* pyramid by accepting input from the user to get the
* height then aligning the pyrimid to the right.
*
*/
#include <stdio.h>
#include <cs50.h>
int main(void)
{
// get user input and set to variable
printf("Height: ");
int height = GetInt();
int i, j, k;
for(i = 1 ; i < height; i++)
{
// create n-1 spaces
for(k = (height - 2); k > (i-1); k--)
{
printf("%c", ' ');
}
// create n+1 hash tags
for(j = 0; j < (i+1); j++)
{
printf("#");
}
printf("\n");
}
return 0;
}
I'm assuming by cleaner you mean "spiffy and fancyer".
This looks spiffy to me:
#include <stdio.h>
#include <cs50.h>
int main(void) {
// get user input and set to variable
printf("Height: ");
int height = GetInt();
int hm2 = height - 2;
int j, k;
for(int i = 1 ; i < height; i++) {
// create n-1 spaces
for(k = hm2; k > (i-1); k--)
printf("%c", ' ');
// create n+1 hash tags
for(j = 0; j < (i+1); j++)
printf("#");
printf("\n");
}
return 0;
}
However, don't get too caught up in making your code fancy. Although it's nice if you're working with others, or yourself really. Your example looked fine.
Now, optimization-wise, that's something to worry about. Just remember that too much optimization can potentially break your program.
For everyone's consideration: this is what "all style and no readability" looks like :)
i = 0;
while (i++ < height*height)
printf ("%c%s", (i-1)/height < height-(i-1)%height-1 ? ' ' : '#',
i % height ? "" : "\n");
It is nigh on impossible to see what the code does without running it. If there is to be a follow-up exercise, this is hard to re-write to form, say, an even-sided pyramid. I'd probably throw this away and start again with the basics, before concatenating it again into a little monster such as this.
(later) Ever so slightly more neat to put the i++ at the end, so two times (i-1) gets traded for a slightly more complicated end-of-line test:
i = 0;
do
printf ("%c%s", i/height < height-i%height-1 ? ' ' : '#',
i % height==height-1 ? "\n" : "");
while (++i < height*height);
I think by cleaner and better way you mean to be a perfect shaped right angled triangle pyramid.
For this you should do as
Change
printf("Height: ");
to
printf("Height: \n\n");
and
for(i = 1 ; i < height; i++)
to
for(i = 0 ; i < height; i++)
And see the sample output.
Here's a suggestion:
#include <stdio.h>
#include <cs50.h>
int main(void) {
//initialize variables
int height, hm2, j, k, i;
printf("Height: \n");
// Get user input
height = GetInt();
hm2 = height - 1;
for(i = 0; i < height; i++) {
// create n spaces
for(k = hm2; k > i; k--)
printf("%c", ' ');
// create n+1 hash tags
for(j = 0; j < i+1; j++)
printf("#");
printf("\n");
}
return 0;
}
Result if the user entered a 5 for the height:
Height:
#
##
###
####
#####
A couple things I considered with this version of the code:
-In C, it's good practice to declare all variables separately from giving them a value and assign values later. Some compilers may bring up this error if you to declare and assign a value in a for loop: "error: β€˜for’ loop initial declarations are only allowed in C99 mode". These changes are considered with what I have provided.
//initialize variables
int height, hm2, j, k, i;
-I added a newline here
printf("Height: \n");
-Instead of hm2 = height - 2 I changed it to:
hm2 = height - 1;
-First loop, now we give i a value and set it to 0 to meet the other changes that were made:
for(i = 0; i < height; i++) {
-For the loop creating n spaces I changed it to:
for(k = hm2; k > i; k--)
-Finally removed parenthesis (no need in this case) in last for loop:
for(j = 0; j < i+1; j++)
Cheers

Resources