ccplusplus.com
Learn C, C++ Concepts
Tuesday, September 13, 2011
matrix addition in c
/****************************************************** * File : matrix-addition.c * Author : Saurabh Gupta * Desc : matrix addition using arrays in c * Source : http://saurabhgupta0527.blogspot.com/ * Created : AM 10:42 13 September 2011 * Note : *****************************************************/ #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; printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXX Matrix Addition 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(nFirstMatrixRow == nSecondMatrixRow && nFirstMatrixColumn == nSecondMatrixColumn) { 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 addition is as follows;\n"); for(i = 0; i < nFirstMatrixRow; i++) { for(j = 0; j < nFirstMatrixColumn; j++) { addedMatrix[i][j] = firstMatrix[i][j] + secondMatrix[i][j]; printf("%d\t",addedMatrix[i][j]); } printf("\n"); } } else { printf("Addition Failed : rows or columns are not equal\n"); } printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } /* * OUTPUT * [sgupta@rhel54x64 c]$ gcc matrix-addition.c -o matrix-addition [sgupta@rhel54x64 c]$ ./matrix-addition XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX Matrix Addition 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 addition is as follows; 3 3 3 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
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