2D Array

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
Last edited on
Please edit your post to use code tags - http://www.cplusplus.com/articles/jEywvCM9/


#include "Chapter 9.h"; Classes/files with space in the names, is not a thing.

Edit: Also please show us what output you are getting, and what output you are expecting.
Last edited on
Topic archived. No new replies allowed.