简体   繁体   中英

Can anyone explain why am i getting “expression must have pointer to object type” in the following code:

#include<iostream>
#include<conio.h>
#include<array>

using namespace std;
int main(){
    system("cls");
    int n, l, m;
    int a[100][100], b[100][100], m[100][100];
    //inputing
    cout<<"For the multiplication of the matrices NxL & LxM: \n";
    cout<<"Enter N: ";
    cin>>n;
    cout<<"Enter L: ";
    cin>>l;
    cout<<"Enter M: ";
    cin>>m;
    cout<<"Enter the first matrix: ";
    for(int i=0; i<n; i++){
        for(int j=0; j<l; j++){
            cout<<i<<":";
            cin>>a[i][j];
        }
        cout<<"         \n";
    }
    cout<<"Enter the second matrix: ";
    for(int i=0; i<l; i++){
        for(int j=0; j<m; j++){
            cout<<i<<":";
            cin>>a[i][j];
        }
        cout<<"         \n";
    }
    //multiplying
    for(int i=0; i<n; i++)
        for(int j=0; j<l; j++){
            m[i][j] = a[i][j]*b[j][i]);
        }
    }
    getch();
    system("cls");
    return 0;
}

I am trying to multiply two matrices in this code, but this statement is creating a major problem. Please tell how to solve this error. it is giving me error in the line m[i][j] = a[i][j]*b[j][i]);

Your code previously had few minor bugs, like missing } . Here I have corrected it.

#include<iostream>
#include<array>
#include<conio.h>

using namespace std;
int main(){
    system("cls");
    int n, l, m;
    int a[100][100], b[100][100], ar[100][100];
    //inputing
    cout<<"For the multiplication of the matrices NxL & LxM: \n";
    cout<<"Enter N: ";
    cin>>n;
    cout<<"Enter L: ";
    cin>>l;
    cout<<"Enter M: ";
    cin>>m;
    cout<<"Enter the first matrix: ";
    for(int i=0; i<n; i++){
        for(int j=0; j<l; j++){
            cout<<i<<":";
            cin>>a[i][j];
        }
        cout<<"         \n";
    }
    cout<<"Enter the second matrix: ";
    for(int i=0; i<l; i++){
        for(int j=0; j<m; j++){
            cout<<i<<":";
            cin>>a[i][j];
        }
        cout<<"         \n";
    }
    //multiplying
    for(int i=0; i<n; i++){
        for(int j=0; j<l; j++){
            ar[i][j] = a[i][j]*b[j][i];
        }
    }
    getch();
    system("cls");
    return 0;
}

Things I have changed:-

  1. Added { at line no 35
  2. Changed the name of the third array. Where the result is to be stored. Actually, there was an ambiguity due to the name of the variable and the 2-d array being the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM