I need to construct a C++ program where I input from the screen the entries of two 3x3 matrices A and B. My program then computes A-5B. The matrices should be declared as two dimensional arrays.
What I have so far
#include <iostream>
using namespace std;
//Define the Matrix3X3 type.
typedef struct
{
float index[3][3];
} Matrix3X3;
//Function prototypes (functions are defined below main())
void printMatrix( Matrix3X3 a );
Matrix3X3 inputMatrix();
Matrix3X3 addMatrices( Matrix3X3 a, Matrix3X3 b );
Matrix3X3 subtractMatrices( Matrix3X3 a, Matrix3X3 b );
Matrix3X3 scalarMultiply( Matrix3X3 a, float scale );
//main() function
void main()
{
cout << "Please enter matrix A:" << endl;
Matrix3X3 A = inputMatrix();
cout << "Matrix A:" << endl;
printMatrix( A );
cout << "Please enter matrix B:" << endl;
Matrix3X3 B = inputMatrix () ;
cout << "Matrix B:" <
printMatrix ( B );
//The result I want is A - 5B.
//I can hold the result in a new matrix and print it out to complete the program.
}
void printMatrix( Matrix3X3 a )
{
//loop through rows
for( int i=0; i < 3; ++i )
{
//loop through column at each row
for( int j=0; j < 3; ++j )
{
//i is the row
//j is the column
float temp = a.index[i][j];
cout << temp << " ";
}
//go to the next line after each row
cout << endl;
}
}
Matrix3X3 inputMatrix()
{
Matrix3X3 temp;
//loop through rows
for( int i=0; i < 3; ++i )
{
//loop through column at each row
for( int j=0; j < 3; ++j )
{
//i is the row
//j is the column
cout << "Enter row " << i << " column " << j << ":" << endl;
cin >> temp.index[i][j];
}
}
return temp;
}
How do I
1) Use the functions to calculate the result
2) And define the rest of the matrix.
Thanks