Matrix Addition

Matrix is 2D array in computer science. Take user input for row and colum. Allocate two 2D dynamic array A and B, first allocate pointes for row elemnets and then allocate columns elements and assign each column pointers to row list. Now populate elements for matrix A with user input. Then do the same for matrix B. Now take element[0,0] from A and B and add them. Repeat the same for rest of all positions individually. Thus the resultant matrix is formed.

Source Code

#include <stdio.h>
#include <malloc.h>
int main(int argc, char* argv[])
{
  int rows = 0;
  int cols = 0;
  int **matrix_a;
  int **matrix_b;
  int r, c;
10    printf ("== Matrix Addition Demo ==\n");
11    printf ("Matrix has rows : ");
12    scanf ("%d", &rows);
13    printf ("Matrix has columns : ");
14    scanf ("%d", &cols);
15    matrix_a = (int **) malloc (sizeof(int **)*rows);
16    matrix_b = (int **) malloc (sizeof(int **)*rows);
17 
18    for (= 0; r < rows; r++) {
19      matrix_a[r] = (int *) malloc(sizeof(int)*cols);
20    }
21    for (= 0; r < rows; r++) {
22      matrix_b[r] = (int *) malloc(sizeof(int)*cols);
23    }
24 
25    printf ("\nMatrix A\n");
26    for (= 0; r < rows; r++) {
27      for (= 0; c < cols; c++) {
28        printf ("Element [%d][%d] : ", r, c);
29        scanf ("%d", &matrix_a[r][c]);
30      }
31    }
32 
33    printf ("\nMatrix B\n");
34    for (= 0; r < rows; r++) {
35      for (= 0; c < cols; c++) {
36        printf ("Element [%d][%d] : ", r, c);
37        scanf ("%d", &matrix_b[r][c]);
38      }
39    }
40    printf ("\nMatrix A\n");
41    for (= 0; r < rows; r++) {
42      printf ("| ");
43      for (= 0; c < cols; c++) {
44        printf ("%.2d ", matrix_a[r][c]);
45      }
46      printf (" |\n");
47    }
48    printf ("\nMatrix B\n");
49    for (= 0; r < rows; r++) {
50      printf ("| ");
51      for (= 0; c < cols; c++) {
52        printf ("%.2d ", matrix_b[r][c]);
53      }
54      printf(" |\n");
55    }
56    printf ("\nMatrix A + B\n");
57    for (= 0; r < rows; r++) {
58      printf ("| ");
59      for (= 0; c < cols; c++) {
60        printf ("%.2d ", (matrix_b[r][c] + matrix_b[r][c]));
61      }
62      printf (" |\n");
63    }
64    return 0;
65  }

Output

== Matrix Addition Demo ==
Matrix has rows : 4
Matrix has columns : 3
Matrix A
Element [0][0] : 1
Element [0][1] : 2
Element [0][2] : 3
Element [1][0] : 4
Element [1][1] : 5
Element [1][2] : 6
Element [2][0] : 7
Element [2][1] : 8
Element [2][2] : 9
Element [3][0] : 10
Element [3][1] : 11
Element [3][2] : 12
Matrix B
Element [0][0] : 13
Element [0][1] : 14
Element [0][2] : 15
Element [1][0] : 16
Element [1][1] : 17
Element [1][2] : 18
Element [2][0] : 19
Element [2][1] : 20
Element [2][2] : 21
Element [3][0] : 22
Element [3][1] : 23
Element [3][2] : 24

Matrix A
| 01 02 03 |
| 04 05 06 |
| 07 08 09 |
| 10 11 12 |

Matrix B
| 13 14 15 |
| 16 17 18 |
| 19 20 21 |
| 22 23 24 |

Matrix A + B
| 26 28 30 |
| 32 34 36 |
| 38 40 42 |
| 44 46 48 |

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

characterizing, array, multidimensional arrays, 2D and 3D dynamic array, add Matrix, multiply Matrix, adjacency Matrix, Circular buffer,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan