So I read the function 1 and 2 on the forum and kinda understand it. But I cannot apply it to when it comes to matrices. The homework problem wants me to write functions on multiplying and adding functions. I got the math part right as you can see on line 36 and 63.
So now I am trying to write function where I can add the two first, but I keep coming up with errors.
My function is line 7 and my recall of that function is line 35. I've tried different ways of writing it and keep getting errors. Please help with this one function and so I can write the other function for multiplying them.
#include <cmath>
#include <iostream>
usingnamespace std;
int addMatrix(int MA[3][3], int MB[3][3]){
int MC[3][3];
for (int i=0;i<=2;i++){
for (int j=0;j<=2;j++) {
MC[i][j]=MA[i][j]+MB[i][j];
}
}
return MC[3][3];
}
int main(int argc, char *argv[])
{
int matrixA[3][3], matrixB[3][3], matrixC[3][3], multMatrix[3][3];
//Enter info for the first matrix
cout<<"\tEnter the values for Matrix A in 3 by 3: "<<endl;
for (int i=0;i<=2;i++){
for (int j=0;j<=2;j++) {
cin>>matrixA[i][j];
}}
//Enter the info for the second matrix
cout<<"\n\nEnter the values for Matrix B in 3 by 3: "<<endl;
for (int i=0;i<=2;i++){
for (int j=0;j<=2;j++) {
cin>>matrixB[i][j];
}}
matrixC[3][3]= addMatrix(matrixA[3][3], matrixB[3][3]);
//This will add the two matrices together
// for (int i=0;i<=2;i++){
// for (int j=0;j<=2;j++) {
// matrixC[i][j]=matrixA[i][j]+matrixB[i][j];
// }
// }
//Displays the matrices
cout<<"\n\nMatrix A and Matrix B\n\n";
cout<<matrixA[0][0]<<"\t"<<matrixA[0][1]<<"\t"<<matrixA[0][2]<<"\n";
cout<<matrixA[1][0]<<"\t"<<matrixA[1][1]<<"\t"<<matrixA[1][2]<<"\n";
cout<<matrixA[2][0]<<"\t"<<matrixA[2][1]<<"\t"<<matrixA[2][2]<<"\n";
cout<<"and\n\n";
cout<<matrixB[0][0]<<"\t"<<matrixB[0][1]<<"\t"<<matrixB[0][2]<<"\n";
cout<<matrixB[1][0]<<"\t"<<matrixB[1][1]<<"\t"<<matrixB[1][2]<<"\n";
cout<<matrixB[2][0]<<"\t"<<matrixB[2][1]<<"\t"<<matrixB[2][2]<<"\n";
//Displays the addition of the matrices
cout<<"\n\nThe adding of the two matrice is\n\n";
cout<<matrixC[0][0]<<"\t"<<matrixC[0][1]<<"\t"<<matrixC[0][2]<<"\n";
cout<<matrixC[1][0]<<"\t"<<matrixC[1][1]<<"\t"<<matrixC[1][2]<<"\n";
cout<<matrixC[2][0]<<"\t"<<matrixC[2][1]<<"\t"<<matrixC[2][2]<<"\n";
//Mulitplying the matrices together
for (int i=0;i<=2;i++){
for (int j=0;j<=2;j++) {
multMatrix[i][j]=0;
for (int k=0; k<=2;k++){
multMatrix[i][j]+=matrixA[i][k]*matrixB[k][j];
}
}
}
//Displays the Product of the matrices
cout<<"\n\nThe product of the two matrice is\n\n";
cout<<multMatrix[0][0]<<"\t"<<multMatrix[0][1]<<"\t"<<multMatrix[0][2]<<"\n";
cout<<multMatrix[1][0]<<"\t"<<multMatrix[1][1]<<"\t"<<multMatrix[1][2]<<"\n";
cout<<multMatrix[2][0]<<"\t"<<multMatrix[2][1]<<"\t"<<multMatrix[2][2]<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
Arrays in C++ are basically pointers. In your function:
Line 7: allocating memory for array;
Line 14: Returning pointer to MC[0][0]
Line 15: MC goes out of scope and is deallocated.
Posiible solutions:
a) Dinamically allocate your array.
b) Pass array you, want write result to, to your addMatrix function.
#include <cmath>
#include <iostream>
#include <windows.h>
usingnamespace std;
void addMatrix(int MC[3][3],int MA[3][3], int MB[3][3])
{
for (int i=0;i<=2;i++){
for (int j=0;j<=2;j++) {
MC[i][j]=MA[i][j]+MB[i][j];
}
}
}
int main(int argc, char *argv[])
{
int matrixA[3][3], matrixB[3][3], matrixC[3][3], multMatrix[3][3];
//Enter info for the first matrix
cout<<"\tEnter the values for Matrix A in 3 by 3: "<<endl;
for (int i=0;i<=2;i++){
for (int j=0;j<=2;j++) {
cin>>matrixA[i][j];
}}
//Enter the info for the second matrix
cout<<"\n\nEnter the values for Matrix B in 3 by 3: "<<endl;
for (int i=0;i<=2;i++){
for (int j=0;j<=2;j++) {
cin>>matrixB[i][j];
}}
addMatrix(matrixC,matrixA,matrixB);
//This will add the two matrices together
// for (int i=0;i<=2;i++){
// for (int j=0;j<=2;j++) {
// matrixC[i][j]=matrixA[i][j]+matrixB[i][j];
// }
// }
//Displays the matrices
cout<<"\n\nMatrix A and Matrix B\n\n";
cout<<matrixA[0][0]<<"\t"<<matrixA[0][1]<<"\t"<<matrixA[0][2]<<"\n";
cout<<matrixA[1][0]<<"\t"<<matrixA[1][1]<<"\t"<<matrixA[1][2]<<"\n";
cout<<matrixA[2][0]<<"\t"<<matrixA[2][1]<<"\t"<<matrixA[2][2]<<"\n";
cout<<"and\n\n";
cout<<matrixB[0][0]<<"\t"<<matrixB[0][1]<<"\t"<<matrixB[0][2]<<"\n";
cout<<matrixB[1][0]<<"\t"<<matrixB[1][1]<<"\t"<<matrixB[1][2]<<"\n";
cout<<matrixB[2][0]<<"\t"<<matrixB[2][1]<<"\t"<<matrixB[2][2]<<"\n";
//Displays the addition of the matrices
cout<<"\n\nThe adding of the two matrice is\n\n";
cout<<matrixC[0][0]<<"\t"<<matrixC[0][1]<<"\t"<<matrixC[0][2]<<"\n";
cout<<matrixC[1][0]<<"\t"<<matrixC[1][1]<<"\t"<<matrixC[1][2]<<"\n";
cout<<matrixC[2][0]<<"\t"<<matrixC[2][1]<<"\t"<<matrixC[2][2]<<"\n";
//Mulitplying the matrices together
for (int i=0;i<=2;i++){
for (int j=0;j<=2;j++) {
multMatrix[i][j]=0;
for (int k=0; k<=2;k++){
multMatrix[i][j]+=matrixA[i][k]*matrixB[k][j];
}
}
}
//Displays the Product of the matrices
cout<<"\n\nThe product of the two matrice is\n\n";
cout<<multMatrix[0][0]<<"\t"<<multMatrix[0][1]<<"\t"<<multMatrix[0][2]<<"\n";
cout<<multMatrix[1][0]<<"\t"<<multMatrix[1][1]<<"\t"<<multMatrix[1][2]<<"\n";
cout<<multMatrix[2][0]<<"\t"<<multMatrix[2][1]<<"\t"<<multMatrix[2][2]<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}