Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6042085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:45:04+00:00 2026-05-23T06:45:04+00:00

I’m having some issues passing vectors to functions. My concern is not with my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:45:05+00:00Added an answer on May 23, 2026 at 6:45 am

    Your global definitions of row,col,matrix, … is the problem.

    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) );
    

    What’s happening here is the following: row and col is now 0 and therefore all your matrices now have 0 rows and columns.

    You can fix this by using the vector::resize() function after you get row and col from the user.

    cout<<"Enter the number of rows for each Matrix: "<<endl;
    cin>>row;
    cout<<"Enter the number of columns for each Matrix: "<<endl;
    cin>>col;
    // Resize "matrix"
    matrix.resize(row);
    for(int i = 0; i < row; ++i) matrix[i].resize(col);
    // Repeat for "matrix2" and "matrix3"    
    

    Also, this means you don’t have to “initialize” your matrix objects. So now you can just define them as:

    vector<vector<int> > matrix;
    vector<vector<int> > matrix2;
    vector<vector<int> > matrix3;
    

    Note:

    1. Think about using typedef to make your code look better.
    2. You don’t need them to be global variables. You are using a vector and your printMatrix and addMatrix functions can call vector::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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have thousands of HTML files to process using Groovy/Java and I need to
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.