How can i pass 2 dimensional vectors to functions ??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<vector>
using namespace std;


void MatrixMulti( vector<vector<int> > A,int m,int n,vector<vector<int> > B,int p, vector<vector<int> > AB){

   for(int i=0;i<m;i++)
      for(int j=0;j<p;j++){

          int S=0;

          for(int l=0;l<n;l++){
          S+=A[i][l]*B[l][j];
          }

          AB[i][j]=S;
      }
}

int main(){
     int m,n,p;
     cin>>m>>n>>p;

     vector<vector<int> > A(m,vector<int>(n));
     vector<vector<int> > B(n,vector<int>(p));

     vector<vector<int> > AB(m,vector<int>(p));

     for(int i=0;i<m;i++)
      for(int j=0;j<n;j++){
          cin>>A[i][j];
      }

       for(int i=0;i<n;i++)
      for(int j=0;j<p;j++){
          cin>>B[i][j];
      }

    MatrixMulti(A,B,AB);

    for(int i=0;i<m;i++){
      for(int j=0;j<p;j++){
         cout<<AB[i][j]<<" ";
      }
    cout<<endl;
    }
}

// I am finding the multiplication of 2 matrices using vectors .
//Pls help !
Last edited on
Please edit your post and add code formatting
[code]
    {your code here}
[/code]


Because copying vectors is often expensive, it is suggested to pass vectors by reference.
If you don't need to change the vector's content, pass by const reference.

 
void MatrixMulti(const vector<vector<int> >& A, const vector<vector<int> >& B, vector<vector<int> >& AB) { ... }

would match
 
MatrixMulti(A, B, AB);

where AB serves as the output argument.
Last edited on

My program is still showing a bug :
 
error: cannot convert 'std::vector<std::vector<int> >' to 'int' for argument '2' to 'void MatrixMulti(std::vector<std::vector<int> >&, int, int, std::vector<std::vector<int> >&, int, std::vector<std::vector<int> >&)'|


Last edited on
Ohh great i got it .. thanks Ganado!
Topic archived. No new replies allowed.