Note that there are some explanatory texts on larger screens.

plurals
  1. POMatrix Arithmetic using Vectors in C++ causing segmentation faults
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <pre><code>#include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;vector&gt; using namespace std; using std::vector; void build(); void printMatrix(vector&lt;vector&lt;int&gt; &gt; ); int row=0, col=0; vector&lt;vector&lt;int&gt; &gt; matrix(row, vector&lt;int&gt; (col) ); vector&lt;vector&lt;int&gt; &gt; matrix2(row, vector&lt;int&gt; (col) ); vector&lt;vector&lt;int&gt; &gt; matrix3(row, vector&lt;int&gt; (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&lt;&lt;"Enter the number of rows for each Matrix: "&lt;&lt;endl; cin&gt;&gt;row; cout&lt;&lt;"Enter the number of columns for each Matrix: "&lt;&lt;endl; cin&gt;&gt;col; for( int i = 0; i &lt; row; i++ ) { for ( int j = 0; j &lt; col; j++ ){ matrix[i][j] = k++; matrix2[i][j] = l++; } } </code></pre> <p>I'm using Global Variables, because I want the Rows &amp; 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.</p> <pre><code>void printMatrix(vector&lt;vector&lt;int&gt; &gt; newMatrix3){ for ( int i = 0; i &lt; row; i++ ) { for ( int j = 0; j &lt; col; j++ ) cout&lt;&lt; setw ( 3 ) &lt;&lt; newMatrix3[i][j] &lt;&lt;' '; cout&lt;&lt;'\n'; } } //========================================= void addMatrix(){ for(int i = 0; i &lt; row; i++){ for(int j = 0; j &lt; col; j++) matrix3[i][j]=(matrix[i][j]+matrix2[i][j]); } </code></pre> <p>}</p> <p>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 <em>very</em> 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. </p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload