ccplusplus.com
Learn C, C++ Concepts
Tuesday, September 13, 2011
matrix multiplication in C
/****************************************************** * File : matrix-multiplication.c * Author : Saurabh Gupta * Desc : matrix multiplication using arrays in c * Source : http://saurabhgupta0527.blogspot.com/ * Created : PM 07:30 13 September 2011 *****************************************************/ #include <stdio.h> int main (int argc, char **argv) { int firstMatrix[10][10], secondMatrix[10][10], addedMatrix[10][10], multipliedMatrix[10][10]; int nFirstMatrixRow, nFirstMatrixColumn, nSecondMatrixRow, nSecondMatrixColumn; int i, j, k, nUserChoice, nMultiply, nAdd; printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXX Matrix Multiplication Program XXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("\nMaximum Row and Column limit : 10\n"); printf("\nNumber of rows and column of first matrix : "); scanf("%d%d", &nFirstMatrixRow, &nFirstMatrixColumn); printf("\nNumber of rows and column of second matrix : "); scanf("%d%d",&nSecondMatrixRow,&nSecondMatrixColumn); if(nSecondMatrixRow == nFirstMatrixColumn) { printf("\nEnter rows and columns of First matrix"); printf("Row wise\n"); for(i = 0; i < nFirstMatrixRow; i++) { for(j = 0; j<nFirstMatrixColumn; j++) { scanf("%d", &firstMatrix[i][j]); } } printf("\nFirst Matrix is:\n"); for(i = 0; i < nFirstMatrixRow; i++) { for(j = 0; j < nFirstMatrixColumn; j++) { printf("%d\t",firstMatrix[i][j]); } printf("\n"); } printf("\nEnter rows and columns of Second matrix \n"); printf("Row wise\n"); for(i=0;i<nSecondMatrixRow;i++) { for(j=0;j<nSecondMatrixColumn;j++) { scanf("%d",&secondMatrix[i][j]); } } printf("\nSecond matrixis:\n"); for(i = 0; i<nSecondMatrixRow; i++) { for(j = 0; j<nSecondMatrixColumn; j++) { printf("%d\t",secondMatrix[i][j]); } printf("\n"); } printf("\nThe result of the multiplication is as follows:\n"); /*a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/ for(i=0;i<nFirstMatrixRow;i++) { for(j=0;j<nSecondMatrixColumn;j++) { multipliedMatrix[i][j]=0; for(k=0;k<nFirstMatrixRow;k++) { multipliedMatrix[i][j]+=firstMatrix[i][k]*secondMatrix[k][j]; /*multipliedMatrix[0][0]=firstMatrix[0][0]*secondMatrix[0][0]+firstMatrix[0][1]*secondMatrix[1][0]+firstMatrix[0][2]*secondMatrix[2][0];*/ } printf("%d\t",multipliedMatrix[i][j]); } printf("\n"); } } else { printf("Matrix multiplication cannot be done"); } printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } /* * OUTPUT * [sgupta@rhel54x64 tmp]$ gcc matrix-multiplication.c -o matrix-multiplication [sgupta@rhel54x64 tmp]$ ./matrix-multiplication XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX Matrix Multiplication Program XXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Maximum Row and Column limit : 10 Number of rows and column of first matrix : 2 2 Number of rows and column of second matrix : 2 2 Enter rows and columns of First matrixRow wise 1 1 1 1 First Matrix is: 1 1 1 1 Enter rows and columns of Second matrix Row wise 2 2 2 2 Second matrixis: 2 2 2 2 The result of the multiplication is as follows: 4 4 4 4 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel54x64 tmp]$ */
See Also
Various other Popular C codes and C topics
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment