Trying to display integers and spaces.

I'm trying to write a program for class that displays the integers 1-9 so that it looks like this:
                1
              1 2
            1 2 3
          1 2 3 4
        1 2 3 4 5 
      1 2 3 4 5 6 
    1 2 3 4 5 6 7
  1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9

The first line has 16 spaces and the number 1, the second line has 14 spaces and 1 2, etc. I'm supposed to use nested for loops. I'm really struggling with this. Thanks for your help!
How many space to output the first time? 16
How many space to output the second time? 14
How many space to output the third time? 12

See a pattern here? How would you recreate this pattern using a for loop?

1
2
3
for (int numberOfSpaces = 16; numberOfSpaces >= 0; ....)
{
  }


I've left the final part of that for loop for you to do.

Last edited on
Thanks! I don't understand how to make it display the correct number of spaces. I understand how to display spaces, of course, but nothing I've tried has turned out right.
In my previous comment, I gave you a loop that would have a variable "numberOfSpaces". That variable would be the number of spaces to output.

Here is a way you might use that variable:

1
2
3
4
for (int spacesStillNeedingToBeOutput = numberOfSpaces; spacesStillNeedingToBeOutput>0; --spacesStillNeedingToBeOutput)
{
  cout <<" ";
}
Thank you so much for your help.
Well, to start, you should probably have the first for loop iterate through all the lines. Inside that for loop, you would need to find out the number of spaces required based on the current line. Once you find that number, you can use a for loop to print all of the spaces. Then you just add another for loop to print all the numbers.
I've got the spaces all worked out now, but I'm struggling with the numbers. This is the closest I've gotten:
                                1
2
3
4
5
6
7
8
9
                              1
2
3

This isn't all of it. It just continues in the same way where the ones have the correct number of spaces, but nothing else does. Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main ()
{
    for (int space=16; space>=0; space--) 
    {
        for (int spaceOutput=space; spaceOutput>0; spaceOutput--) 
        {
            cout << "  ";
        }
        for (int num=1; num<=9; num++) 
        {
            cout << num << endl;
        }
    }
    return 0;
}


I've tried a few different things, but none of them have worked.
Shouldn't you be ending the line after you've output all your numbers?
That did help. I don't understand how to make it only display the numbers I want. Right now it's giving me all of them.
Well, the pattern 1=16, 2=14, 3=12, ... looks like f(m) = 18 - 2m = 2(9 - m) to me, so you could stick to a normal incrementing loop (1, 2, 3, ...) and then calc how much padding you need to add with the inner loop from that.

And the inner loop shouldn't be outputting 1..9 each time, should it!
Last edited on
Right now I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main ()
{
    for (int space=16; space>=0; space=space-2) 
    {
        for (int spaceOutput = space; spaceOutput>0; spaceOutput=spaceOutput-2)
        {
            cout <<"  ";
        }
        for (int num=1; num<=9; num++) 
        {
            cout << num << " ";
        }
        cout << endl;
    }
    return 0;
}


Which gives me this:
                1 2 3 4 5 6 7 8 9 
              1 2 3 4 5 6 7 8 9 
            1 2 3 4 5 6 7 8 9 
          1 2 3 4 5 6 7 8 9 
        1 2 3 4 5 6 7 8 9 
      1 2 3 4 5 6 7 8 9 
    1 2 3 4 5 6 7 8 9 
  1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 
So close :)

If only there was some way to only output "1" in the first loops, and then "1 2" in the second loop, and then "1 2 3" in the third loop. The patterm would be something like this:

Loop 1: "1"
Loop 2: "1 2"
Loop 3: "1 2 3"
Loop 4: "1 2 3 4"

Hey look! On loop 1 you stop at 1, and on loop 2 you stop at 2, and on loop 3 you stop at 3, and so on. Seems like there a pattern there...

Or here's another way to look at it. Each time, you have to output this much stuff:

               1


That's a whole bunch of spaces, and the rest in numbers. Perhaps if you kept track of how many spaces you'd written, you'd be able to know how many numbers you had to write.

Welcome to programming, by the way :) This is programming. The art and craft of thinking about problems in such a way that you can apply the solutions in a programming language. The language itself is just syntax. Programming is thinking.
Last edited on
make your second loop use a variable from your first loop so that it increments also. I struggled with this also a bit ago

http://www.cplusplus.com/forum/beginner/61241/
I've got it now. Thanks for all the help!
Topic archived. No new replies allowed.