Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:50:55+00:00 2026-06-10T11:50:55+00:00

I was developing a program to find matrix multiplication. #include <iostream> using namespace std;

  • 0

I was developing a program to find matrix multiplication.

#include <iostream>

using namespace std;

int main()
{
    int a=0,b=0,c=0,d=0,e=0;
    cout<<"Enter the order of the first matrix  A  \n\nNumber of Rows : ";
    cin>>a;
    cout<<"\nNumber of Columns : ";
    cin>>b;
    cout<<endl;
    int matrixA[a][b];
    cout<<"Enter the matrix Elements "<<endl;
    for(int m=0; m<a; m++)
    {

        for(int n=0; n<b; n++)
        {
            cout<<"A ("<< m+1 <<" , "<<n+1<<" ) =";
            cin>>matrixA[m][n];
            //cout<<",";
        }
        cout<<endl;
    }

//////////////////////////  Startup

    cout<<"Enter the order of the Second matrix  A  \n\nNumber of Rows : "<<b;
    c=b;
    cout<<"\nNumber of Columns : ";
    cin>>d;
    cout<<endl;
    int matrixB[c][d];
    cout<<"Enter the matrix Elements "<<endl;
    for(int p=0; p<c; p++)
    {
        for(int q=0; q<d; q++)
        {
            cout<<"B ("<< p+1 <<" , "<<q+1<<" ) =";
            cin>>matrixB[p][q];
            //cout<<",";
        }
        cout<<endl;
    }

    ///////////// initialisting matrixAns
    int matrixAns[a][d];

    for(int p=0; p<a; p++)
    {
        for(int q=0; q<d; q++)
        {
            matrixAns[p][q]=0;    
        }    
    }

////////////////////  finding ans
    for(int r=0; r<a; r++)
    {
        for(int s=0; s<d; s++)
        {
            for(int t=0; t<b; t++)
            {    
                e = matrixA[r][t]*matrixB[t][s];       
            }
            matrixAns[r][s] = e+matrixAns[r][s];
            matrixAns[r][s] = e+matrixAns[r][s];   //dont know why i have to add this same code again
    }
    }

////////////////////// Ans Printing    
    cout<<"\nMatrix Multiplication Answer  \n"<<endl;
    for(int h=0; h<a; h++)
    {    
        for(int i=0; i<d; i++)
        {    
            cout<<" "<<matrixAns[h][i]<<" ";    
        }
        cout<<endl;
    }    
}

And one important thing: this is not my homework or assignment!

The method that I have used works perfectly but it does not give me the right answer until I use this statement twice. (I got this by a trial and error method).

matrixAns[r][s] = e+matrixAns[r][s];  

I have also initailised the matrixAns by using a loop (and had set it to 0).

I am new to C++ and want to know what was the error that I got and how the use of the two statements gives me correct answer.

Is there any way to get rid of one of the statements without destroying the app?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:50:57+00:00Added an answer on June 10, 2026 at 11:50 am

    You’re not doing the dot products correctly when calculating your answer. You need to multiply individual cells between the two matrices, then sum all those products for an individual cell in your answer.

    Your code is only taking the product, in e, of the last multiplication – matrix[r][b - 1] * matrixB[b - 1][s] – and discarding the first N-1 products. Adding e – this last multiplication – once, or twice or 3 times are all incorrect, though it may appear to work with certain inputs.

    Your answer loop, with comments:

    for(int r=0; r<a; r++)
    {
        for(int s=0; s<d; s++)
        {
            for(int t=0; t<b; t++)
            {
                e = matrixA[r][t]*matrixB[t][s];
            }
    
            // now e only has the value from that final multiplication, of
            //    matrix[r][b - 1] * matrixB[b - 1][s]. All of the other
            //    products were lost.
    
            // so now it doesn't matter how many times you add e, you'll
            //    get the wrong product:
            matrixAns[r][s] = e+matrixAns[r][s];
            matrixAns[r][s] = e+matrixAns[r][s];
        }
    }
    

    Change your answer loop to:

    for(int r=0; r<a; r++)
    {
        for(int s=0; s<d; s++)
        {
            for(int t=0; t<b; t++)
            {
                e = matrixA[r][t]*matrixB[t][s];
    
                // accumulation of the products should be INSIDE the loop:
                matrixAns[r][s] = matrixAns[r][s] + e;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a program where I find myself doing this like this a
I am developing some program in C# which will send the mail using outlook
I am currently developing my program using sample BluetoothChat program on android device. This
I'm developing a program using Windows 7. There are WCF services (soap, rest) that
I'm developing a program that load webpages using WebBrowser control in VB6. How do
I am developing a Java program using Eclipse that should exit with different codes
I am developing a java project in eclipse. In order to distribute the program
I have been developing a Java program in Netbeans/Ubuntu 12.04 using OpenJdk. I have
I find myself needing a string table in a Haskell program I'm developing. In
I've been trying to optimize the Windows program I am developing, trying to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.