Run-Time Check Failure #2 - Stack around the variable 'ThetaMatrix' was corrupted.

I have written this code to multiply rotation matrices together. However, I keep getting a "Run-Time Check Failure #2 - Stack around the variable 'ThetaMatrix' was corrupted." upon running. This has happened for almost all the matrix variables. I'm not sure what I do to fix this? (Sorry, I know the code is probably very poor).

#include <iostream>
#include <cmath>
#include <math.h>

using namespace std;

void ThetaEntry (const double& angle1, double product1[3][3])
{
double pi;
pi = 3.14;
product1[0][0] = cos(angle1*pi/180);
product1[0][1] = -sin(angle1*pi/180);
product1[0][2] = 0;
product1[1][1] = sin(angle1*pi/180);
product1[1][2] = cos(angle1*pi/180);
product1[1][3] = 0;
product1[2][1] = 0;
product1[2][2] = 0;
product1[2][3] = 1;
}

void BetaEntry (const double& angle1, double product1[3][3])
{
double pi;
pi = 3.14;
product1[0][0] = cos(angle1*pi/180);
product1[0][1] = 0;
product1[0][2] = -sin(angle1*pi/180);
product1[1][1] = 0;
product1[1][2] = 1;
product1[1][3] = 0;
product1[2][1] = sin(angle1*pi/180);
product1[2][2] = 0;
product1[2][3] = cos(angle1*pi/180);
}

void GammaEntry (const double& angle1, double product1[3][3])
{
double pi;
pi = 3.14;
product1[0][0] = 1;
product1[0][1] = 0;
product1[0][2] = 0;
product1[1][1] = 0;
product1[1][2] = cos(angle1*pi/180);
product1[1][3] = sin(angle1*pi/180);
product1[2][1] = 0;
product1[2][2] = -sin(angle1*pi/180);
product1[2][3] = cos(angle1*pi/180);
}

void REntry (const double& a, const double& b, const double& c, double product1[3][3])
{
product1[0][0] = a;
product1[0][1] = 0;
product1[0][2] = 0;
product1[1][1] = b;
product1[1][2] = 0;
product1[1][3] = 0;
product1[2][1] = c;
product1[2][2] = 0;
product1[2][3] = 0;
}

void Multiply(double matrixx[3][3], double matrixy[3][3], double matrixz[3][3])
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
for (int inner = 0; inner < 3; inner++)
{
matrixz[row][col] += matrixx[row][inner] * matrixy[inner][col];
}
//std::cout << matrixz[row][col] << " ";
}
//std::cout << "\n";
}

}

void DisplayMatrix(double matrix[3][3])
{
for (int t = 0; t < 3; t++)
{
for (int i = 0; i < 3; i++)
{
cout << "Matrix " << "[" << t << "]" << "[" << i << "] = " << matrix[t][i] << endl;
}
}
}

void main ()
{
double x, y, z, theta, beta, gamma;
double ThetaMatrix[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
double BetaMatrix[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
double GammaMatrix[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
double ThetaBetaMatrix[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
double ThetaBetaGammaMatrix[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
double RMatrix[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
double FinalMatrix[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
cout << "input x value" << endl;
cin >> x;
cout << "input y value" << endl;
cin >> y;
cout << "input z value" << endl;
cin >> z;
cout << "input theta value" << endl;
cin >> theta;
cout << "input beta value" << endl;
cin >> beta;
cout << "input gamma value" << endl;
cin >> gamma;
ThetaEntry (theta, ThetaMatrix);
BetaEntry (beta, BetaMatrix);
GammaEntry (gamma, GammaMatrix);
REntry (x, y, z, RMatrix);
Multiply (ThetaMatrix, BetaMatrix, ThetaBetaMatrix);
Multiply (ThetaBetaMatrix, GammaMatrix, ThetaBetaGammaMatrix);
Multiply (ThetaBetaGammaMatrix, RMatrix, FinalMatrix);
cout << FinalMatrix[0][0] << endl;
cout << FinalMatrix[1][0]<< endl;
cout << FinalMatrix[2][0]<<endl;

}
Please use code tags when posting code. Highlight the code and then click the "<>" button to the right of the edit window.

Your code performs several out-of-bounds array accesses. Just search for [3] to see them. Remember, an array with 3 elements uses indexes 0-2, not 1-3.

Topic archived. No new replies allowed.