Stack around np
Apr 22, 2015 at 2:52am UTC
Im creating a program to solve a number pyramid, it reads in these numbers, it then Stores them in an array then displays the array except the -1's, in the shape of a pyramid. Then it solves as much of the pyramid as it can, then displays the pyramid again. it does this till all the zeros are gone. but whenever it does its first pass it messes the display up and "corupts the stack around np" which is the name of the array. Please help!
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
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
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <strstream>
using namespace std;
int main()
{
ifstream inFile;
const int maxr = 7;
const int maxc = 7;
inFile.open("figure2_A.txt" ); //opening the figure2_A file
if (inFile.fail())
{
cout<< "the file was not found" << endl;
system("pause" );
exit(1);
}
int np[maxr][maxc]; //array initialization
int i, j, shift = 3;
double zerocount = 0, zerocnt = 0, count = 0;
for (i = 0; i < maxr; i++)
{
for (j = 0; j < maxc; j++)
{
inFile >> np[i][j]; //storing it in the array
}
}
inFile.close(); // closing the file
cout << "Initial display of the pyramid:" << endl << endl;
for (i = 6; i >= 0; i--)
{
cout << endl;
for (j = 0; j < maxc; j++)
{
if (np[i][j] == -1)
cout << " " ;
else
cout << setw(6) << np[i][j];
}
}
for (i = 0; i < maxr - 1; i++)
{
for (j = 0; j < maxc; j++)
{
if (np[i][j] == 0)
zerocnt++;
}
}
cout << endl << endl << endl; //putting space after the pyramid
cout << "The number of zeros starting the pyramid is: " << zerocnt << endl;
while (zerocount != 0); //Begin giant for loop
{
//solver in the while loop
for (i = 6; i >= 0; i--)
{
for (j = 0; j < 7; j++)
{
(np[i][j] != -1 || np[i + 1][j + 1] == 0 || np[i][j + 1] != 0 || np[i][j] != 0);
np[i + 1][j + 1] = np[i][j + 1] + np[i][j];
(np[i][j] != -1 || np[i + 1][j + 1] != 0 || np[i][j + 1] == 0 || np[i][j] != 0);
np[i][j + 1] = np[i + 1][j + 1] - np[i][j];
(np[i][j] != -1 || np[i + 1][j + 1] != 0 || np[i][j + 1] == !0 || np[i][j] == 0);
np[i][j] = np[i + 1][j + 1] - np[i][j + 1];
}
}
//Display in the while loop
for (i = 6; i >= 0; i--)
{
cout << endl;
for (j = 0; j < 7; j++)
{
if (np[i][j] == -1)
cout << " " ;
else
cout << setw(6) << np[i][j];
}
}
//Zero counter in the while loop
for (i = 0; i < maxr - 1; i++)
{
for (j = 0; j < maxc; j++)
{
if (np[i][j] == 0)
zerocount++;
}
}
count++;
cout << endl << endl << endl; //putting space after the pyramid
cout << "The number of zeros remaining is: " << zerocount << endl;
cout << "The number of passes through the pyramid so far is: " << count << endl;
}
cout << endl;
system("pause" );
return 0;
}
Apr 22, 2015 at 7:46am UTC
Within the loop on line 72:
Lines 74/77/80: I guess that it should be if
?
Both i + 1
and j + 1
will be out ouf bounds when i/j are 6
Apr 22, 2015 at 2:06pm UTC
Lines 74, 77, and 80, are if
statements. Sorry about that, fixing it does not however solve the problem
Last edited on Apr 22, 2015 at 2:06pm UTC
Apr 22, 2015 at 2:13pm UTC
The problem is this:
Both i + 1 and j + 1 will be out ouf bounds when i/j are 6
Change line 70: 6 -> 5 and 72: 7 -> 6
don't forget to remove the ; at the end of lines 74/77/80
Apr 22, 2015 at 7:08pm UTC
That didnt fix it, some where in this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//solver in the while loop
for (i = 6; i >= 0; i--)
{
for (j = 0; j < 7; j++)
{
(np[i][j] != -1 || np[i + 1][j + 1] == 0 || np[i][j + 1] != 0 || np[i][j] != 0);
np[i + 1][j + 1] = np[i][j + 1] + np[i][j];
(np[i][j] != -1 || np[i + 1][j + 1] != 0 || np[i][j + 1] == 0 || np[i][j] != 0);
np[i][j + 1] = np[i + 1][j + 1] - np[i][j];
(np[i][j] != -1 || np[i + 1][j + 1] != 0 || np[i][j + 1] == !0 || np[i][j] == 0);
np[i][j] = np[i + 1][j + 1] - np[i][j + 1];
}
}
its causing all the negative ones to be changed to negatve 2's as which cause the display to be messed up, as for the stack corupting i have no idea
Topic archived. No new replies allowed.