Hi, I'm trying to get this program to read a file which contains a matrix.
I am trying to output a text file which has the matrix of the input file but with the average of each row. Every time I enter the name of the inputFile and the outputFile I get an outputFile with nothing on it. Can someone take a look at what I'm doing wrong here? Thanks in advanced, bookmark.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int NUMBER_OF_ROWS = 9;
const int NUMBER_OF_COLUMNS = 9;
void printMatrix(int matrix[][NUMBER_OF_COLUMNS],
int NUMBER_OF_ROWS);
void sumRows(int matrix[][NUMBER_OF_COLUMNS],
int NUMBER_OF_ROWS);
int main()
{
ifstream infile;
ofstream outfile;
char inputFile[51];
char outputFile[51];
cout << "Enter the input file name: ";
cin >> inputFile;
cout << endl;
infile.open(inputFile);
cout << "Enter the output file name: ";
cin >> outputFile;
cout << endl;
outfile.open(outputFile);
int sum;
int col;
int row;
int board[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
for(row = 0; row < NUMBER_OF_ROWS; row++)
for(col = 0; row < NUMBER_OF_COLUMNS; col++)
infile >> board[row][col];
printMatrix(board, NUMBER_OF_ROWS); //Line 2
cout << endl; //Line 3
sumRows(board, NUMBER_OF_ROWS); //Line 4
cout << endl; //Line 5
for(row =0; row < NUMBER_OF_ROWS; row++)
{
outfile << board[row][col] << sum/col;
}
return 0;
}
void printMatrix(int matrix[][NUMBER_OF_COLUMNS],
int noOfRows)
{
int row, col;
for (row = 0; row < noOfRows; row++)
{
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
cout << setw(5) << matrix[row][col] << " ";
cout << endl;
}
}
void sumRows(int matrix[][NUMBER_OF_COLUMNS], int noOfRows)
{
int row, col;
int sum;
//Sum of each individual row
for (row = 0; row < noOfRows; row++)
{
sum = 0;
for (col = 0; col < NUMBER_OF_COLUMNS; col++)
sum = sum + matrix[row][col];
cout << "Sum of row " << (row + 1) << " = " << sum
<< endl;
}
system("pause");
}