Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

this code does not work... help!

Status
Not open for further replies.

OlesadeMontserrat

Programmer
Jul 20, 2000
2
GB
this is my code. The function identity at the end does not work. Could you tell me why.<br><br>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br><br>#define max 100<br><br>int t, rowA, rowB, colA, colB, rowR, colR, arrayA[max][max], arrayB[max][max], arrayR[max][max];<br><br>void INPUT (int t, int arrayA[][max], int arrayB[][max]);<br>void DISPLAY (int t, int arrayA[][max], int arrayB[][max], int arrayR[][max]);<br>void MENU ();<br>void ReENTER();<br>void ADDITION (int arrayR[][max]);<br>void SUBTRACTION (int arrayR[][max]);<br>void MULTIPLY (int arrayR[][max]);<br>void DETERMINANT ();<br>void TRANSPOSE (int arrayR[][max]);<br>void INVERSE (int arrayR[][max]);<br>void IDENTITY (int arrayR[][max]);<br><br>int main ()<br>{<br>&nbsp;&nbsp;int&nbsp;&nbsp;re, t, Nmatrix;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;printf(&quot;\n*************************************\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\n\nEnter number of matricies, 1 or 2\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\n*************************************\n&quot;);<br>&nbsp;&nbsp;scanf(&quot;%d&quot;,&Nmatrix);<br>&nbsp;&nbsp;for(t=1; t&lt; (Nmatrix+1); t++)<br>&nbsp;&nbsp;&nbsp;&nbsp;INPUT(t, arrayA, arrayB);<br>&nbsp;&nbsp;for(t=1; t&lt; (Nmatrix+1); t++)<br>&nbsp;&nbsp;&nbsp;&nbsp;DISPLAY (t, arrayA, arrayB, arrayR);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;MENU();<br>&nbsp;&nbsp;<br>}<br>/***********************************************************************/<br>void INPUT(int t, int arrayA[][max], int arrayB[][max])<br>{<br>&nbsp;&nbsp;int n,m,row,col;<br>&nbsp;&nbsp;printf(&quot;*************************************\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\n\nEnter array number %d\n\n&quot;,t);<br>&nbsp;&nbsp;printf(&quot;*************************************\n&quot;);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;printf(&quot;Enter number of columns\n&quot;);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;scanf(&quot;%d&quot;, &col);<br>&nbsp;&nbsp;fflush(stdin);&nbsp;&nbsp;&nbsp;&nbsp;/*flush the input stream in case of bad input*/<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;printf(&quot;Enter number of rows\n&quot;);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;scanf(&quot;%d&quot;, &row);<br>&nbsp;&nbsp;fflush(stdin);&nbsp;&nbsp;&nbsp;/* flush the input stream in case of bad input*/<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;for (n=0; n&lt;row; n++){<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;for (m=0; m&lt;col; m++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Enter element for row %d, column %d\n&quot;, n, m);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (t==1) {<br> scanf(&quot;%d&quot;, &arrayA[n][m]);<br> rowA = row; colA = col;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if (t==2){&nbsp;&nbsp;<br> scanf(&quot;%d&quot;, &arrayB[n][m]);<br> rowB = row; colB = col;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>}<br><br>/************************************************************************/<br>/************************************************************************/<br><br>void DISPLAY (int t, int arrayA[][max], int arrayB[][max], int arrayR[][max])<br>{<br>&nbsp;&nbsp;int n, m, k, N, rowI, colI, array[max][max];<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (t==1){<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\nThis is matrix number %d&quot;, t);<br>&nbsp;&nbsp;&nbsp;&nbsp;for (n=0; n&lt;rowA; n++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (m=0; m&lt;colA; m++)<br> printf(&quot;%d\t&quot;, arrayA[n][m]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (t==2){<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\nThis is matrix number %d&quot;, t);<br>&nbsp;&nbsp;&nbsp;&nbsp;for (n=0; n&lt;rowB; n++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (m=0; m&lt;colB; m++)<br> printf(&quot;%d\t&quot;, arrayB[n][m]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (t==3){<br>&nbsp;&nbsp;&nbsp;&nbsp;for (n=0; n&lt;rowR; n++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (m=0; m&lt;colR; m++)<br> printf(&quot;%d\t&quot;, arrayR[n][m]);<br>&nbsp;&nbsp;&nbsp;&nbsp;} <br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\nThis is matrix number %d, it will be over written next time a calculation is performed.&quot;,t);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;This matrix can replace the perminat matricies(1 and 2) by selecting number 1 from the menu\n&quot;);<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;printf(&quot;\n&quot;);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;/*display the product of two matrices*/<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (t==4){<br>&nbsp;&nbsp;&nbsp;&nbsp;for (n=0; n&lt;rowA; n++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\n&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*Spacing between the lines*/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (m=0; m&lt;colB; m++){ <br> printf(&quot;\t&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*spacing between columns*/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> for (k=0; k&lt;colR; k++)<br> &nbsp;&nbsp;printf(&quot;\n\n&quot;);<br> printf(&quot;%d\t&quot;, arrayR[n][m]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\nThis is matrix number 3, it will be over written next time a calculation is performed.&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;This matrix can replace the perminat matricies(1 and 2) by selecting number 1 from the menu\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;/*t=display*/<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;/*display the transpose of matrix A*/<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (t==5)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(n=0; n&lt;rowA; n++)<br> {<br> &nbsp;&nbsp;for(m=0; m&lt;colA; m++)<br> &nbsp;&nbsp;&nbsp;&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrayR[m][n]=arrayA[n][m];<br> &nbsp;&nbsp;&nbsp;&nbsp;}<br> }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\t\n&quot;); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(n=0; n&lt;rowA; n++)<br> {<br> &nbsp;&nbsp;printf(&quot;\t&quot;);<br> &nbsp;&nbsp;for(m=0; m&lt;colA; m++)<br> &nbsp;&nbsp;&nbsp;&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\t&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\t%d&quot;, arrayR[n][m]);<br> &nbsp;&nbsp;&nbsp;&nbsp;}<br> &nbsp;&nbsp;printf(&quot;\n&quot;);<br> &nbsp;&nbsp;printf(&quot;\n\t\n&quot;); <br> }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\nThis is matrix number 3, it will be over written next time a calculation is performed.&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;This matrix can replace the perminat matricies(1 and 2) by selecting number 1 from the menu\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*t=display*/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;/*display the transpose of matrix B*/<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;if (t==6)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;for(n=0; n&lt;rowB; n++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(m=0; m&lt;colB; m++)<br> {<br> &nbsp;&nbsp;arrayR[m][n]=arrayB[n][m];<br> }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\t\n&quot;); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(n=0; n&lt;rowB; n++)<br> {<br> &nbsp;&nbsp;printf(&quot;\t&quot;);<br> &nbsp;&nbsp;for(m=0; m&lt;colB; m++)<br> &nbsp;&nbsp;&nbsp;&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\t&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\t%d&quot;, arrayR[n][m]);<br> &nbsp;&nbsp;&nbsp;&nbsp;}<br> &nbsp;&nbsp;printf(&quot;\n&quot;);<br> &nbsp;&nbsp;printf(&quot;\n\t\n&quot;); <br> }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\nThis is matrix number 3, it will be over written next time a calculation is performed.&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;This matrix can replace the perminat matricies(1 and 2) by selecting number 1 from the menu\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*t=display*/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;/*display the identity matrix*/<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (t==7)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(n=0; n&lt;rowR; n++)<br> {<br> &nbsp;&nbsp;for(m=0; m&lt;colR; m++)<br> &nbsp;&nbsp;&nbsp;&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (n==m)<br> arrayR[n][m]=array[1][1];<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (n!=m)<br> &nbsp;&nbsp;&nbsp;&nbsp;arrayR[n][m]=array[0][0];<br> &nbsp;&nbsp;&nbsp;&nbsp;}<br> }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\t%d&quot;, arrayR[n][m]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;printf(&quot;\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\n\nThis is matrix number 3, it will be over written next time a calculation is performed.&quot;);<br>&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;This matrix can replace the perminat matricies(1 and 2) by selecting number 1 from the menu\n&quot;);<br>&nbsp;&nbsp;/*t=display*/<br>&nbsp;&nbsp;printf(&quot;\n&quot;);<br>}<br>/*******************************************************************/<br>/********************************************************************/<br><br>void MENU ()<br>{<br>&nbsp;&nbsp;int m;<br>&nbsp;&nbsp;printf(&quot;\n\n=================================================\n&quot;);<br>&nbsp;&nbsp;printf(&quot;What would you like to do?\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\t1:\tReplace a matrix\n&quot;);&nbsp;&nbsp;/*try slashf*/<br>&nbsp;&nbsp;printf(&quot;\t2:\tDo an addition\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\t3:\tDo a subtraction\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\t4:\tDo a multiplication\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\t5:\tCalculate a determinant\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\t6:\tCalculate the transposed matrix\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\t7:\tCalculate the inverse matrix\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\t8:\tDo you want to view the identity matrix\n&quot;);<br>&nbsp;&nbsp;printf(&quot;\t9:\tExit\n&quot;);<br>&nbsp;&nbsp;printf(&quot;=================================================\n&quot;);<br>&nbsp;&nbsp;scanf(&quot;%d&quot;, &m);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (m==1) ReENTER();<br>&nbsp;&nbsp;if (m==2) ADDITION(arrayR);<br>&nbsp;&nbsp;if (m==3) SUBTRACTION (arrayR);<br>&nbsp;&nbsp;if (m==4) MULTIPLY (arrayR);<br>&nbsp;&nbsp;if (m==6) TRANSPOSE (arrayR);<br>&nbsp;&nbsp;if (m==8) IDENTITY (arrayR);<br>}<br><br>/************************************************************/<br>/*************************************************************/<br><br>void ReENTER ()<br>{<br>&nbsp;&nbsp;int new, n, m;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;printf(&quot;Would you like to:\n&quot;);<br>&nbsp;&nbsp;printf(&quot;1:\tRe-enter a matrix\n&quot;);<br>&nbsp;&nbsp;printf(&quot;2:\tReplace a matrix with a matrix just calculated\n&quot;);<br>&nbsp;&nbsp;scanf(&quot;%d&quot;, &new);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (new == 1){<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Which matrix would you like to re-enter?\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(&quot;%d&quot;,&t);<br>&nbsp;&nbsp;&nbsp;&nbsp;INPUT(t, arrayA, arrayB);<br>&nbsp;&nbsp;&nbsp;&nbsp;DISPLAY (t, arrayA, arrayB, arrayR);<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (new == 2){<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Which matrix do you want to be replaced?\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(&quot;%d&quot;, &t);<br>&nbsp;&nbsp;&nbsp;&nbsp;if (t==1){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (n=0; n&lt;rowR; n++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (m=0; m&lt;colR; m++)<br> arrayA[n][m] = arrayR[n][m];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;if (t==2){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (n=0; n&lt;rowR; n++){<br> for (m=0; m&lt;colR; m++)<br> arrayB[n][m] = arrayR[n][m];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;MENU();<br>}<br><br><br>/**************************************************************/<br>/***************************************************************/<br><br>void ADDITION (int arrayR[][max])<br>{<br>&nbsp;&nbsp;int n,m;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if ( (rowA != rowB) ¦¦ (colA != colB) ){<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\nThe two matricies are not of the same size!\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;MENU ();<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;t=3;<br>&nbsp;&nbsp;rowR = rowA; colR = colA;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;for (n=0; n&lt;rowR; n++){<br>&nbsp;&nbsp;&nbsp;&nbsp;for (m=0; m&lt;colR; m++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrayR[n][m] = arrayA[n][m] + arrayB[n][m];<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;printf(&quot;This is the resulting matrix:\n&quot;);<br>&nbsp;&nbsp;DISPLAY (t, arrayA, arrayB, arrayR);<br>&nbsp;&nbsp;MENU ();<br>&nbsp;&nbsp;<br>}<br><br>/**************************************************************/<br>/****************************************************************/<br><br>void SUBTRACTION (int arrayR[][max])<br>{<br>&nbsp;&nbsp;int sub,n,m;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if ( (rowA != rowB) ¦¦ (colA != colB) ){<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\nThe two matricies are not of the same size!\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;MENU ();<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;t=3;<br>&nbsp;&nbsp;rowR = rowA; colR = colA; <br>&nbsp;&nbsp;<br>&nbsp;&nbsp;printf(&quot;Would you like to:\n&quot;);<br>&nbsp;&nbsp;printf(&quot;1:\tSubtract matrix 1 from 2\n&quot;);<br>&nbsp;&nbsp;printf(&quot;2:\tSubtract matrix 2 from 1\n&quot;);<br>&nbsp;&nbsp;scanf(&quot;%d&quot;,&sub);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (sub==1){<br>&nbsp;&nbsp;&nbsp;&nbsp;for (n=0; n&lt;rowR; n++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (m=0; m&lt;colR; m++)<br> arrayR[n][m] = arrayA[n][m] -&nbsp;&nbsp;arrayB[n][m];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (sub==2){<br>&nbsp;&nbsp;&nbsp;&nbsp;for (n=0; n&lt;rowR; n++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (m=0; m&lt;colR; m++)<br> arrayR[n][m] = arrayB[n][m] -&nbsp;&nbsp;arrayA[n][m];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;MENU ();<br>}<br>/***********************************************************************************/<br>/**********************************************************************************/<br><br> /* Multiply matrix A by matrix B, storing the&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br> * product in matrix R, where all three matrices are&nbsp;&nbsp;&nbsp;&nbsp;*<br> * a by a in size:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br> *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a-1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br> *&nbsp;&nbsp;&nbsp;R[ n ][ m ] = SUM m1[ n ][ k ] * m2[ k ][ m ]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br> *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k=0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br> * and 0 &lt;= n,m &lt; a.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br><br>void MULTIPLY (int arrayR[][max])<br>{<br>&nbsp;&nbsp;int mul, m,n, k, colR;<br><br>&nbsp;&nbsp;if (colA != rowB){<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n Can multiply the matrices because the number of columns in A different to the number of rows in B\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;MENU ();<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;t=4;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;colR=colA; colR=rowB;<br>&nbsp;&nbsp;printf(&quot;\n\n%d\n\n&quot;, colR);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;for ( n = 0; n &lt; rowA; n++ )<br>&nbsp;&nbsp;&nbsp;&nbsp;for ( m= 0; m &lt; colB; m++ ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrayR[n][m ] = 0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ( k = 0; k &lt; colR; k++ ){<br> arrayR[n][m] += arrayA[n][k] * arrayB[k][m];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;printf(&quot;This is the resulting matrix:\n&quot;);<br>&nbsp;&nbsp;DISPLAY (t, arrayA, arrayB, arrayR);<br>&nbsp;&nbsp;MENU ();<br><br><br>}<br><br>/****************************************************************************************************/<br>/****************************************************************************************************/<br><br>void TRANSPOSE (int array[][max])<br>{<br>&nbsp;&nbsp;int n, m,k, trans;<br>&nbsp;<br><br>&nbsp;&nbsp;printf(&quot;Would you like to:\n&quot;);<br>&nbsp;&nbsp;printf(&quot;1:\tFind the transpose of matrix A\n&quot;);<br>&nbsp;&nbsp;printf(&quot;2:\tFind the transpose of matrix B\n&quot;);<br>&nbsp;&nbsp;scanf(&quot;%d&quot;, &trans);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;t=5;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;if (trans == 1){<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\nYou will calculate the transpose of matrix A\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;for(n=0; n&lt;rowA; n++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(m=0; m&lt;colA; m++)<br> arrayR[m][n] = arrayA[n][m];<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;This is the resulting matrix:\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;DISPLAY(t, arrayA, arrayB, arrayR);<br>&nbsp;&nbsp;&nbsp;&nbsp;MENU();<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;t=6;<br>&nbsp;&nbsp;if (trans == 2){<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n\nYou will calculate the transpose of matrix B\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;for(n=0; n&lt;rowB; n++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(m=0; m&lt;colB; m++)<br> arrayR[m][n] = arrayB[n][m];<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;This is the resulting matrix:\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;DISPLAY(t, arrayA, arrayB, arrayR);<br>&nbsp;&nbsp;&nbsp;&nbsp;MENU();<br>&nbsp;&nbsp;}<br><br>}<br><br>/*************************************************************************************/<br>/************************************************************************************/<br><br>/*VIEW THE IDENTITY MATRIX*/<br><br><br><br>void IDENTITY (int array[][max])<br>{<br>&nbsp;&nbsp;int n, m,k, colI, rowI, arrayI[max][max];<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;printf(&quot;Enter number of columns\n&quot;);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;scanf(&quot;%d&quot;, &colR);<br>&nbsp;&nbsp;fflush(stdin);&nbsp;&nbsp;&nbsp;&nbsp;/*flush the input stream in case of bad input*/<br>&nbsp;&nbsp;printf(&quot;Enter number of rows\n&quot;);<br>&nbsp;&nbsp;scanf(&quot;%d&quot;, &rowR);<br>&nbsp;&nbsp;fflush(stdin);&nbsp;&nbsp;&nbsp;/* flush the input stream in case of bad input*/<br>&nbsp;&nbsp;printf(&quot;\nthis is rowR %d\n&quot;, rowR);<br>&nbsp;&nbsp;printf(&quot;\nthis is colR %d\n&quot;,colR);<br>&nbsp;&nbsp;<br>&nbsp;&nbsp;t=7;<br><br>&nbsp;&nbsp;<br>&nbsp;&nbsp;for(n=0; n&lt;rowR; n++){<br>&nbsp;&nbsp;&nbsp;&nbsp;for(m=0; m&lt;colR; m++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (n==m) {<br> <br> arrayR[n][m] = array[1][1];;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (n!=m)<br> arrayR[n][m] = array[0][0];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\tarray1 is%d\n&quot;, arrayR[n][m]);<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;} <br>&nbsp;&nbsp;printf(&quot;\tarray2 is%d\n&quot;, arrayR[n][m]);<br>&nbsp;&nbsp;printf(&quot;This is the resulting matrix:\n&quot;); <br>&nbsp;&nbsp;DISPLAY(t, arrayA, arrayB, arrayR);<br>&nbsp;&nbsp;MENU();<br>&nbsp;&nbsp;<br>}<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
 
