# include<iostream.h>
void main()

const int m=3;
const int n=4;
const int p=5; 
int A[m][n];
int B[n][p];
{

        cout<<"3x4 matrix";
        for(m=0; m<3; m++)
        {
            for(n=0; n<4; n++)
                cin>>A[m][n];
                cout<<A[m][n]<<"\t";
        }
        cout<<"\n";



        cout<<"4x5 matrix";
        for(n=0; n<3; n++)
        {
            for(p=0; p<5; p++)
                cin>>B[n][p];
                cout<<B[n][p]<<"\t";
        }
        cout<<"\n";


        int d, c=0;
        for(m=0; m<3; m++)
        {
            for(p=0; p<5; p++)
            {
                for(n=0; n<4; n++)
                d=A[m][n]*B[n][p];
                c=c+d;
            }
            cout<<c;
        }
        
}

Dani AI

Generated

correctly flagged the biggest compile-time problem: the same names used for size constants were being reused as loop counters. The snippet from also has several other faults that stop correct multiplication — an old header/void main, missing braces so inner loops only apply to the next statement, wrong loop limits when filling B, no dedicated result matrix, and the accumulator for each C element is never reset.

Key fixes, in short:

  • Use a proper signature and header (int main() and #include <iostream>).
  • Keep sizes and indices separate (e.g. const int M=3, N=4, P=5; and indices i,j,k).
  • Always brace nested loops so the body contains all intended statements.
  • Read A with bounds 0..M-1 and B with 0..N-1; the original used n<3 when n should run to 4.
  • For each output cell set sum = 0, accumulate sum += A[i][k]*B[k][j], then store C[i][j] = sum. Not resetting the accumulator yields wrong results.

A minimal corrected example (reads A then B, computes C, prints C):

#include <iostream>

int main() {
    const int M = 3, N = 4, P = 5;
    int A[M][N], B[N][P], C[M][P];

    for (int i = 0; i < M; ++i)
        for (int j = 0; j < N; ++j)
            std::cin >> A[i][j];

    for (int i = 0; i < N; ++i)
        for (int j = 0; j < P; ++j)
            std::cin >> B[i][j];

    for (int i = 0; i < M; ++i) {
        for (int j = 0; j < P; ++j) {
            int sum = 0;
            for (int k = 0; k < N; ++k)
                sum += A[i][k] * B[k][j];
            C[i][j] = sum;
            std::cout << C[i][j] << (j == P-1 ? '\n' : '\t');
        }
    }
    return 0;
}

Additional tips: compile with warnings enabled (e.g. -Wall) to catch accidental assignments to const, test with small known matrices (identity, all-ones) to confirm correctness, and switch to std::vector if sizes must be chosen at runtime.

Recommended Answers

All 2 Replies

Can you explain the problem?????

const int m=3;
const int n=4;
const int p=5; 
int A[m][n];
int B[n][p];

Put your initializations inside main if you are using it locally or above main(global usage) if it is used in more than one function

for(m=0; m<3; m++)

This will create problems for you as you have declared m as const. Same with n and p.

In this case use different variables for declaring array and indexing thru the array.

int A[m][n];
int B[n][p];

If you are declaring array like this then m, n, and p must be constant

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.