Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to create a c++ program code that can multiply 3 matrices that read from data file but it cannot be run. Can someone tell me how to fix it

What I have tried:

C++
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int A[10][10],B[10][10],C[10][10],mul[10][10],res[10][10],r,c,i,j,k;
cout<<"enter the number of row=";
cin>>r;
cout<<"enter the number of column=";
cin>>c;
cout<<"enter the first matrix element=\n";
fstream f("data.txt");
f >> m >> n;
if ((m != 4) || (n != 3))
{
cout << "Matrix not 4 by 3!n";
return 1;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
f >> A[i][j];
}
cout<<"enter the second matrix element=\n";
fstream f("data.txt");
f >> m >> n;
if ((m != 4) || (n != 3))
{
cout << "Matrix not 4 by 3!n";
return 1;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
f >> B[i][j];
}
cout<<"enter the third matrix element=\n";
fstream f("data.txt");
cout << "Matrix not 4 by 3!n";
return 1;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
f >> C[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{ mul[i][j]=0;
for(k=0;k<c;k++)
{ mul[i][j]+=A[i][k]*B[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
res[i][j]=0;
for(k=0;k<c;k++)
{
res[i][j]+=C[i][k]*mul[k][j];
}
}
}
ofstream fout("result.txt");
fout << m << " " << n << "n";
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
fout << res[i][j];
}
fout << "n";
}
return 0;
}
Posted
Updated 28-Jun-21 21:40pm
v2
Comments
Greg Utas 28-Jun-21 11:12am    
I slightly edited your code, but you've got missing braces: several } without a preceding {. There's even a return in the middle! And you need to indent your code, because some people won't bother to look at it if they first have to reformat it to follow it. Please edit your code so that it accurately reflects the original. As it stands, it won't even compile.
Richard MacCutchan 28-Jun-21 11:17am    
You should also split your code into separate functions. Create one function that reads the data file and populates the matrix. You can then simply pass the address of any matrix into the function and it will populate the values. Better still, as this is C++, you can create the matrix dynamically and return the filled matrix to the caller.
SeeSharp2 28-Jun-21 11:34am    
Start by fixing any errors the compiler gives you. Or post what those errors are if you do not know how to fix them.

But just posting a bunch of code and asking someone else to fix it all is very rude.
CPallini 28-Jun-21 11:53am    
Could you please provide us an example of your input file?
Patrice T 28-Jun-21 13:51pm    
No need, a simple reindentation show the problem.
see my solution.

Quote:
but it cannot be run.

It is generally a good idea to give details on how it misbehave like error messages or unexpected results.
Not a direct correction, but an help.
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int A[10][10], B[10][10], C[10][10], mul[10][10], res[10][10], r, c, i, j, k;
    cout << "enter the number of row=";
    cin >> r;
    cout << "enter the number of column=";
    cin >> c;
    cout << "enter the first matrix element=\n";
    fstream f("data.txt");
    f >> m >> n;
    if ((m != 4) || (n != 3))
    {
        cout << "Matrix not 4 by 3!n";
        return 1;
    }
    for (int i = 0; i < m; i++)
        for (int j = 0; j < m; j++)
            f >> A[i][j];
}// here you see that there is 1 too many '}'
cout << "enter the second matrix element=\n";
fstream f("data.txt");
f >> m >> n;
if ((m != 4) || (n != 3))
{
    cout << "Matrix not 4 by 3!n";
    return 1;
}
for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++)
        f >> B[i][j];
}
cout << "enter the third matrix element=\n";
fstream f("data.txt");
cout << "Matrix not 4 by 3!n";
return 1;
}
for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++)
        f >> C[i][j];
}
}
for (i = 0; i < r; i++)
{
    for (j = 0; j < c; j++)
    {
        mul[i][j] = 0;
        for (k = 0; k < c; k++)
        {
            mul[i][j] += A[i][k] *B[k][j];
        }
    }
}
for (i = 0; i < r; i++)
{
    for (j = 0; j < c; j++)
    {
        res[i][j] = 0;
        for (k = 0; k < c; k++)
        {
            res[i][j] += C[i][k] *mul[k][j];
        }
    }
}
ofstream fout("result.txt");
fout << m << " " << n << "n";
for (int i = 0; i < m; i++)
{
    for (int j = 0; j < n; j++)
    {
        fout << res[i][j];
    }
    fout << "n";
}
return 0;
}

Indentation style - Wikipedia[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
 
Share this answer
 
v2
Comments
CPallini 28-Jun-21 16:02pm    
5. Good catch,
I think it is not the only problem of such a code.
For instance it checks the value of n and then it uses twice m as limit of the iterations.
Patrice T 28-Jun-21 16:13pm    
Thank you.
I did not even tried to understand the code once I found this error.
Apart from formatting and mismatched brackets, I see at least three major issues:

1. You use matrix size information from three different sources: (a) reading r and c from data.txt, (b) hard coded values (4 and 3) in your code, (c) reading values m and n (for each matrix) out of data.txt. All of these values must match for the calulation to make any sense, so you really have to make up your mind why data.txt contains these values (r and c, and m and n for each matrix), and why in your code you're using your own! If I were to make a guess, you should not use the hard coded values 4 and 3, but instead the values from data.txt.

2. Several of your nested loops run for i from 0 to m and j from 0 to m. These sholuld run from 0 to r and 0 to c respectively. By the way, if you were using meaningful variablenames rather than i, j, m or n, this would be much more obvious!

3. You can't multiply a 4x3 matrix with a 4x3 matrix: the number of columns in the first must correspond to the number of rows in the second matrix! Therefore it doesn't make sense to compare all matrix operands with the same row and column value!

I strongly suggest you look up matrix multiplication on Wikipedia or elsewhere and make sure your are interpreting and using the matrix operands correctly.
 
Share this answer
 
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900