number pattern

I want to make this pattern using only for loops.

(The asterisks represent spaces)

1 2 3 4 5 5 4 3 2 1
1 2 3 4 ***4 3 2 1
1 2 3 ******3 2 1
1 2 *********2 1
1 ************1

please help!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>

/*
This function prints numbers like this:
1 2 3 4 5 5 4 3 2 1
1 2 3 4     4 3 2 1
1 2 3         3 2 1
1 2             2 1
1                 1

In this case, largestNumber was 5. You can put in any natural number for
largestNumber, and that will be the number it counts up to, then counts
back down from.
*/
void numberPattern(unsigned largestNumber)
{
	//numberToSpace is the number that will be replaced with a space. All
	//numbers larger than it will also be replaced with spaces.
	for (unsigned numberToSpace = largestNumber + 1; numberToSpace >= 1; --numberToSpace)
	{
		//First, we need to count up from 1 to largestNumber.
		for (unsigned countUpNumber = 1; countUpNumber <= largestNumber; ++countUpNumber)
		{
			//If countUpNumber is greater than or equal to numberToSpace,
			//print a space in place of the number. Otherwise, print the number.
			if (countUpNumber >= numberToSpace)
			{
				std::cout << " ";
			}

			else
			{
				std::cout << countUpNumber;
			}

			std::cout << " ";
		}
		
		//Now, we count down from largestNumber to 1.
		for (unsigned countDownNumber = largestNumber; countDownNumber >= 1; --countDownNumber)
		{
			//Same as before.
			if (countDownNumber >= numberToSpace)
			{
				std::cout << " ";
			}

			else
			{
				std::cout << countDownNumber;
			}

			std::cout << " ";
		}
		
		//We are now done with all of the numbers for this line, so we print an end
		//line character.
		std::cout << std::endl;
	}
}

int main()
{
	numberPattern(5);
	numberPattern(3);
	numberPattern(9);
}


Output:

1 2 3 4 5 5 4 3 2 1 
1 2 3 4     4 3 2 1 
1 2 3         3 2 1 
1 2             2 1 
1                 1 
                    
1 2 3 3 2 1 
1 2     2 1 
1         1 
            
1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8     8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7         7 6 5 4 3 2 1 
1 2 3 4 5 6             6 5 4 3 2 1 
1 2 3 4 5                 5 4 3 2 1 
1 2 3 4                     4 3 2 1 
1 2 3                         3 2 1 
1 2                             2 1 
1                                 1 
                                    
We will not do your homework for you, post the code that you have written and we will edit to help you along the way. Our goal is to help you learn and if you aren't going to put forth any effort neither will we.

/rant
Topic archived. No new replies allowed.