Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

stuck at pset1

0 votes
asked Oct 31, 2015 in Pset1 by sabin (230 points)
#include <cs50.h>
#include <stdio.h>

int main(void)

{
    int height, rows, spaces, hashes;

        do 
        {   
            printf("How high will mario climb? Please pick a number between 1-23: ");
            height = GetInt();
        } 
        while (height <= 0 || height >= 24);

    for (rows = 0; rows <= height; rows++) //print user generated amount of rows
    {

        for (spaces = height - rows - 2 ; spaces == 0; spaces --) //print spaces
        {
            printf(" ");
        }
        for (hashes = rows + 2; hashes <= height; hashes++) //print hashes
        {
          printf("#");  
        }
        printf("\n");
    }
   return 0;
 }
 please let me know my error

1 Answer

0 votes
answered Oct 31, 2015 by Faïza Harbi (11,960 points)
less or equal?? no, strictly less. why? take a pen and paper, and say add 10 times 1 to a var int i initialized to 0.

if you do while i <= 10 or for(i = 0 ; i <= 10; i++) : You 're gonna get 11. count the number of time i is less or equal to 10.

the do whie loop. 0 is allowed but 24 is not!!!!

 

For loop:

for( starting point; stop point; step/update). how can spaces == 0 be a condition here, both hashes and spaces are dependent of the height. no height dependence for you spaces. so ....

 

Look further in time, there are other Pset1 questions
commented Oct 31, 2015 by Faïza Harbi (11,960 points)
also, check logical operators
commented Nov 1, 2015 by sabin (230 points)
24 in do while loop can be used because it is the conditions that prompt and validate user input
commented Nov 1, 2015 by Faïza Harbi (11,960 points)
OK so this is all you've got from what I wrote? in your do while loop you don't allow 0 to be a height. that was my point
commented Nov 1, 2015 by sabin (230 points)
yeah i got it from cs50 ..... but i dont know it was yours sorry for that anyway thanxs for your help
Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

187 questions

248 answers

217 comments

3,056 users

10 Online
0 Member And 10 Guest
...