954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Matrix Output I/O

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");
}
bookmark
Light Poster
44 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

I don't see any tests to verify that the files have been opened successfully. Do you know that the output file is actually being opened properly?
Example:

#include <iostream>
#include <fstream>

using std::cout;
using std::ofstream;

int main() {
  ofstream outFile("filename.txt", std::ios::out | std::ios::trunc); //declare the output file stream
  if (outFile.is_open())                    //test for successful open
    cout << "File opened successfully";     //report successful open
  else {
    cout << "Error opening file.";          //report error
    return 1;
  }

  for (int i = 0; i < 10; ++i) {
    outFile << i << " ";                    //test output to file
  }

  return 0;
}

NOTE:
You don't generally need both parts of the test. I've only done it this way for demonstration purposes. Generally something like this is sufficient:

if (!outFile.is_open()) {                   //test for successful open
  cout << "Error opening file.";          //report error
  return 1;
}

Notice the added "not" in the condition.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

When I run the program I input the input file which contains the matrix. Then I create an output file when the program prompts me. When I check to see if the output file is in the folder, I see it there but there is nothing in it. It's just blank. How do I test if it's being opened properly? thanks for your help.

bookmark
Light Poster
44 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Ahh ok. I'll add that. thanks again.

bookmark
Light Poster
44 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

I have tested it but it shows that the output file is being opened.

bookmark
Light Poster
44 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Oh, I see what's going on. What's wrong with your attempt to output your matrix here:
for(row =0; row < NUMBER_OF_ROWS; row++)
{
outfile << board[row][col] << sum/col;
}

What's missing? You're outputting amatrix.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

So my only problem is that board should be matrix?

bookmark
Light Poster
44 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

No. The problem is that you only have 1 loop that controls row. You forgot to add an inner ("nested") loop to control col.

As a result, all of your output is bogus.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

Ahh. thanks I didn't get what you were saying lol.

bookmark
Light Poster
44 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Ok. So now what I have is this code.

#include <iostream> 
#include <iomanip>	 
#include <fstream>
   
using namespace std;

const int NUMBER_OF_ROWS = 4;
const int NUMBER_OF_COLUMNS = 20;

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);

if(!outfile.is_open()){ 
	cerr << "ERROR IN OPENING FILE" << endl;
	exit(EXIT_FAILURE);
}

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; col < NUMBER_OF_COLUMNS; col++);
}

    sumRows(board, NUMBER_OF_ROWS);                 //Line 4
    cout << endl;                                   //Line 5

	for(row =0; row < NUMBER_OF_ROWS; row++)
		{
		for(col=0; col < NUMBER_OF_COLUMNS; col++)
	outfile << board[row][col];
	}
    return 0;
}
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];

    }
	system("pause");
}


I'm getting an output on my outputfile but it is giving me a lot of repeated number such as this

"-858993460-858993460-858993460-858993460"

bookmark
Light Poster
44 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
for(row = 0; row < NUMBER_OF_ROWS; row++)	{
  for(col = 0; col < NUMBER_OF_COLUMNS; col++);
}
 
sumRows(board, NUMBER_OF_ROWS);                 //Line 4
cout << endl;                                   //Line 5


Why did you do this to your input loop? This accomplishes absolutely nothing.

This version:
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;

was much better.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: