0

0 down vote

I wonder if someone can clear something up for me. I have been working on this problem, and I have got a solution that works. ie, the numbes of spaces decline, and the number of hashes increases. BUT. I don't understand why it works.

This is part of the code. Height is the integer number given by the user. I then defined an integer s, equaled it to 'line' (has to be line otherwise the program doesn't work,

I initially tried this logic: the number of spaces has to be (height - 1) to start off with, and from there on, each line has one space less, so I tried to place s-- in the 'for loop'. But it didn't work.

What I did was the following, which works perfectly, but the logic seems all wrong to me. It shouldn't work, but it does. Nowhere am I stating I'm decrementing s, so why does the program do it?

for (line = 0; line < height; line++){

 // print the spaces --> how on earth does this work??? Shouldn't this see        
//the spaces increasing? Not decreasing?

   for (s = line; s < height - 1 ; s++){
        printf(" ");
   }

}

Hope someone can shed some light on this. Many thanks

2
  • Where exactly are you printing the #? I do not see the printf("#"); statement anywhere. Commented Dec 12, 2014 at 16:00
  • I didn't show that in the question as the #'s aren't relevant to my question.
    – firefly
    Commented Dec 13, 2014 at 20:14

2 Answers 2

4

The value of s is increasing but the number of spaces it prints will be decreasing because you are inside the outer loop.

Take your program and add some debug print statements like this:

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

int main(int argc, string argv[])
{
    int line, s;
    int height = 10;

    for (line = 0; line < height; line++)
    {
        printf("line is %d and s is: ", line);  //  <--- add this

        for (s = line; s < height - 1 ; s++)    
        {
           printf("%d ", s);                  //  <--- change to print 's'
        }

        printf("\n");

    }
}

What you will get is this:

line is 0 and s is: 0 1 2 3 4 5 6 7 8 
line is 1 and s is: 1 2 3 4 5 6 7 8 
line is 2 and s is: 2 3 4 5 6 7 8 
line is 3 and s is: 3 4 5 6 7 8 
line is 4 and s is: 4 5 6 7 8 
line is 5 and s is: 5 6 7 8 
line is 6 and s is: 6 7 8 
line is 7 and s is: 7 8 
line is 8 and s is: 8 
line is 9 and s is: 

Each time the second loop runs, the starting value of s is the next larger number, so each line will have one fewer space. That's what you want. Can you see now why your code works?

2
  • fantastic! Thanks for your answer :-)
    – firefly
    Commented Jan 7, 2015 at 21:22
  • You're quite welcome! Please upvote and then accept my answer so this falls out of the unanswered queue. :)
    – curiouskiwi
    Commented Jan 7, 2015 at 21:30
0

Your "s" variable is initialized with the "line" variable and as "line" variable is ALWAYS incremented the initial value for inner for loop that is "s" is also incremented each time outer for loop is executed. To get lesser spaces your "s" variable needs to decrement.

The correct code would be

for (line = 0; line < height; line++)
{
    for (s = height - line; s > 0 ; s--)
         printf(" ");
}

Also you need to print "#" as well as get the next "#s" on another line

3
  • I do print the '#''s, I just didn't add that in the question as that's in another loop, I have no problem with the '#''s.
    – firefly
    Commented Dec 13, 2014 at 20:07
  • I do print the '#''s, I just didn't add that in the question as that's in another loop, I have no problem with the '#''s. The code as you describe it doesn't work. That code was the first thing I tired . It prints out an equal number of spaces and shifts the whole pyramid to the right x number of spaces. Now the code I entered, most definitely does work. Each line has one space less with my code. With your code each line has the same number of spaces. The problem is, logically this doesn't make sense and I had hoped someone can explain why it does. ie, why does my code work. Thanks
    – firefly
    Commented Dec 13, 2014 at 20:13
  • I've fixed the bug. Check it out.
    – Ibrahim
    Commented Dec 14, 2014 at 5:12

You must log in to answer this question.

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