This comes from Engineering Problem Solving with C 3rd editi
This comes from Engineering Problem Solving with C++ 3rd edition: Chapter 8, Problem 20: When running this, I\'m getting a runtime library error: Debug Assertion Failed! Expression vector subscript out of range. Please look over the code and let me know what you think could be causing the error, the program allows the input of temperatures and tolerance, but the error pops up just after that.
The program is supposed to take in temperatures and a tolerance value, then values are updated down the columns until equilibrium values reached.
#include<iostream>
#include<vector>
#include<iomanip>
#include<cmath>
using namespace std;
const int NROWS = 6;
const int MCOLS = 8;
int main()
{
int rows, cols;
vector< vector<double> > table(NROWS, vector<double>(MCOLS));
double temp_top;
double temp_right;
double temp_left;
double temp_bottom;
double tolerance;
double check_value;
double updated_temp;
double maxTemp = 0;
cout << \"Please enter values for the sides of the metal plate.\" << endl;
cout << \"Top: \";
cin >> temp_top;
cout << \"Right Side: \";
cin >> temp_right;
cout << \"Left Side: \";
cin >> temp_left;
cout << \"Bottom: \";
cin >> temp_bottom;
cout << \"Enter Tolerance value (must be greater than 0): \";
cin >> tolerance;
for (rows = 1; rows < NROWS - 1; rows++)
{
for (cols = 1; cols < MCOLS - 1; cols++)
{
table[rows][cols] = 0.0;
table[rows][0] = temp_left;
table[rows][MCOLS - 1] = temp_right;
}
}
for (cols = 0; cols < MCOLS; cols++)
{
table[0][cols] = temp_top;
table[NROWS - 1][cols] = temp_bottom;
}
do
{
maxTemp = 0.0;
for (cols = 1; cols < MCOLS; cols++)
{
for (rows = 1; rows < NROWS; rows++)
{
updated_temp = (table[rows + 1][cols] + table[rows - 1][cols] + table[rows][cols - 1] + table[rows][cols + 1]) / 4;
check_value = fabs(updated_temp - table[rows][cols]);
if (check_value > maxTemp)
maxTemp = check_value;
table[rows][cols] = updated_temp;
}
}
} while ((maxTemp>tolerance) || (maxTemp < -tolerance));
cout << \"\ \ Equilibrium values across the grid: \ \" << endl;
for (rows = 1; rows < NROWS; rows++)
{
for (cols=1; cols < MCOLS; cols++)
{
cout << setprecision(5) << \" \" << setw(10) << table[rows][cols] << \" \";
}
cout << endl;
}
getchar();
getchar();
return 0;
}
Solution
You are aceesing the vector beyond its memory in below assignment
updated_temp = (table[rows + 1][cols] + table[rows - 1][cols] + table[rows][cols - 1] + table[rows][cols + 1]) / 4;
if cols = 7 then accesing table[rows][cols +1] gives error as accessing vector beyond memory allocated as there is no column number 8..similarly for rows+1..hope it helps...
Below is the working code without Debug Assertion Failed!.I added one if statement to check arrays subscript ..rest unchanged and works..
-----------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream>
#include<vector>
#include<iomanip>
#include<cmath>
using namespace std;
const int NROWS = 6;
const int MCOLS = 8;
int main()
{
int rows, cols;
vector< vector<double> > table(NROWS, vector<double>(MCOLS));
double temp_top;
double temp_right;
double temp_left;
double temp_bottom;
double tolerance;
double check_value;
double updated_temp;
double maxTemp = 0;
cout << \"Please enter values for the sides of the metal plate.\" << endl;
cout << \"Top: \";
cin >> temp_top;
cout << \"Right Side: \";
cin >> temp_right;
cout << \"Left Side: \";
cin >> temp_left;
cout << \"Bottom: \";
cin >> temp_bottom;
cout << \"Enter Tolerance value (must be greater than 0): \";
cin >> tolerance;
for (rows = 1; rows < NROWS - 1; rows++)
{
for (cols = 1; cols < MCOLS - 1; cols++)
{
table[rows][cols] = 0.0;
table[rows][0] = temp_left;
table[rows][MCOLS - 1] = temp_right;
}
}
for (cols = 0; cols < MCOLS; cols++)
{
table[0][cols] = temp_top;
table[NROWS - 1][cols] = temp_bottom;
}
do
{
maxTemp = 0.0;
for (cols = 1; cols < MCOLS; cols++)
{
for (rows = 1; rows < NROWS; rows++)
{
//added below line
if( ( cols + 1 ) != MCOLS && ( rows + 1 ) != NROWS)
updated_temp = (table[rows + 1][cols] + table[rows - 1][cols] + table[rows][cols - 1] + table[rows][cols + 1]) / 4;
check_value = fabs(updated_temp - table[rows][cols]);
if (check_value > maxTemp)
maxTemp = check_value;
table[rows][cols] = updated_temp;
}
}
} while ((maxTemp>tolerance) || (maxTemp < -tolerance));
cout << \"\ \ Equilibrium values across the grid: \ \" << endl;
for (rows = 1; rows < NROWS; rows++)
{
for (cols=1; cols < MCOLS; cols++)
{
cout << setprecision(5) << \" \" << setw(10) << table[rows][cols] << \" \";
}
cout << endl;
}
getchar();
getchar();
return 0;
}


