Display an array as a pyramid

How do i get a 2D array to display as a pyramid? This actual program will read in a file (same number of negative ones as this array but random numbers instead of zeros) and store it in the array then display the array, but I need to display it as a pyramid.
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
#include <iostream>
#include <fstream>
#include <cstdlib>   
#include <iomanip>   
#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};  
	int i, j, shift = 3;

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

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

	system("pause");

	return 0;

}

1
2
3
4
5
6
The 2D array isnt just zero's so I CANT use a something like this that counts zeros or negative ones

if (np[i][j] == -1)
    cout << ' ';
else
    cout << setw(2) << '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

Once again, in the actual program the numbers ARE NOT zeros, so i cannot use a zero counter.
http://www.cplusplus.com/forum/beginner/161523/
http://www.cplusplus.com/forum/beginner/161700/

I need to figure out how to do it with an array
I need to get the zeros in this array to display as a pyramid.
12
13
14
15
16
17
18
	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}; 
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've gotten 2 answers. The first one you said you can't use because you have to use only what's in the array. The second one you're saying you can't use because it counts the negative ones inside the array. The only things in your array are zeros and negative ones!

The 2D array isnt just zero's so I CANT use a something like this that counts zeros or negative ones
Once again, in the actual program the numbers ARE NOT zeros, so i cannot use a zero counter.

Maybe instead of asking about the array you've shown, and getting perfectly applicable solutions for making a pyramid out of that array (which was the exact question you asked), you should say what is the actual array you're asking about.
Last edited on
This is the actual code im working with, i didnt want to use this in the question becuase i dont think you can read in the file.
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
#include <iostream>
#include <fstream>
#include <cstdlib>   
#include <iomanip>   
#include <algorithm> 
#include <strstream>
using namespace std;

int main()
{
	ifstream inFile;
	const int maxr = 7;
	const int maxc = 7;

	inFile.open("figure2_A.txt"); //reading in the figure2_A file
	
	int np[maxr][maxc]; //array initialization
	
	int i, j, shift = 3;

	for (i = maxr - 1; i >= 0; i--) 
	{ 
		for (j = maxc - 1; j >= 0; j--)
		{
				inFile >> np[i][j];	//storing it in the array
		}
	}

	cout << "Starting display for the pyramid:" << endl;

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

	system("pause");

	return 0;

}
Last edited on
No idea what's in your array without knowing what's in the file.
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
This is the File that gets read in
4
0
0
0
0
0
0
-1
12
22
35
0
15
21
-1
-1
0
0
0
0
0
-1
-1
-1
0
118
102
0
-1
-1
-1
-1
0
0
179
-1
-1
-1
-1
-1
0
0
-1
-1
-1
-1
-1
-1
0
So your array looks like this?

   -1   -1   -1   -1   -1   -1    0
   -1   -1   -1   -1   -1    0    0
   -1   -1   -1   -1    0    0  179
   -1   -1   -1    0  118  102    0
   -1   -1    0    0    0    0    0
   -1   12   22   35    0   15   21
    4    0    0    0    0    0    0


You want the pyramid to look like this?

               0
             0   0
           0   0 179
         0 118 102   0
       0   0   0   0   0
    12  22  35   0  15  21
   4   0   0   0   0   0   0


You see this code I gave you?

1
2
3
4
if (np[i][j] == -1)
    cout << ' ';
else
    cout << setw(2) << '0'; 


Was this really so hard?

1
2
3
4
if (np[i][j] == -1)
    cout << "  ";
else
    cout << setw(4) << np[i][j]; 
Last edited on
I had figured this out:
1
2
3
4
if (np[i][j] == -1)
    cout << "  ";
else
    cout << setw(4) << np[i][j];

But It still doesnt display as a perfect pyramid but i give up, that will have to do
Last edited on
If you mean you want the numbers "centered", then you need to do something like this:

1
2
3
int left = (2 * maxWidth - width) / 2;
int right = 2 * maxWidth - left - width;
cout << string(left, ' ') << np[i][j] << string(right, ' ');


^ Here maxWidth is the length of the longest number and width is the length of the current number. Of course, the blanks that are output when the number is negative should also be maxWidth spaces.

Figuring out how to find the length of numbers should be easy enough to do on your own.
Thanks for the help and patience, im not exactly c++ savvy. but im still having trouble because i didnt know exactly where the code above went, so this is what i have now:
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
#include <iostream>
#include <fstream>
#include <cstdlib>   
#include <iomanip>   
#include <algorithm> 
#include <strstream>
using namespace std;
int main()
{
	ifstream inFile;
	const int maxr = 7;
	const int maxc = 7;
	inFile.open("figure2_A.txt"); //reading in the figure2_A file
	
	int np[maxr][maxc]; //array initialization
	
	int i, j, shift = 3;

	for (i = maxr - 1; i >= 0; i--) 
	{ 
		for (j = maxc - 1; j >= 0; j--)
		{
				inFile >> np[i][j];	//storing it in the array
		}
	}

	cout << "Starting display for the pyramid:" << endl;

	for (i = 0; i <= maxr - 1; i++)
	{ 
		
		cout << endl;
		for (j = maxc - 1; j >= 0; j--)
		{
			 int width =  np[i][j].length(i); //im getting an error code here "Expression must have a class type" 
			 int maxWidth = 3;
	         int left = (2 * maxWidth - width) / 2;
             int right = 2 * maxWidth - left - width;
				if (np[i][j] == -1)
                   cout << setw(maxWidth) << " ";
				else
                   cout << string(left, ' ') << np[i][j] << string(right, ' ');
		}
	}
			
	cout << endl;

	system("pause");

	return 0;

}
Topic archived. No new replies allowed.