Hi again, all---
I'm trying to write a function that will add two matrices and output the resulting matrix.
I've got two matrices already and everything, now i just need to write the function that i can use to call in my switch loop (The program must read two matrices, and the user can then input if they want to add, subtract, or multiply them, and then see their resultant matrix.)
So i've just pasted the function definition i'm trying to use.
double AddMatrix(double x[][5], double y[][5])
{
double matsum[5][5];
int row, col;
for(row=0; row<5; row++)
for(col=0; col<5; col++)
{
matsum[5][5]=(x[row][col]+y[row][col]);
}
return matsum[5][5];
}
In my little command window, it shows me an answer but it's completely wrong!! i don't know how to make a new matrix that adds and i'm kind of lost and frustrated...if someone could give me tips or a skeleton of a function that might work, i'd be much obliged! thanks!
Actually, I'll show you the entire code here, it's incomplete, but as soon as I know how to add (and therefore subtract and multiply) matrices I can fill it all in.
/*--------------------------------------------------------------------*/
/* Final Exam- Matrix Operations */
/* */
/* This program will take two data files, open them, read them, save */
/* their data in arrays, then let the user choose if they want to */
/* add, subtract, or multiply the matrices as many times as they want*/
/* until they choose the "quit" option, and show the results. */
/* */
#include <iostream> //Include the necessary libraries.
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
double AddMatrix(double x[][5], double y[][5]);
// Function Prototype(s) Will Go Here
//Begin main function
int main()
{
const int nrows=5; //Define the necessary constants
const int ncols=5;
//Define variables
double Matrix1Info[nrows][ncols];
double Matrix2Info[nrows][ncols];
char ch;
int row, col;
ifstream matrix, matrix2;
string filename, filename2;
// Ask user for name of input file 1.
cout << "Enter first input file: " <<endl;
cin >> filename;
// Open file and read the info.
matrix.open(filename.c_str());
if( matrix.fail() )
{
cout << "Error opening input file\n";
}
else
{
for (row=0; row<nrows; row++)
for (col=0; col<ncols; col++)
matrix >> Matrix1Info[row][col]; //Stores info into first matrix array
for(int r=0; r<5; r++)
{
for(int c=0; c<5; c++)
{
cout << Matrix1Info[r][c] << ' ';
}
cout << endl;
}
cout << "Enter second input file: " <<endl; //Gets 2nd matrix file
cin >> filename2;
matrix2.open(filename2.c_str()); //Opens 2nd file
if( matrix2.fail() )
{
cout << "Error opening input file\n";
}
else
{
for (row=0; row<nrows; row++)
for(col=0; col<ncols; col++)
matrix2 >> Matrix2Info[row][col]; //Stores info into 2nd matrix array
for(int f=0; f<5; f++)
{
for(int g=0; g<5; g++)
{
cout << Matrix2Info[f][g] << ' ';
}
cout << endl;
}
cout << "Enter A for addition, S for subtraction, M for multiplication, or Q to quit: " << endl;
cin >> ch;
while(ch != 'Q')
{
switch(ch)
{
case 'A':
cout << AddMatrix << endl;
cout << "Enter A for addition, S for subtraction, M for multiplication, or Q to quit: " <<endl;
case 'S':
//function for subtraction goes here
case 'M':
//function for multiplication goes here
break;
default:
cout << "Invalid operation choice." << endl;
cout << "Enter A for addition, S for subtraction, M for multiplication, or Q to quit: " <<endl;
}//end switch
cin >> ch;
}//end while loop
}
}
//End of main file.
return 0;
}
double AddMatrix(double x[][5], double y[][5])
{
double matsum[5][5];
int row, col;
const int nrows=5;
const int ncols=5;
for(row=0; row<5; row++)
for(col=0; col<5; col++)
{
matsum[nrows][ncols]=(x[row][col]+y[row][col]);
}
return matsum[5][5];
}