'How to make the pyramid (CS50 Mario Program) formed by this code to be right aligned?
Please help me create the pyramid with height "n" print correctly using hashes and spaces that is right-aligned. I have posted the code itself below. The program correctly asks for the user input, but doesn't build the pyramid right-aligned. If anyone can fix this, please help.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int i=0;
int n=0;
do
{
printf("Height:");
n=GetInt();
} while (n<0 || n>23);
for (i=0; i<n; i++)
{
printf(" ");
for (int x=0; x<i+2; x++)
{
printf("#");
}
printf("\n");
}
return 0;
}
Solution 1:[1]
Here's one of the correct solutions!
#include <stdio.h>
#include <cs50.h>
void buildPyramid(int height);
int main(void)
{
// Initialize the variable height
int height;
// Run the loop to get a value of height between 1 and 8, inclusive, from the user
do
{
height = get_int("Height: ");
}
while (height < 1 || height > 8);
// Call the function and pass height to it as a parameter
buildPyramid(height);
}
// Declare the function buildPyramid
void buildPyramid(int height)
{
// Loop to add a new line
for (int i = 0; i < height; i++)
{
// Loop to add spaces
for (int k = height - i; k > 1; k--)
{
printf(" ");
}
// Loop to add hashes
for (int j = 0; j <= i; j++)
{
printf("#");
}
printf("\n");
}
}
Solution 2:[2]
#include<cs50.h>
#include<stdio.h>
#include<stdio.h>
void half_pyramid(int n);
int main(void){
int height;
do{
height = get_int("Height: ");
}while(height<=0||height>23);
half_pyramid(height);
}
void half_pyramid(int n)
{
int spaces;
int dashes;
for(int i = 2; i<=n+1;i++){
for(spaces = (n-i); spaces>=0;spaces--){
printf(" ");
}
for (dashes = 1; dashes <= i; dashes++){
printf("#");
}
printf("\n");
}
}
Solution 3:[3]
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
int spaces;
int hash;
do
{
height = get_int("Height: ");
}
while (height < 0 || height > 23);
for (int i = 0; i < height; i++)
{
// For loop to print out the spaces
for (spaces = (height - i); spaces >= 2; spaces--)
{
printf(" ");
}
// For loop to print out hashes
for (hash = 0; hash <= (i + 1); hash++)
{
printf("#");
}
printf("\n");
}
}
Solution 4:[4]
int main(void){
int height;
int spaces;
int dashes;
do
{
printf("Height:");
height = GetInt();
}
while (height <= 0 || height >= 23);
//loop for the rows
for (int i = 1; i <= height; i++){ //fixed the <= part of the operator
//loop for the spaces and dashes per row
for (spaces = (height - i); spaces >= 0; spaces--){
printf(" ");
}
for (dashes = 1; dashes <= (i + 1); dashes++){
printf("#");
}
printf("\n");
}
return 0;
}
Solution 5:[5]
Inside your main loop you have:
// Instruction for printing 1 space
// Sub loop for printing x+1 number of hashes
// Instruction for printing a new line
So each time it loops round it's correctly drawing the hashes and a new line but your not telling it to draw any more than 1 space each time as unlike the subloop for the hashes the instruction doesn't increase with each pass.
Solution 6:[6]
#include<stdio.h>
#include<cs50.h>
int main (void)
{
int height;
int c = 0;
do
{
printf("Height?\n");
height = GetInt();
}while(height < 0 || height > 23);
for(int a = height; a > 0; a--)
{
for(int b = 0; b < a - 1; b++)
{
printf(" ");
}
printf("#");
c++;
for (int d = 0; d < c; d++)
{
printf("#");
}
printf("\n");
}
}
Solution 7:[7]
Here is how I solved the version of this problem with both sides, but you can make it work with only one, just use 2 for-loops instead of three.
If we have to take a number 1 – 8 as an input which is the height of our pyramid, we can use one for loop to print out each row of the pyramid.
Inside this loop, we will need another two for loops that print spaces and hashes.
And since we need the other side of the pyramid as well, we will use three loops in total within our main loop.
Why three loops and not four? Because we don’t actually need to print space on the right side of the pyramid.
Beside the height variable we need another one that will work as a counter, since we cannot manipulate our height variable.
These for loops work in opposite direction, i.e. the more spaces we have, less hashes we print and vice versa.
So, the final CS50 Pset 1 Mario More solution looks exactly like this:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do {
height = get_int("Height: ");
}
while (height < 1 || height > 8);
if (height > 0 || height < 9) {
int counter = 0;
for (int row=0; row<height; row++) {
if (counter != height) {
for (int spaces = (height-1) - counter; spaces > 0; spaces--) {
printf(" ");
}
for (int hashes = 0; hashes <= counter; hashes++) {
printf("#");
}
//ignore this block below if you want only one side
printf(" ");
for (int hashes = 0; hashes <= counter; hashes++) {
printf("#");
}
//##################################################
printf("\n");
counter++;
}
}
}
}
I actually made a blog post about this since I like to keep notes in case that you need more information.
Solution 8:[8]
This is how I got it to work for mario more comfortable.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n;
do
{
n = get_int("Height: ");
}
while (n < 1 || n > 25);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i + j < n - 1)
printf(" ");
else
printf("#");
}
printf(" ");
for (int j = 0; j < n; j++)
{
if (n - i < j + 2)
printf("#");
}
printf("\n");
}
}
Solution 9:[9]
#include<cs50.h>
#include<stdio.h>
int main()
{
int n;
do
{
n = get_int("Height: ");
}
while (n < 1);
for (int i = 0; i<n; i++)
{
for (int j = 0; j < i+1; j++)
{
printf("#");
}
printf("\n");
}
}
Solution 10:[10]
I think your code won't be working properly in the last raw, for that the second loop starts with adding an empty block automatically for each i-iteration, however the last i-iteration to build the base raw doesn't have any empty block, but rather only #s. The code should look more like this:
#include <cs50.h>
#include <stdio.h>
int main(void)
{ int height;
do {
height = get_int("height: ");
}
while (height<1 || height>8);
int i, j;
for ( i = 0; i < height; i++) {
for ( j = 0; j < height; j++) {
if (j + i < height - 1) {
printf(" ");
} else {
printf("#");
}
}
printf("\n");
}
}
Solution 11:[11]
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Get positive integer from user
int height;
do
{
height = get_int("Height: ");
}
while (height < 1 || height > 8);
//for loop for next line
for (int hash = 1; hash < height + 1; hash++)
{
for (int space = height - hash; space > 0; space--) // for loop to add spaces
{
printf(" ");
}
for (int hashwidth = 0; hashwidth < hash; hashwidth++)// for loop to add hashes
{
printf("#");
}
printf("\n");
}
}
Solution 12:[12]
Can the above problem be solved using so-called width.c program in the book C Primer Plus by Stephen Prata?
/* width.c -- field widths */
#include <stdio.h>
#define PAGES 959
int main(void)
{
printf("*%d*\n", PAGES);
printf("*%2d*\n", PAGES);
printf("*%10d*\n", PAGES);
printf("*%-10d*\n", PAGES);
return 0;
}
Solution 13:[13]
#include<cs50.h>
#include<stdio.h>
int main()
{
int n;
do
{
n = get_int("Height: ");
}
while (n < 1 || n > 8);
for (int i = 0; i<n; i++)
{
for (int k=n-i; k>1 ; k--)
{
printf(" ");
}
for (int j = 0; j < i+1; j++)
{
printf("#");
}
printf("\n");
}
}
Solution 14:[14]
After banging my head for awhile, I came across this, and it works.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int height = 0;
int line = 0;
int column = 0;
while (true)
{
height = get_int("Height: ");
if (height >= 1 && height <= 8)
{
break;
}
}
/* Chooses a row: */
for ( line = 1 ; line <= height ; line++ )
{
/* Chooses a column: */
for ( column = 1 ; column <= height ; column++ )
{
/* Prints an hash character or a space character: */
if ( column >= height + 1 - line )
{
printf("#");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
Solution 15:[15]
#include <stdio.h>
#include <cs50.h>
int main(void){
int s=0;
int hash=0;
int ht;
int h;
do{
printf("Height: ");
ht=GetInt();
for (h = ht; h > 0 ; h--){
printf("\n");
hash = ht - (h - 1);
s = ht - hash;
int i;
for (i = 0; i <= s; i++){
printf(" ");
}
for (i = 0; i <= hash; i++){
printf("#");
}
printf(" ");
for (i = 0; i <= hash; i++){
printf("#");
}
}
return 0;
}
while(ht > 0 || ht < 23);
return 0;
}
Solution 16:[16]
//this is how you only make the right aligned pyramid
#include <stdio.h>
#include <cs50.h>
int main(void){
int s=0;
int hash=0;
int ht;
int h;
do{
printf("Height: ");
ht=GetInt();
for (h = ht; h > 0 ; h--){
printf("\n");
hash = ht - (h - 1);
s = ht - hash;
int i;
for (i = 0; i <= s; i++){
printf(" ");
}
for (i = 0; i <= hash; i++){
printf("#");
}
}
return 0;
}
while(ht > 0 || ht < 23);
return 0;
}
Solution 17:[17]
//Author: Andriyevski Serhiy
#include <stdio.h>
#include <cs50.h>
int main(void)
{
// declaring my variables
int height;
int all_row;
int space;
int hash;
// prompts user for integer until integer is 0 to 23
do
{
printf("Please choose a number from 0 to 23:");
height = GetInt();
}
while ((height < 0) || (height > 23));
// this is the loop that will create the number of rows in the pyramid entered by the user
for (all_row = 1; all_row <= height; all_row++)
{
for (space = (height - all_row); space > 0; space--)
{
printf(" ");
}
for (hash = 1; hash <= (all_row + 1); hash++)
{
printf("#");
}
printf("\n");
}
return 0;
}
Solution 18:[18]
int main(void)
{
do
{
height = get_int("Height: "); //Requesting input from user for height.
}
while (height < 0 || height > 8); //Limits user input to between 0 and 8.
for (int rows = 1; rows <= height; rows++)
{
for (int spaces = 0; spaces < (height - rows); spaces++) //Based on the row number, there will be a certain amount of spaces. E.g. If the height is 8 and it is row 1, there are 7 spaces.
{
printf(" ");
}
for (int hashes = 0; hashes < rows; hashes++) //Based on which row it is, the number of hashes printed is equal to the row number. E.g. row 1 has 1 hash.
{
printf("#");
}
printf(" ");
for (int hashes = 0; hashes < rows; hashes++) //This prints the right side of the pyramid. No need to print the spaces.
{
printf("#");
}
printf("\n");
}
Hopefully this code is reasonable enough to read and hope it helps!
Also, it helped me to realise that the terminal prints from top to bottom and this was a main factor in designing the algorithm for this problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
