I’m having some issues passing vectors to functions. My concern is not with my logic itself, as if I need to adjust later I will. My program requirements state that I must have separate functions that build the matrices, print the final matrix, and ones to perform the desired mathematical operations. I’m not concerned with help on the math logic.
It seems that I have the “hard’ stuff down, for example, creating a vector of a vector, etc, but I’m having trouble passing the vectors to functions etc.
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
using std::vector;
void build();
void printMatrix(vector<vector<int> > );
int row=0, col=0;
vector<vector<int> > matrix(row, vector<int> (col) );
vector<vector<int> > matrix2(row, vector<int> (col) );
vector<vector<int> > matrix3(row, vector<int> (col) );
int main(){
build();
addMatrix();
printMatrix(matrix3);
return 0;
}
//====================================================
void build(){
//currently intended just to build 2x matrices of different increasing data
int k=0, l=5;
cout<<"Enter the number of rows for each Matrix: "<<endl;
cin>>row;
cout<<"Enter the number of columns for each Matrix: "<<endl;
cin>>col;
for( int i = 0; i < row; i++ ) {
for ( int j = 0; j < col; j++ ){
matrix[i][j] = k++;
matrix2[i][j] = l++;
}
}
I’m using Global Variables, because I want the Rows & Columns to stay the same and in the program, I’m only going to be able to call one of the mathematical functions at at time.
void printMatrix(vector<vector<int> > newMatrix3){
for ( int i = 0; i < row; i++ ) {
for ( int j = 0; j < col; j++ )
cout<< setw ( 3 ) << newMatrix3[i][j] <<' ';
cout<<'\n';
}
}
//=========================================
void addMatrix(){
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++)
matrix3[i][j]=(matrix[i][j]+matrix2[i][j]);
}
}
This program compiles 100% so if you see a syntax error it’s because my copy + paste messed up. As soon as I enter the dimensions for the matrix, the program crashes with a segmentation fault. I’m very new to C++, so this is very frustrating. I’m also all ears to take suggestions on style/best practice. I have the feeling that my use of global variables is not ideal….but I’m under instructions to make the arithmetic functions as re-usable as possible. Also, I don’t think I’m making the best use of functions.
Thank you.
Your global definitions of
row,col,matrix, … is the problem.What’s happening here is the following:
rowandcolis now0and therefore all your matrices now have 0 rows and columns.You can fix this by using the
vector::resize()function after you getrowandcolfrom the user.Also, this means you don’t have to “initialize” your
matrixobjects. So now you can just define them as:Note:
typedefto make your code look better.vectorand yourprintMatrixandaddMatrixfunctions can callvector::size()to find out the size of your matrix. You should rewrite those functions to take your matrix as an argument (lots of good advice here on that) and then work on them.