Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy did I need to use this statement twice - Matrix Multiplication
    primarykey
    data
    text
    <p>I was developing a program to find matrix multiplication.</p> <pre><code>#include &lt;iostream&gt; using namespace std; int main() { int a=0,b=0,c=0,d=0,e=0; cout&lt;&lt;"Enter the order of the first matrix A \n\nNumber of Rows : "; cin&gt;&gt;a; cout&lt;&lt;"\nNumber of Columns : "; cin&gt;&gt;b; cout&lt;&lt;endl; int matrixA[a][b]; cout&lt;&lt;"Enter the matrix Elements "&lt;&lt;endl; for(int m=0; m&lt;a; m++) { for(int n=0; n&lt;b; n++) { cout&lt;&lt;"A ("&lt;&lt; m+1 &lt;&lt;" , "&lt;&lt;n+1&lt;&lt;" ) ="; cin&gt;&gt;matrixA[m][n]; //cout&lt;&lt;","; } cout&lt;&lt;endl; } ////////////////////////// Startup cout&lt;&lt;"Enter the order of the Second matrix A \n\nNumber of Rows : "&lt;&lt;b; c=b; cout&lt;&lt;"\nNumber of Columns : "; cin&gt;&gt;d; cout&lt;&lt;endl; int matrixB[c][d]; cout&lt;&lt;"Enter the matrix Elements "&lt;&lt;endl; for(int p=0; p&lt;c; p++) { for(int q=0; q&lt;d; q++) { cout&lt;&lt;"B ("&lt;&lt; p+1 &lt;&lt;" , "&lt;&lt;q+1&lt;&lt;" ) ="; cin&gt;&gt;matrixB[p][q]; //cout&lt;&lt;","; } cout&lt;&lt;endl; } ///////////// initialisting matrixAns int matrixAns[a][d]; for(int p=0; p&lt;a; p++) { for(int q=0; q&lt;d; q++) { matrixAns[p][q]=0; } } //////////////////// finding ans for(int r=0; r&lt;a; r++) { for(int s=0; s&lt;d; s++) { for(int t=0; t&lt;b; t++) { e = matrixA[r][t]*matrixB[t][s]; } matrixAns[r][s] = e+matrixAns[r][s]; matrixAns[r][s] = e+matrixAns[r][s]; //dont know why i have to add this same code again } } ////////////////////// Ans Printing cout&lt;&lt;"\nMatrix Multiplication Answer \n"&lt;&lt;endl; for(int h=0; h&lt;a; h++) { for(int i=0; i&lt;d; i++) { cout&lt;&lt;" "&lt;&lt;matrixAns[h][i]&lt;&lt;" "; } cout&lt;&lt;endl; } } </code></pre> <p>And one important thing: this is not my homework or assignment!</p> <p>The method that I have used works perfectly but it does not give me the right answer until I use this statement twice. (I got this by a trial and error method).</p> <pre><code>matrixAns[r][s] = e+matrixAns[r][s]; </code></pre> <p>I have also initailised the <code>matrixAns</code> by using a loop (and had set it to 0).</p> <p>I am new to C++ and want to know what was the error that I got and how the use of the two statements gives me correct answer. </p> <p>Is there any way to get rid of one of the statements without destroying the app?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    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