Hi i am new to C++ and my professor just threw us a curve ball code to do. i have to Develop a C++ program to model the mathematical notion of a matrix of integers wich im only using addition. He also asks that we use functions that should validate the matrix dimensions before performing the operations, as the dimensions of the matrices should be compatible for that operation. I have to using pointers for the 2D array, use pointer notation to access the value of any element in the 2D array. My program should not use the notation of [] but only use * operator for dereferencing.
well im only trying to get the dang addition code to work wothout pointers and im even getting trouble with that. i get the data and display it right on the compiler, but when the output is sent to my outfile.txt file it gives me the wrong numbers, i know my problem is the addMatrix funtion, here is my coding so far, pleas help.
CODE
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
using std::ifstream;
using std::ofstream;
using std::endl;
double AddMatrix(double x[][3], double y[][3]);
int main()
{
const int nrows = 3;
const int ncols = 3;
double instreamAInfo[nrows][ncols];
double instreamBInfo[nrows][ncols];
int row, col;
char ch;
ifstream instreamA;
ifstream instreamB;
ofstream outstream;
instreamA.open("infileA.txt");
for (row=0; row<nrows; row++)
for (col=0; col<ncols; col++)
instreamA >> instreamAInfo[row][col]; //stores info in instreamAInfo
for(int r=0; r<3; r++)
{
for(int c=0; c<3; c++)
{
cout << instreamAInfo[r][c] << ' ';
}
cout << endl;
}
cout << endl;
instreamB.open("infileB.txt");
for (row=0; row<nrows; row++)
for (col=0; col<ncols; col++)
instreamB >> instreamBInfo[row][col]; //sotres info in instreamBInfo
for(int r=0; r<3; r++)
{
for(int c=0; c<3; c++)
{
cout << instreamBInfo[r][c] << ' ';
}
cout << endl;
}
outstream.open("outfile.txt");
cout << "Enter A for addition or Q to quit: " << endl;
cin >> ch;
while(ch != 'Q')
{
switch(ch)
{
case 'A':
outstream << AddMatrix << endl;
break;
default:
cout << "Invalid operation choice." << endl;
cout << "Enter A for addition or Q to quit: " <<endl;
}//end switch
cin >> ch;
}
return 0;
}
double AddMatrix (double x[][3], double y [][3])
{
double matsum[3][3];
int row, col;
const int nrows = 3;
const int ncols = 3;
for (row = 0; row < 3; row++)
for (col=0; col < 3; col++)
{
matsum[nrows][ncols] = (x[row][col] + y[row][col]);
}
return matsum[3][3];
}