array display

I'm having trouble getting this 2 dimensional array to display as a pyramid, and i have no idea where to begin.
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
#include <iostream>
#include <fstream>
#include <cstdlib>  
#include <string>   
#include <iomanip> 
#include <vector>  
#include <algorithm> 
#include <strstream>
using namespace std;

int main()
{
	const int maxr = 7;
	const int maxc = 7;
	int np[maxr][maxc] = { 0,0,0,0,0,0,0, 
			        -1,0,0,0,0,0,0,
			        -1,-1,0,0,0,0,0,
			        -1,-1,-1,0,0,0,0,
				-1,-1,-1,-1,0,0,0,
				-1,-1,-1,-1,-1,0,0,
				-1,-1,-1,-1,-1,-1,0};  //array num, 20 elements
	  
	int i, j, shift = 3;

	//array skipping the -1
	cout << endl;
	cout << "Starting of the display for the pyramid" << endl;

	for (i = maxr - 1; i >= 0; i--)
	{ 
		cout << endl;
		for (j = maxc - 1; j >= 0; j--)
		{
				if (np[i][j] != -1)
				cout << setw(shift) << np[i][j] << " ";
		}
	}
			
	cout << endl;

	system("pause");

	return 0;

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
The display looks something like this:
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
0 0 0 0 0 0
I want the display to look like this:
           0
          0 0
         0 0 0
        0 0 0 0
       0 0 0 0 0
      0 0 0 0 0 0
You don't need an array to do this at all.
Start with a pencil and paper. Write down the number of spaces and the number of zero's that you need to print for row 1.
Then do the same for row 2.
Then row 3.
Continue if you want, or skip right to row 6.

Now, figure out a formula for the number of spaces as a function of the row number.
And do the same for the number of zero's.

Now the code becomes easy:
1
2
3
4
5
6
for (int row = 1; row <7; ++row) {
    int numSpaces = number of spaces for the row
    int zeros = number of zeros for the row
    printSpaces(numSpaces); // you'll need to write the printSpaces() function
    printZeros(zeros);   // and write the printZeros() function
}

I need to figure out how to do it with an array because later on I have to read a file into the array and display it as a pyramid, and the file isnt just 0's.
Topic archived. No new replies allowed.