I'm trying to add and print two matrices and get the sum by using 2-Dimensional Arrays.

I have to read in 18 numbers from a file.

My txt file includes:

1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 3

Currently, my program will come up, but I do not see anything on the screen. I get a popup error.

Here is my code:

#include <iostream>
#include <fstream>

using namespace std;

const int ROWS = 3;
const int COLS = 3;

int main ()
{

    ifstream inData;

    cout<<fixed<<showpoint;
    cout.precision(2);

    inData.open ("Test.txt");


    int firstmatrix[ROWS][COLS] = {1,1,1,1,1,1,1,1,1};
    int secondmatrix[ROWS][COLS] = {3,3,3,3,3,3,3,3,3};
    int sum;

    inData>>firstmatrix[ROWS][COLS]>>secondmatrix[ROWS][COLS];

    for (int i = 0; i<3; i++)
    {
        sum = 0;
        for (int j=0; j<3;j++)
        {

        sum = (firstmatrix[ROWS][COLS] + secondmatrix[ROWS][COLS]);

        cout<<firstmatrix[i][j]<<" + "<<secondmatrix[i][j]<<" = "<<sum<<endl;

        }
    }

    inData.close();

    system ("PAUSE");
    return 0;
}   

Recommended Answers

All 15 Replies

line 24 is junk since ROWS and COLS are not valid indexes for the matrixes. The same for line 32.

Try to get this working with hardcoded data before reading in data from a file, so comment out lines 12 through 16, line 24 and line 39.

Recompile and run.

I did the comment out, and it ran, but how do I get it to run properly with the code I have? What do I need to take out or replace to another location?

uncomment line 32 and replace ROWS and COLS with appropriate indexes. If ROWS == 3 by definition as declared on line 6 and you declare the array as having ROWS number of rows, like you do on line 20 and 21, then the largest valid index is ROWS minus 1, or 2. By using ROWS (and COLS) as you do in line 32 you overwrite the array and screw everything up, unless your compiler is kind enough to block you from doing so by freezing or not compiling. In line 32 you want the current row, i, not ROWS. A similar argument holds for COLS as well.

Thanks for the reply.

I did what you told me to do about the line, but I'm still getting an error when I run the program. Did I read the data into the file wrong or something?

Here is the updated code:

#include <iostream>
#include <fstream>

using namespace std;

const int ROWS = 3;
const int COLS = 3;

int main ()
{
    
    ifstream inData;
    
    cout<<fixed<<showpoint;
    cout.precision(2);
    
    inData.open ("Unknown.txt");
    
    
    int firstmatrix[ROWS][COLS] = {1,1,1,1,1,1,1,1,1};
    int secondmatrix[ROWS][COLS] = {3,3,3,3,3,3,3,3,3};
    int sum;
    
    inData>>firstmatrix[ROWS][COLS]>>secondmatrix[ROWS][COLS];
    
    for (int i = 0; i<3; i++)
    {
        sum = 0;
        for (int j=0; j<3;j++)
        {
        
        sum = (firstmatrix[i][j] + secondmatrix[i][j]);
        
        cout<<firstmatrix[i][j]<<" + "<<secondmatrix[i][j]<<" = "<<sum<<endl;
   
        }
    }
    
    inData.close();
    
    system ("PAUSE");
    return 0;
}

Line 24. is wrong. It has to be inside for-loop.
Look up for reading files

Thank you. I got it to print out, but my "ROWS" and "COLS" is not executing correctly. The way the numbers print out is the same as my file.

Input file:

1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 3

It prints out exactly the like the input file. I want the 1's and 3's to be printed as two different matrixs. How do I do this?

Here is the updated code:

#include <iostream>
#include <fstream>

using namespace std;

const int ROWS = 3;
const int COLS = 3;

int main ()
{

    ifstream inData;

    cout<<fixed<<showpoint;
    cout.precision(2);

    inData.open ("Unknown.txt");


    int firstmatrix[ROWS][COLS] = {1,1,1,1,1,1,1,1,1};
    int secondmatrix[ROWS][COLS] = {3,3,3,3,3,3,3,3,3};
    int sum;


    for (int i = 0; i<3; i++)
    {
        sum = 0;
        for (int j=0; j<3;j++)
        {

        inData>>firstmatrix[i][j]>>secondmatrix[i][j];

        sum = (firstmatrix[i][j] + secondmatrix[i][j]);

        cout<<firstmatrix[i][j];
        cout<<secondmatrix[i][j];
        }
     //   cout<<firstmatrix[i][j]<<" + "<<secondmatrix[i][j]<<" = "<<sum<<endl;


    }

    inData.close();

    system ("PAUSE");
    return 0;
}   

