Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
26. Write a program to add and multiply two matrices.
/*
File: Prgrm26.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to add and multiply two matrices.
*/
#include <stdio.h>
int main (void)
{
int matrix_1 [10][10], matrix_2 [10][10];
int sum_matrix [10][10], multiply_matrix [10][10];
int i, j, k, row_1, column_1, row_2, column_2, sum;
//Input first matrix.
printf ("***Input first matrix***\n");
printf ("Input number of rows: ");
scanf ("%d", &row_1);
printf ("Input number of columns: ");
scanf ("%d", &column_1);
for (i = 0; i < row_1; i++)
{
for (j = 0; j < column_1; j++)
{
printf ("Input element %d %d: ", i + 1, j + 1);
scanf ("%d", &matrix_1 [i][j]);
}
}
//Input second matrix.
printf ("\n***Input second matrix***\n");
printf ("Input number of rows: ");
scanf ("%d", &row_2);
printf ("Input number of columns: ");
scanf ("%d", &column_2);
for (i = 0; i < row_2; i++)
{
for (j = 0; j < column_2; j++)
{
printf ("Input element %d %d: ", i + 1, j + 1);
scanf ("%d", &matrix_2 [i][j]);
}
}
//Output first matrix.
printf ("\n***First Matrix***\n");
for (i = 0; i < row_1; i++)
{
for (j = 0; j < column_1; j++)
{
printf ("%d\t", matrix_1[i][j]);
}
printf ("\n");
}
//Output second matrix.
printf ("\n\n***Second Matrix***\n");
for (i = 0; i < row_2; i++)
{
for (j = 0; j < column_2; j++)
{
printf ("%d\t", matrix_2[i][j]);
}
printf ("\n");
}
//Calculate and output the sum of matrices.
if (row_1 == row_2 && column_1 == column_2)
{
printf ("\n\n***Sum of Matrices***\n");
for (i = 0; i < row_1; i++)
{
for (j = 0; j < column_1; j++)
{
sum_matrix [i][j] = matrix_1 [i][j] + matrix_2 [i][j];
printf ("%d\t", sum_matrix [i][j]);
}
printf ("\n");
}
}
else
{
printf ("\nSum of matrices is not possible.");
}
//Calculate and output the multiplication of matrices.
if (column_1 == row_2)
{
printf ("\n\n***Multiplication of Matrices***\n");
for (i = 0; i < row_1; i++)
{
for (j = 0; j < column_2; j++)
{
for (sum = 0, k = 0; k < column_1; k++)
{
sum = sum + matrix_1 [i][k] * matrix_2 [k][j];
}
multiply_matrix [i][j] = sum;
printf ("%d\t", multiply_matrix [i][j]);
}
printf ("\n");
}
}
else
{
printf ("\n\nMultiplication of matrices is not possible.");
}
int sum_matrix [10][10], multiply_matrix [10][10];
int i, j, k, row_1, column_1, row_2, column_2, sum;
//Input first matrix.
printf ("***Input first matrix***\n");
printf ("Input number of rows: ");
scanf ("%d", &row_1);
printf ("Input number of columns: ");
scanf ("%d", &column_1);
for (i = 0; i < row_1; i++)
{
for (j = 0; j < column_1; j++)
{
printf ("Input element %d %d: ", i + 1, j + 1);
scanf ("%d", &matrix_1 [i][j]);
}
}
//Input second matrix.
printf ("\n***Input second matrix***\n");
printf ("Input number of rows: ");
scanf ("%d", &row_2);
printf ("Input number of columns: ");
scanf ("%d", &column_2);
for (i = 0; i < row_2; i++)
{
for (j = 0; j < column_2; j++)
{
printf ("Input element %d %d: ", i + 1, j + 1);
scanf ("%d", &matrix_2 [i][j]);
}
}
//Output first matrix.
printf ("\n***First Matrix***\n");
for (i = 0; i < row_1; i++)
{
for (j = 0; j < column_1; j++)
{
printf ("%d\t", matrix_1[i][j]);
}
printf ("\n");
}
//Output second matrix.
printf ("\n\n***Second Matrix***\n");
for (i = 0; i < row_2; i++)
{
for (j = 0; j < column_2; j++)
{
printf ("%d\t", matrix_2[i][j]);
}
printf ("\n");
}
//Calculate and output the sum of matrices.
if (row_1 == row_2 && column_1 == column_2)
{
printf ("\n\n***Sum of Matrices***\n");
for (i = 0; i < row_1; i++)
{
for (j = 0; j < column_1; j++)
{
sum_matrix [i][j] = matrix_1 [i][j] + matrix_2 [i][j];
printf ("%d\t", sum_matrix [i][j]);
}
printf ("\n");
}
}
else
{
printf ("\nSum of matrices is not possible.");
}
//Calculate and output the multiplication of matrices.
if (column_1 == row_2)
{
printf ("\n\n***Multiplication of Matrices***\n");
for (i = 0; i < row_1; i++)
{
for (j = 0; j < column_2; j++)
{
for (sum = 0, k = 0; k < column_1; k++)
{
sum = sum + matrix_1 [i][k] * matrix_2 [k][j];
}
multiply_matrix [i][j] = sum;
printf ("%d\t", multiply_matrix [i][j]);
}
printf ("\n");
}
}
else
{
printf ("\n\nMultiplication of matrices is not possible.");
}
};
Output
***Input first matrix***
Input number of rows: 3
Input number of columns: 3
Input element 1 1: 5
Input element 1 2: 9
Input element 1 3: 7
Input element 2 1: 4
Input element 2 2: 8
Input element 2 3: 6
Input element 3 1: 3
Input element 3 2: 1
Input element 3 3: 2
***Input second matrix***
Input number of rows: 3
Input number of columns: 3
Input element 1 1: 4
Input element 1 2: 7
Input element 1 3: 9
Input element 2 1: 8
Input element 2 2: 3
Input element 2 3: 6
Input element 3 1: 5
Input element 3 2: 2
Input element 3 3: 1
***First Matrix***
5 9 7
4 8 6
3 1 2
***Second Matrix***
4 7 9
8 3 6
5 2 1
***Sum of Matrices***
9 16 16
12 11 12
8 3 3
***Multiplication of Matrices***
127 76 106
110 64 90
30 28 35
No comments:
Post a Comment
Please do not post spam links.