Need help with program for class

So far everything in this program works as it should, except for my "add" class Matrix function.

Here's the assignment: http://www.scribd.com/doc/115862779/Project-6-Assignment

My program runs fine, with the exception that it not adding the correct matrices. I think the temp2 matrix isn't keeping the correct values from Matrix a but I'm not sure. :/ Please help!


#include<iostream>
#include<iomanip>
using namespace std;

class Matrix
{
private:
static const int rows=3;
static const int columns=3;
double data[rows*columns];
double temp2[rows][columns];
public:
Matrix();
void add(const Matrix&);
void mult(double);
void transpose();
double trace();
double norm();
void read();
void readb();
void write();
};

Matrix::Matrix()
{
double data[rows*columns] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0} ;
}

void Matrix::add(const Matrix&m)
{
for (int i=0; i<3; i++)
{
int z=0;
for (int j=0; j<3; j++)
{
data[3*i+z] = temp2[i][j] + data[3*i+z];
z++;
}
}
}

void Matrix::mult(double c)
{
for (int i=0; i<9; i++)
{
data[i] = (data[i])*c;
}
for (int i=0; i<3; i++)
{
int z=0;
for (int j=0; j<3; j++)
{
temp2[i][j] = data[3*i+z];
z++;
}
}
}

void Matrix::transpose()
{
double temp[3][3];
for (int i=0; i<3; i++)
{
int z=0;
for (int j=0; j<3; j++)
{
temp[i][j] = data[3*i+z];
temp2[i][j] = data[3*i+z];
z++;
}
}

for (int p=0; p<3; p++)
{
for (int q=0; q<3; q++)
{
temp[q][p] = temp2[p][q];
}
}

for (int i=0; i<3; i++)
{
int z=0;
for (int j=0; j<3; j++)
{
data[3*i+z] = temp[i][j];
z++;
}
}
}

double Matrix::trace()
{
cout.setf(ios::fixed | ios::showpoint);
cout.precision(3);
double a;
a = data[0] + data[4] + data[8];
return a;
}


double Matrix::norm()
{
cout.setf(ios::fixed | ios::showpoint);
cout.precision(3);
double sum=0.0;
for (int i=0; i<9; i++)
{
sum += pow(data[i], 2.0);
}
return pow(sum, 0.5);
}

void Matrix::read()
{
for (int i=0; i<9; i++)
cin >> data[i];
}


void Matrix::write()
{
cout.setf(ios::fixed | ios::showpoint);
cout.precision(3);
for (int i=0; i<3; i++)
{
cout << setw(10) << data[i];
}
cout << endl;
for (int i=3; i<6; i++)
{
cout << setw(10) << data[i];
}
cout << endl;
for (int i=6; i<9; i++)
{
cout << setw(10) << data[i];
}
cout << endl;
}


int main()
{
Matrix a, b;
double c;
int choice;

do
{
cout << "\n COP2271 MATRIX CALCULATOR CLASS PROGRAM MENU\n";
cout << "\n 0 - Quit\n 1 - Enter A\n 2 - Enter B\n 3 - A=A+B\n 4 - A=c*A\n 5 - A=transpose(A)\n 6 - print Trace(A)\n 7 - print ||A||\n";
cout << "\nEnter menu selection : ";
cin >> choice;

if (choice == 1)
{
cout << "Enter 9 elements of A \n";
a.read();
}

else if (choice == 2)
{
cout << "Enter 9 elements of B \n";
b.read();
}

else if (choice == 3)
{
a.add(b);
cout << "A+B= \n";
a.write();
}

else if (choice == 4)
{
cout << "Enter c : ";
cin >> c;
a.mult(c);
cout << "c*A= \n";
a.write();
}

else if (choice == 5)
{
cout << "transposed A= \n";
a.transpose();
a.write();
}

else if (choice == 6)
{
cout << "Trace(A)= " << a.trace() ;
cout << endl;
}

else if (choice == 7)
{
cout << "||A||= " << a.norm() ;
cout << endl;
}

else if (choice == 0)
{
goto Quit;
}

else
{
cout << "Invalid entry.";
}


} while (choice != 0);

Quit:
cout << "Now Quitting..";
system("PAUSE");
return 0;
}




Nevermind! Sorry, just figured out I was missing a variable

---> m.data[3*i+z];
Topic archived. No new replies allowed.