Line 24 is wrong for many reasons. First, the use of ROWS and COLS as indexes of arrays like this is wrong because they are not valid indexes. You are reading to memory you don't have control/authority over. It is a bad error, though a common one. If your program provides you with the output you would expect with lines 17, 24 and 39 commented out and using the data you hardcoded into the arrays on lines 20 and 21, then you can proceed with reading data from the file. How, you read the data from the file is going to depend on how the file is formatted. You should read the data from the file using a loop. But, you shouldn't use the loop you have to read the file. You should create another one specifically to accept data from the file. Once you have the data from the file, then you analyze/manipulate it using other loop(s). You will need to post a representative portion of the file to give us some idea of how it will need to be read. But don't bother with file input until you can do what you want using the data already in the arrays from the initialization step during declaration on lines 20 and 21.

If that's your input file then do this:

int firstmatrix[ROWS][COLS];
int secondmatrix[ROWS][COLS];
int i, j;
for(i = 0; i < ROWS; ++i)
  for(j = 0; j < COLS; ++j)
    inData >> firstmatrix[i][j];

for(i = 0; i < ROWS; ++i)
  for(j = 0; j < COLS; ++j)
    inData >> secondmatrix[i][j];

and display them using the same double loop format except replace inData with cout and adding a cout << '\n' at the appropriate time (HINT: after the inner loop has completed each time)

I read in the file, I'm having problems getting the ROWS and COLS to work in my print out.

You can't put them in the same for-loop!
(Because then what happens? First A[1,1] is printed, then B[1, 1]...)
You have to write two separate for loops each for it's own matrix

Do you want all 1s in firstmatrix and all 3s in secondmatrix like you had hardcoded in your first post, or is some 1s and some 3s in both firstmatrix and secondmatrix ok? If you want the former, then you have to pull the file read outside of the current loop. If you want the mixture, then you can leave it as is.

Thank you guys for replying.

My output printed out the way I want it to. The only thing is that I got it to print out vertically going down. Is there a way to make the results appear across? I tried to do /n different ways, but I was unable to get it that way.

This is how it currently appears...but I want it to appear horizontally. If I can't get horizontally, then it is fine the way it is. It is really no biggy, just a preference.

Here is it current results, but i would like to make it going across:

111
111
111
+
333
333
333
=
444
444
444

Here is the updated code:

#include <iostream>
#include <fstream>

using namespace std;

const int ROWS = 3;
const int COLS = 3;

int main ()
{

    ifstream inData;

    cout<<fixed<<showpoint;
    cout.precision(2);

    inData.open ("Unknown.txt");


    int firstmatrix[ROWS][COLS];
    int secondmatrix[ROWS][COLS];
    int sum;


    for (int i = 0; i<ROWS; i++)
    {
        for (int j=0; j<COLS;j++)
        {

        inData>>firstmatrix[i][j];

        cout<<firstmatrix[i][j];
        }
        cout<<endl;

    }

         cout<<"+ ";
         cout<<endl;

          for (int i = 0; i<ROWS; i++)
    {
        for (int j=0; j<COLS;j++)
        {
            inData>>secondmatrix[i][j];

            cout<<secondmatrix[i][j];

        }
        cout<<endl;
    }
       cout<<"="<<endl;

    for (int i = 0; i<ROWS; i++)
    {
        for (int j=0; j<COLS;j++)
        {

        sum = (firstmatrix[i][j] + secondmatrix[i][j]);

        cout<<sum;
        }
        cout<<""<<endl;
    }


       cout<<""<<endl; 


    inData.close();

    system ("PAUSE");
    return 0;
}   

It's not too hard, although a bit tricky. You have to print first line:
1 1 1, then tabb it, then 3 3 3, then tabb it, then 4 4 4 and then go into newline.

In second row instead of tabbing, you add signs + and =, and third row is the same as first. Try it

lol I couldn't get it to work

Frist I'd store the sums in a third matrix for later use.
You'd have 3 lines:

111    333    444
111 + 333 = 444
111    333    444

So use loops to get what you want when you want it.

maybe something like this:

for(i = 0; i < numLines; ++i)
  if(i != numLines/2)
    print first line of first matrix
    print three spaces
    print first line of second matrix
    print three spaces 
    print first line of result matrix
  else
    you figure out what to do different here
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.