Chapter - 9: Arrays

Write a program to add two 6 x 6 matrices.


L
Sections
9
Exercises

A

B

C

D

E

F

G

H

I

J

K

L

1

2

3

4

5

6

7

8

9

10

11

13

14

15

16

17

18

19

#include<stdio.h>
#include<conio.h>

int main()
{
	int a[6][6], b[6][6], sum[6][6], i, j;

	printf("Enter first matrix.\n");

	for (i = 0; i<6; i++)//scanning elements of first matrix.
	{
		for (j = 0; j<6; j++)
			scanf("%d", &a[i][j]);
	}

	printf("\n\nEnter the second matrix.\n\n");

	for (i = 0; i<6; i++)//scanning elements of second matrix.
	{
		for (j = 0; j<6; j++)
			scanf("%d", &b[i][j]);
	}

	printf("\n\n\n");

	for (i = 0; i<6; i++)//summing up the matrices
		for (j = 0; j<6; j++)
			sum[i][j] = a[i][j] + b[i][j];

	for (i = 0; i<6; i++)//displaying the matrix with it's summition
	{
		for (j = 0; j<6; j++)
			printf("%d ", a[i][j]);
		for (j = 0; j<6; j++)
		{
			if (i == 2 && j == 2)
				printf("+");
			else
				printf(" ");
		}
		for (j = 0; j<6; j++)
			printf("%d ", b[i][j]);
		for (j = 0; j<6; j++)
		{
			if (i == 2 && j == 2)
				printf("=");
			else
				printf(" ");
		}
		for (j = 0; j<6; j++)
		{
			if (sum[i][j]<9)
				printf(" ");
			printf("%d ", sum[i][j]);
		}

		printf("\n");
	}


	_getch();
	return 0;
}

© 2020 Garbage Valuegarbage value logo