Matrix Arithmetic using Vectors in C++ causing segmentation faults

后端 未结 7 1199
执笔经年
执笔经年 2021-01-15 02:37

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

7条回答
  •  执笔经年
    2021-01-15 02:39

    You never add the elements to the matrices, ie. matrix and matrix2 are empty when you call build. You need to size the matrix after your receive the users input.

    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: "<>row;
    cout<<"Enter the number of columns for each Matrix: "<>col;
    matrix.resize(row);
    matrix2.resize(row);
    for( int i = 0; i < row; i++ ) {
        matrix[i].resize(col, 0);
        matrix2[i].resize(col, 0);
        for ( int j = 0; j < col; j++ ){
            matrix[i][j] = k++;
            matrix2[i][j] = l++;
        }
    }
    

提交回复
热议问题