|
+- -+ | 1 2 3 | | 4 5 6 | | 7 8 9 | +- -+ |
|
I want to focus on
programming issues
NOT algorithmic issues
Once you have mastered the programming techniques, you can easily write the same functions for NxN matrices |
class Matrix3x3
{
.....
}; // <--- Note: class definition must end in ;
|
|
| What information is necessary to represent a 3x3 matrix ? |
class Matrix3x3
{
public:
double A[3][3];
}; // <--- Note: class definition must end in ;
|
class Matrix3x3
{
public:
double A[3][3];
};
|
there are NO functions existing that accept a parameter of that class.
Then obviously you CANNOT invoke any function using a parameter of the new class
We must write some first....
I compare writing functions that uses the new class
| Teach the compiler what to do with the new class |
+- -+ +- -+ | 1 2 3 | | 8 9 10 | | 4 5 6 | + 7 = | 11 12 13 | | 7 8 9 | | 14 15 16 | +- -+ +- -+ |
|
|
class Matrix3x3
{
public:
double A[3][3];
};
Matrix3x3 addScalar(Matrix3x3 *m, double x)
{
Matrix3x3 out;
int i, j;
for (i = 0; i < 3; i = i+1)
for (j = 0; j < 3; j = j+1)
out.A[i][j] = m->A[i][j] + x;
return(out);
}
|
int main(int argc, char *argv[])
{
Matrix3x3 B, C;
C = addScalar(&B, 40);
}
|
|
class Matrix3x3
{
public:
double A[3][3];
Matrix3x3 addScalar(double x)
{
Matrix3x3 out;
int i, j;
for (i = 0; i < 3; i = i+1)
for (j = 0; j < 3; j = j+1)
out.A[i][j] = this->A[i][j] + x;
return(out);
}
};
|
NOTE:
|
int main(int argc, char *argv[])
{
Matrix3x3 B, C;
C = B.addScalar(40);
}
|
+- -+ +- -+ +- -+ | 1 0 0 | | 1 2 3 | | 2 2 3 | | 1 1 0 | + | 4 5 6 | = | 5 6 6 | | 1 1 1 | | 7 8 9 | | 8 9 10 | +- -+ +- -+ +- -+ |
|
|
class Matrix3x3
{
public:
double A[3][3];
};
Matrix3x3 addMatrix(Matrix3x3 *m1, Matrix3x3 *m2)
{
Matrix3x3 out;
int i, j;
for (i = 0; i < 3; i = i+1)
for (j = 0; j < 3; j = j+1)
out.A[i][j] = m1->A[i][j] + m2->A[i][j];
return(out);
}
|
int main(int argc, char *argv[])
{
Matrix3x3 A, B, C;
C = addMatrix(&A, &B);
}
|
|
class Matrix3x3
{
public:
double A[3][3];
Matrix3x3 addMatrix(Matrix3x3 *m2)
{
Matrix3x3 out;
int i, j;
for (i = 0; i < 3; i = i+1)
for (j = 0; j < 3; j = j+1)
out.A[i][j] = this->A[i][j] + m2->A[i][j];
return(out);
}
};
|
NOTE:
|
int main(int argc, char *argv[])
{
Matrix3x3 A, B, C;
C = B.addMatrix(&A);
}
|