Two vectors are created and values are assigned to both the vectors and then the two vectors are multiplied with each other. (A matrix multiplication). Gives a segmentation fault error in the multiply function. Is it something to with trying to access an location out of the scope?
#include<iostream>
#include <vector>
using namespace std;
int n;
vector <int> mat1Rows;
vector <int> mat2Rows;
vector <int> multRows;
vector <vector<int> > mat1;
vector <vector<int> > mat2;
vector <vector<int> > multMat;
void assignValues(int num){
for(int i = 0; i < num; i++){
for(int j = 0; j < num; j++){
mat1Rows.push_back(j);
mat2Rows.push_back(j);
}
mat1.push_back(mat1Rows);
mat2.push_back(mat2Rows);
mat1Rows.clear();
mat2Rows.clear();
}
}
void multiply(int n){
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
for(int k = 0; k < n; ++k){
multMat[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
void displayMult(int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << multMat[i][j] << " " ;
}
cout << endl;
}
}
int main(){
cout << "Enter the size of the matrix: ";
cin >> n;
assignValues(n);
multiply(n);
displayMult(n);
return 0;
}