0
#include<stdio.h>
#include<cs50.h>

int main(void)
{
    int n,r,a,s;

    do { 
        printf("the pyramids height?\n");
        n = get_int();
    } while(n<=0||n>=23);

    for(r=0;r==n+1;r++)
    {
        for(s=n-2;s==0;s--)
        {
            printf(" ");
        }

        for(a=;a==n+1;a++)
        {
            printf("#");
        }
    }
    {
        printf("\n");
    }
}

This is the code I wrote. I dont get the logical error I made, i have been doing it for a week or more. can someone help me?

2
  • Your code should result in a syntax error in the last for loop, a=; You haven't set the initial value.
    – robert_x44
    Commented Aug 22, 2017 at 18:13
  • It was mistake. I was making a change and forgot initializing Commented Aug 23, 2017 at 1:46

2 Answers 2

0

There are a few problems with your code, I'll try to point you in the right direction on the biggest mistake.

All of your for loops use == as a loop-continuation condition. This is not normal, and is probably not doing what you expect it to.

for(r=0;r==n+1;r++)
{
    ...
}

In this loop, the first thing that happens is that r is set to 0. Then the loop condition r==n+1 is tested, and if it is TRUE, the loop executes. The problem in your code is that the loop will never execute.

Executing the loop for n == 1 (if the user input was 1 for number of levels):

r = 0  // r is set to 0
r == n + 1  -->  0 == 1 + 1  -->  0 == 2  -->  False

Because the condition is false, the loop will not run. The loop will run only if that condition is True.

If you want a for loop to run 10 times:

for (r = 0; r < 10; r++)

If you want a for loop to run n times:

for (r = 0; r < n; r++)

Another way to think about a for loop is to convert it into a while loop. Your for loop would look like this:

r = 0;
while (r == n + 1) {  // this only runs if `r== n + 1`
    ...
    r++;
}

It may be easier to understand what is happening as a while-loop.

2
  • I am stuck in the part of making the logic of it moving to the next row. when i run my code i get a line full of hashes. Commented Aug 23, 2017 at 1:54
  • @SathyaSrivatsav At the end of each iteration of your outer for loop, you need to print a newline character "\n". You have a printf statement that does that, but it is outside your loop. So it prints one blank line after everything runs. You want it to run at the end of each line - so it needs to be inside the loop.
    – robert_x44
    Commented Aug 23, 2017 at 2:20
0

first, you need to check if your height is between 0 & 23. all you need is here to variables i and j maybe it easier for you to keep track. instead if while(n<=0||n>=23); because this is wrong here you're telling the computer that while the height is less or higher keep doing you need the height to be in the range of 0-23 implement for loop to keep going as it smaller than the height and inside that loop implement another for loop to keep tracking of the spaces to print it else prints #
I hope that helps give it check mark on the side of it did

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .