matrix addition in c

Solutions on MaxInterview for matrix addition in c by the best coders in the world

showing results for - "matrix addition in c"
Stefania
30 Feb 2017
1//This program is for matrix addition by programiz.com
2#include <stdio.h>
3int main() {
4    int r, c, a[100][100], b[100][100], sum[100][100], i, j;
5    printf("Enter the number of rows (between 1 and 100): ");
6    scanf("%d", &r);
7    printf("Enter the number of columns (between 1 and 100): ");
8    scanf("%d", &c);
9
10    printf("\nEnter elements of 1st matrix:\n");
11    for (i = 0; i < r; ++i)
12        for (j = 0; j < c; ++j) {
13            printf("Enter element a%d%d: ", i + 1, j + 1);
14            scanf("%d", &a[i][j]);
15        }
16
17    printf("Enter elements of 2nd matrix:\n");
18    for (i = 0; i < r; ++i)
19        for (j = 0; j < c; ++j) {
20            printf("Enter element a%d%d: ", i + 1, j + 1);
21            scanf("%d", &b[i][j]);
22        }
23
24    // adding two matrices
25    for (i = 0; i < r; ++i)
26        for (j = 0; j < c; ++j) {
27            sum[i][j] = a[i][j] + b[i][j];
28        }
29
30    // printing the result
31    printf("\nSum of two matrices: \n");
32    for (i = 0; i < r; ++i)
33        for (j = 0; j < c; ++j) {
34            printf("%d   ", sum[i][j]);
35            if (j == c - 1) {
36                printf("\n\n");
37            }
38        }
39
40    return 0;
41}
42
Edoardo
19 Jan 2018
1#include <stdio.h>
2int main(){
3int row, column, mat1[100][100], mat2[100][100], sum[100][100], i, j;
4printf("Enter the number of rows and columns : \n");
5scanf("%d %d", &row, &column);
6printf("\nInput Matrix 1 elements : ");
7for(i=0; i<row; ++i)
8for(j=0; j<column; ++j)
9{
10scanf("%d",&mat1[i][j]);
11}
12printf("\nMatrix 1\n");
13for(i=0;i<row;i++)
14{
15for(j=0;j<column;j++)
16{
17printf("%d",mat1[i][j]);
18}
19printf("\n");
20}
21
22printf("\nInput Matrix 2 elements : ");
23for(i=0; i<row; ++i)
24for(j=0; j<column; ++j)
25{
26scanf("%d", &mat2[i][j]);
27}
28printf("\nMatrix 2\n");
29for(i=0;i<row;i++)
30{
31for(j=0;j<column;j++)
32{
33printf("%d",mat1[i][j]);
34}
35printf("\n");
36}
37// Adding Two matrices
38printf("\nAdded Matrix\n");
39for(i=0;i<row;++i)
40for(j=0;j<column;++j)
41{
42sum[i][j]=mat1[i][j]+mat2[i][j];
43}
44
45// print the result
46
47for(i=0;i<row;++i)
48for(j=0;j<column;++j)
49{
50printf("%d ",sum[i][j]);
51
52if(j==column-1)
53{
54printf("\n");
55}
56}
57
58return 0;
59}
60/*©VinCoD*/
Dahlia
20 Apr 2017
1#include <stdio.h>
2void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2);
3void multiplyMatrices(int first[][10], int second[][10], int multResult[][10], int r1, int c1, int r2, int c2);
4void display(int mult[][10], int r1, int c2);
5
6int main() {
7    int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2;
8    printf("Enter rows and column for the first matrix: ");
9    scanf("%d %d", &r1, &c1);
10    printf("Enter rows and column for the second matrix: ");
11    scanf("%d %d", &r2, &c2);
12
13    // Taking input until columns of the first matrix is equal to the rows of the second matrix
14    while (c1 != r2) {
15        printf("Error! Enter rows and columns again.\n");
16        printf("Enter rows and columns for the first matrix: ");
17        scanf("%d%d", &r1, &c1);
18        printf("Enter rows and columns for the second matrix: ");
19        scanf("%d%d", &r2, &c2);
20    }
21
22    // Function to take matrices data
23    enterData(first, second, r1, c1, r2, c2);
24
25    // Function to multiply two matrices.
26    multiplyMatrices(first, second, mult, r1, c1, r2, c2);
27
28    // Function to display resultant matrix after multiplication.
29    display(mult, r1, c2);
30
31    return 0;
32}
33
34void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2) {
35    printf("\nEnter elements of matrix 1:\n");
36
37    for (int i = 0; i < r1; ++i) {
38        for (int j = 0; j < c1; ++j) {
39            printf("Enter a%d%d: ", i + 1, j + 1);
40            scanf("%d", &first[i][j]);
41        }
42    }
43    printf("\nEnter elements of matrix 2:\n");
44
45    for (int i = 0; i < r2; ++i) {
46        for (int j = 0; j < c2; ++j) {
47            printf("Enter b%d%d: ", i + 1, j + 1);
48            scanf("%d", &second[i][j]);
49        }
50    }
51}
52
53void multiplyMatrices(int first[][10], int second[][10], int mult[][10], int r1, int c1, int r2, int c2) {
54
55    // Initializing elements of matrix mult to 0.
56    for (int i = 0; i < r1; ++i) {
57        for (int j = 0; j < c2; ++j) {
58            mult[i][j] = 0;
59        }
60    }
61
62    // Multiplying first and second matrices and storing in mult.
63    for (int i = 0; i < r1; ++i) {
64        for (int j = 0; j < c2; ++j) {
65            for (int k = 0; k < c1; ++k) {
66                mult[i][j] += first[i][k] * second[k][j];
67            }
68        }
69    }
70}
71
72void display(int mult[][10], int r1, int c2) {
73
74    printf("\nOutput Matrix:\n");
75    for (int i = 0; i < r1; ++i) {
76        for (int j = 0; j < c2; ++j) {
77            printf("%d  ", mult[i][j]);
78            if (j == c2 - 1)
79                printf("\n");
80        }
81    }
82}
83
queries leading to this page
addition of three dimensional of two matrix in chow to add two matrix in c by functionsummation of two array c programimplement matrix multiplication in cprogram for matrix multiplication using array in c2d array multiplication in caddition and multiplication on matrix in caddition of two matrices in carray of integers multiplication in caddition of two matrix in cc program for the matrix multiplicationc program to add two matricessum of two matrix c programadd two matrices in cmatrix multiplication in c logicmatrix multiplication c programmatrix addition i n cc program to find matrix multiplicationmatrix multiplication programmatrix addition of two number program in cmatrix multiplication code in c gfgmatrix program in calgorithm for matrix multiplication in cwrite a c program multiplication of two matrix2d array multiplication cc matrix multiplication functionc add multidimensional matrixmatrix multiplication code in cmultiply 2 matrice codec metric multiplacationmatrix multiplication c codec program for matrix multiplication2x2 matrix multiplication in cprogram in c for matrix additionhow to sum two array in cmatrix addition and multiplication in cc add two matrixwrite a c program to do the following using pointer to pointer 28dynamic memory allocation 29 3a a 29 to add two matrices a and b of size mxn 28read m and n as input 29 c 29 to multiply two matrices a and b c 29 to find the transpose of a matrix matrix addition program in cmatrix multiplication i c programizhow to take multiple matrix based on user in cproduct of matrix in c programmultiplication of matrix program in cmultiplication of 2d arrayhow to add elements to a matrix in chow to do addition of two matrix in cc multiply matricesfunction insert on matrix cc program for matrix additionaddition of 2 array in c3 2a3 matrix addition program in cmultiplication of two matrix in cadd 2 matrix in cadding matrices in cmultiply two matrix cmultiply matrix in cmultiply matric in caddition of 2 matrices in cmatrix addition using array in cmatrix multiplication in c 2b 2bmatrix multiplication in c with different dimensionsmatrix multiplication cadding matrix using array in cprogram for matrix multiplicationmultiplication arrays codeenter two matrix a and b create a new matrix c which stores all elements of matrix a first 2c then all elements of marix b c matrix appendmatrix multiplication data structurearray multiplication caddition of matrices code in cmatrix multiplication codematrix addition in c programmingfunction for matrix addition in cmatrix in c programmingcode in c for matrix multiplicationmatrix multiplication with different dimensions in cmultiplication of matrix 2 2a2 code in cinsert on matrix csum of matrix in function in ccode for matrix multiplicationfuntion to addition of 3x3 matrix in c programmingaddition of 2 matrix in cc program to add to matrixwrite a c program to do the following using pointer to pointer 28dynamic memory allocation 29 3a a 29 to add two matrices a and b of size mxn 28read m and n as inputs 29 b 29 to multiply two matrices a and b c 29 to find the transpose of a matrix write a c program for matrix multiplication using operator overloadingmultiplication of two arrays in csproduct of matrix in cmultiplication of 2 matrices codeaddition of 2 arrays in cmultiplication of matrix in cc matrix multiplyhow to code matrix multiplication in c with a matrixc program to multiply two matricesc matrizes turorialadd matrix in cmatrices in ccode of matrix multiplication in cmultiplying matrix in cmatrices multiplication in c2 2a2 and 2 2a1 matrix multiplication in cmultiplication of matrix in c using 2d arraymatrix multiplication in c 2d arraymultiplication of 2d array in caddition of two 3 2a3 matrix in cmultiplication array in cc program for addition of two 3 c3 973 matricesprogram for adding matrices in chow to addition program and multi code cmultiplication of two matrices codeaddition of one dimensional array in cc multiply two matricesmultiplying any 2 matrices in caddition matrix in cc programs to add two matrixc matrix multiplication matrix addition in carray multiplication in caddition of matrix in cmatrix multiplication in c program2d array addition in cc program to find matrix multiplication using arrayswrite a program to print multiplication of two matricesadding two matrix in cmatrix addition code in cwrite a c program to find the matrix multiplication of two matrices matrix matrix multiplication cmatrix multiplication program in cmultiply matrices in c to read two matrix a and b and perform matrix addition matrix addition in chow to multiply two arrays in chow to do addition of two matrix in c using functionmatrix code in c 2 2a 40ad matrix addition in cmultiplication of two array elements in cmultiplication matrix table c if statementmatrix multiplication in cmatrix sum code in cprogram for matrixmatrix programming in cmultiply two matrices in cc program for addition of two matrices multiply 2 matrices in caddition subtraction multiplication division in c programminghow to append 2 arrays in csum of two array in csum of matrixes in cmultiplication of two matrices in cmatrix multiplication algorithm in caddition of multidimensional array in cmatrix multiplicationin cmultiplication matrix in carray of multiplication cwrite a program to add 2 matrices in caddition of two matrices in c using functionsmultiplication two matrices by using 2 dimensional array in csum of 2 matrices in c with user inputsaddition of matrix in c using arraywrite a c program for addition of matrixhow to multiply 2 matrices in chow to add two matrix in cc matrix mathmultiplying matrices in cfunctions in c program for 3x3 matrix addition how to do matrix multiplication in 2d array 273 by 3 matrix addition in cmultiply arrays in cmatrix addition in c with user inputhow to perform addtion of matrices in cmatrix multiplication codingmultiply matrix cperform matrix multiplication using chow to add matrix datatypes to chow to multiply matrices in cadding two matrices in cmatrix addition using array in c programaddition of matrices in c using functionssum of two matrix in ctwo matrices a and b in c programcode for matrx multiplication 2dmatrix in cadd two matrix in clogic of matrix addition in caddition of two arrays in cmatrix multiplication function c language2 2a2 and 2 2a3 matrix multiplication in cprogram to find multiplication of two matricesadd element to matrix in cc program to perform addition of two matricesmultiple matrix arrayaddition of matrices in cmatrix multiplication from file in cmultiplication of matrix 2 2a2 codematrix multiplication in c using functionmultiplying two matrices c programmatrix addition i cmultiplication of arrays in cmatrix addition in c