i think normally you would fflush the io streams before you get data.<br><br>&nbsp;&nbsp;fflush(stdin);&nbsp;&nbsp;&nbsp;&nbsp;/*flush the input stream in case of bad input*/ <br>&nbsp;&nbsp;scanf(&quot;%d&quot;, &colR);&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;printf(&quot;Enter number of rows\n&quot;);<br>&nbsp;&nbsp;fflush(stdin);&nbsp;&nbsp;&nbsp;/* flush the input stream in case of bad input*/<br>&nbsp;&nbsp;scanf(&quot;%d&quot;, &rowR);<br>&nbsp;&nbsp;<br>but this is just a wild guess.... presently i ma not around a C compiler to test the prog...but try this. if it is a prob with fflush().<br><br>
 
Hey Buddy,<br>&nbsp;&nbsp;Didnt get the code of Ur identity matrix at all.<br>U have written<br>----<br>arrayR[n][m] = array[1][1];;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (n!=m)<br>arrayR[n][m] = array[0][0];<br>---------<br>U havent initialised the array 'array' so all the values are 0 and U dont want that modify the code to<br>----<br>arrayR[n][m] = 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (n!=m)<br>arrayR[n][m] = 0;<br><br>thanks<br>amit <p> amit<br><a href=mailto: crazy_indian@mailcity.com> crazy_indian@mailcity.com</a><br><a href= > </a><br>to bug is human to debug devine
 
Identity matrix is one in which whose rows and columns
are equal the value is 1 otherwise 0.
For Identity matrix code is
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
if (i==j)
a[j]=1;
else
a[j]=0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top