OlesadeMontserrat
Programmer
this is my code. The function identity at the end does not work. Could you tell me why.<br><br>#include<stdio.h><br>#include<stdlib.h><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> int re, t, Nmatrix;<br> <br> printf("\n*************************************\n"<br> printf("\n\nEnter number of matricies, 1 or 2\n"<br> printf("\n*************************************\n"<br> scanf("%d",&Nmatrix);<br> for(t=1; t< (Nmatrix+1); t++)<br> INPUT(t, arrayA, arrayB);<br> for(t=1; t< (Nmatrix+1); t++)<br> DISPLAY (t, arrayA, arrayB, arrayR);<br> <br> MENU();<br> <br>}<br>/***********************************************************************/<br>void INPUT(int t, int arrayA[][max], int arrayB[][max])<br>{<br> int n,m,row,col;<br> printf("*************************************\n"<br> printf("\n\nEnter array number %d\n\n",t);<br> printf("*************************************\n"<br> <br> printf("Enter number of columns\n"<br> <br> scanf("%d", &col);<br> fflush(stdin); /*flush the input stream in case of bad input*/<br> <br> <br> printf("Enter number of rows\n"<br> <br> scanf("%d", &row);<br> fflush(stdin); /* flush the input stream in case of bad input*/<br> <br> for (n=0; n<row; n++){<br> printf("\n\n"<br> for (m=0; m<col; m++){<br> printf("Enter element for row %d, column %d\n", n, m);<br> if (t==1) {<br> scanf("%d", &arrayA[n][m]);<br> rowA = row; colA = col;<br> }<br> else if (t==2){ <br> scanf("%d", &arrayB[n][m]);<br> rowB = row; colB = col;<br> }<br> }<br> }<br> <br>}<br><br>/************************************************************************/<br>/************************************************************************/<br><br>void DISPLAY (int t, int arrayA[][max], int arrayB[][max], int arrayR[][max])<br>{<br> int n, m, k, N, rowI, colI, array[max][max];<br> <br> if (t==1){<br> printf("\n\nThis is matrix number %d", t);<br> for (n=0; n<rowA; n++){<br> printf("\n\n"<br> for (m=0; m<colA; m++)<br> printf("%d\t", arrayA[n][m]);<br> } <br> printf("\n"<br> }<br> <br> if (t==2){<br> printf("\n\nThis is matrix number %d", t);<br> for (n=0; n<rowB; n++){<br> printf("\n\n"<br> for (m=0; m<colB; m++)<br> printf("%d\t", arrayB[n][m]);<br> }<br> } <br> <br> if (t==3){<br> for (n=0; n<rowR; n++){<br> printf("\n\n"<br> for (m=0; m<colR; m++)<br> printf("%d\t", arrayR[n][m]);<br> } <br> printf("\n\nThis is matrix number %d, it will be over written next time a calculation is performed.",t);<br> printf(" This matrix can replace the perminat matricies(1 and 2) by selecting number 1 from the menu\n"<br> }<br> printf("\n"<br> <br> /*display the product of two matrices*/<br> <br> if (t==4){<br> for (n=0; n<rowA; n++){<br> printf("\n\n" /*Spacing between the lines*/<br> for (m=0; m<colB; m++){ <br> printf("\t" /*spacing between columns*/ <br> for (k=0; k<colR; k++)<br> printf("\n\n"<br> printf("%d\t", arrayR[n][m]);<br> }<br> }<br> printf("\n\nThis is matrix number 3, it will be over written next time a calculation is performed."<br> printf(" This matrix can replace the perminat matricies(1 and 2) by selecting number 1 from the menu\n"<br> /*t=display*/<br> printf("\n"<br> }<br> /*display the transpose of matrix A*/<br> <br> if (t==5)<br> {<br> for(n=0; n<rowA; n++)<br> {<br> for(m=0; m<colA; m++)<br> {<br> arrayR[m][n]=arrayA[n][m];<br> }<br> }<br> printf("\n\t\n" <br> for(n=0; n<rowA; n++)<br> {<br> printf("\t"<br> for(m=0; m<colA; m++)<br> {<br> printf("\t"<br> printf("\t%d", arrayR[n][m]);<br> }<br> printf("\n"<br> printf("\n\t\n" <br> }<br> printf("\n"<br> printf("\n\nThis is matrix number 3, it will be over written next time a calculation is performed."<br> printf(" This matrix can replace the perminat matricies(1 and 2) by selecting number 1 from the menu\n"<br> /*t=display*/<br> printf("\n"<br><br> /*display the transpose of matrix B*/<br> <br> if (t==6)<br> {<br> for(n=0; n<rowB; n++)<br> {<br> for(m=0; m<colB; m++)<br> {<br> arrayR[m][n]=arrayB[n][m];<br> }<br> }<br> printf("\n\t\n" <br> for(n=0; n<rowB; n++)<br> {<br> printf("\t"<br> for(m=0; m<colB; m++)<br> {<br> printf("\t"<br> printf("\t%d", arrayR[n][m]);<br> }<br> printf("\n"<br> printf("\n\t\n" <br> }<br> printf("\n"<br> printf("\n\nThis is matrix number 3, it will be over written next time a calculation is performed."<br> printf(" This matrix can replace the perminat matricies(1 and 2) by selecting number 1 from the menu\n"<br> /*t=display*/<br> printf("\n"<br> <br> } <br> <br> }<br> /*display the identity matrix*/<br> <br> if (t==7)<br> {<br> for(n=0; n<rowR; n++)<br> {<br> for(m=0; m<colR; m++)<br> {<br> if (n==m)<br> arrayR[n][m]=array[1][1];<br> if (n!=m)<br> arrayR[n][m]=array[0][0];<br> }<br> }<br> printf("\t%d", arrayR[n][m]);<br> }<br> <br> printf("\n"<br> printf("\n\nThis is matrix number 3, it will be over written next time a calculation is performed."<br> printf(" This matrix can replace the perminat matricies(1 and 2) by selecting number 1 from the menu\n"<br> /*t=display*/<br> printf("\n"<br>}<br>/*******************************************************************/<br>/********************************************************************/<br><br>void MENU ()<br>{<br> int m;<br> printf("\n\n=================================================\n"<br> printf("What would you like to do?\n"<br> printf("\t1:\tReplace a matrix\n" /*try slashf*/<br> printf("\t2:\tDo an addition\n"<br> printf("\t3:\tDo a subtraction\n"<br> printf("\t4:\tDo a multiplication\n"<br> printf("\t5:\tCalculate a determinant\n"<br> printf("\t6:\tCalculate the transposed matrix\n"<br> printf("\t7:\tCalculate the inverse matrix\n"<br> printf("\t8:\tDo you want to view the identity matrix\n"<br> printf("\t9:\tExit\n"<br> printf("=================================================\n"<br> scanf("%d", &m);<br> <br> if (m==1) ReENTER();<br> if (m==2) ADDITION(arrayR);<br> if (m==3) SUBTRACTION (arrayR);<br> if (m==4) MULTIPLY (arrayR);<br> if (m==6) TRANSPOSE (arrayR);<br> if (m==8) IDENTITY (arrayR);<br>}<br><br>/************************************************************/<br>/*************************************************************/<br><br>void ReENTER ()<br>{<br> int new, n, m;<br> <br> printf("Would you like to:\n"<br> printf("1:\tRe-enter a matrix\n"<br> printf("2:\tReplace a matrix with a matrix just calculated\n"<br> scanf("%d", &new);<br> <br> if (new == 1){<br> printf("Which matrix would you like to re-enter?\n"<br> scanf("%d",&t);<br> INPUT(t, arrayA, arrayB);<br> DISPLAY (t, arrayA, arrayB, arrayR);<br> }<br> <br> if (new == 2){<br> printf("Which matrix do you want to be replaced?\n"<br> scanf("%d", &t);<br> if (t==1){<br> for (n=0; n<rowR; n++){<br> for (m=0; m<colR; m++)<br> arrayA[n][m] = arrayR[n][m];<br> }<br> }<br> if (t==2){<br> for (n=0; n<rowR; n++){<br> for (m=0; m<colR; m++)<br> arrayB[n][m] = arrayR[n][m];<br> }<br> }<br> }<br> <br> MENU();<br>}<br><br><br>/**************************************************************/<br>/***************************************************************/<br><br>void ADDITION (int arrayR[][max])<br>{<br> int n,m;<br> <br> if ( (rowA != rowB) ¦¦ (colA != colB) ){<br> printf("\nThe two matricies are not of the same size!\n"<br> MENU ();<br> }<br> t=3;<br> rowR = rowA; colR = colA;<br> <br> for (n=0; n<rowR; n++){<br> for (m=0; m<colR; m++)<br> arrayR[n][m] = arrayA[n][m] + arrayB[n][m];<br> }<br><br> printf("This is the resulting matrix:\n"<br> DISPLAY (t, arrayA, arrayB, arrayR);<br> MENU ();<br> <br>}<br><br>/**************************************************************/<br>/****************************************************************/<br><br>void SUBTRACTION (int arrayR[][max])<br>{<br> int sub,n,m;<br> <br> if ( (rowA != rowB) ¦¦ (colA != colB) ){<br> printf("\nThe two matricies are not of the same size!\n"<br> MENU ();<br> }<br> <br> t=3;<br> rowR = rowA; colR = colA; <br> <br> printf("Would you like to:\n"<br> printf("1:\tSubtract matrix 1 from 2\n"<br> printf("2:\tSubtract matrix 2 from 1\n"<br> scanf("%d",&sub);<br> <br> if (sub==1){<br> for (n=0; n<rowR; n++){<br> for (m=0; m<colR; m++)<br> arrayR[n][m] = arrayA[n][m] - arrayB[n][m];<br> }<br> }<br> <br> if (sub==2){<br> for (n=0; n<rowR; n++){<br> for (m=0; m<colR; m++)<br> arrayR[n][m] = arrayB[n][m] - arrayA[n][m];<br> }<br> }<br> <br> MENU ();<br>}<br>/***********************************************************************************/<br>/**********************************************************************************/<br><br> /* Multiply matrix A by matrix B, storing the *<br> * product in matrix R, where all three matrices are *<br> * a by a in size: *<br> * a-1 *<br> * R[ n ][ m ] = SUM m1[ n ][ k ] * m2[ k ][ m ] *<br> * k=0 *<br> * and 0 <= n,m < a. */<br><br>void MULTIPLY (int arrayR[][max])<br>{<br> int mul, m,n, k, colR;<br><br> if (colA != rowB){<br> printf("\n Can multiply the matrices because the number of columns in A different to the number of rows in B\n"<br> MENU ();<br> }<br> t=4;<br> <br> colR=colA; colR=rowB;<br> printf("\n\n%d\n\n", colR);<br> <br> for ( n = 0; n < rowA; n++ )<br> for ( m= 0; m < colB; m++ ) {<br> arrayR[n][m ] = 0;<br> for ( k = 0; k < colR; k++ ){<br> arrayR[n][m] += arrayA[n][k] * arrayB[k][m];<br> }<br> }<br> <br> printf("This is the resulting matrix:\n"<br> DISPLAY (t, arrayA, arrayB, arrayR);<br> MENU ();<br><br><br>}<br><br>/****************************************************************************************************/<br>/****************************************************************************************************/<br><br>void TRANSPOSE (int array[][max])<br>{<br> int n, m,k, trans;<br> <br><br> printf("Would you like to:\n"<br> printf("1:\tFind the transpose of matrix A\n"<br> printf("2:\tFind the transpose of matrix B\n"<br> scanf("%d", &trans);<br> <br> t=5;<br> <br> if (trans == 1){<br> printf("\n\nYou will calculate the transpose of matrix A\n"<br> for(n=0; n<rowA; n++)<br> for(m=0; m<colA; m++)<br> arrayR[m][n] = arrayA[n][m];<br> printf("This is the resulting matrix:\n"<br> DISPLAY(t, arrayA, arrayB, arrayR);<br> MENU();<br> printf("\n"<br> }<br> <br> t=6;<br> if (trans == 2){<br> printf("\n\nYou will calculate the transpose of matrix B\n"<br> for(n=0; n<rowB; n++)<br> for(m=0; m<colB; m++)<br> arrayR[m][n] = arrayB[n][m];<br> <br> <br> printf("This is the resulting matrix:\n"<br> DISPLAY(t, arrayA, arrayB, arrayR);<br> MENU();<br> }<br><br>}<br><br>/*************************************************************************************/<br>/************************************************************************************/<br><br>/*VIEW THE IDENTITY MATRIX*/<br><br><br><br>void IDENTITY (int array[][max])<br>{<br> int n, m,k, colI, rowI, arrayI[max][max];<br> <br> <br> printf("Enter number of columns\n"<br> <br> scanf("%d", &colR);<br> fflush(stdin); /*flush the input stream in case of bad input*/<br> printf("Enter number of rows\n"<br> scanf("%d", &rowR);<br> fflush(stdin); /* flush the input stream in case of bad input*/<br> printf("\nthis is rowR %d\n", rowR);<br> printf("\nthis is colR %d\n",colR);<br> <br> t=7;<br><br> <br> for(n=0; n<rowR; n++){<br> for(m=0; m<colR; m++){<br> if (n==m) {<br> <br> arrayR[n][m] = array[1][1];;<br> } else if (n!=m)<br> arrayR[n][m] = array[0][0];<br> }<br> printf("\tarray1 is%d\n", arrayR[n][m]);<br> <br> } <br> printf("\tarray2 is%d\n", arrayR[n][m]);<br> printf("This is the resulting matrix:\n" <br> DISPLAY(t, arrayA, arrayB, arrayR);<br> MENU();<br> <br>}<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>