Skip to main content
added 69 characters in body
Source Link
curiouskiwi
  • 18.7k
  • 2
  • 18
  • 43

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?

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

        for (s = line; s < height - 1 ; s++)
        {
           printf("%d ", 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?

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?

Source Link
curiouskiwi
  • 18.7k
  • 2
  • 18
  • 43

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

        for (s = line; s < height - 1 ; s++)
        {
           printf("%d ", 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?