Sum of matrix and its mirror image
C program to print sum of matrix and its mirror image.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 10
void read_matrix(int mat[MAX][MAX], int row, int col)
{
int i, j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("Element [%d][%d]\t:",i,j);
scanf("%d",&mat[i][j]);
}
}
}
void print_matrix(int mat[MAX][MAX], int row, int col)
{
int i, j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
}
void mirror_image(int mat1[MAX][MAX], int row, int col)
{
int i, j, k, temp;
for(i=0;i<row;i++)
{
for(j=0,k=col-1;j<k;j++,k--)
{
temp = mat1[i][j];
mat1[i][j] = mat1[i][k];
mat1[i][k] = temp;
}
}
}
void main()
{
int mat[MAX][MAX], mat1[MAX][MAX];
int row, col, i, j;
printf("Enter the number of rows\t:");
scanf("%d",&row);
printf("Enter the number of columns\t:");
scanf("%d",&col);
printf("Enter the elements of the matrix\n");
read_matrix(mat, row, col);
printf("\nMatrix entered is\n");
print_matrix(mat, row, col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
mat1[i][j] = mat[i][j];
}
}
mirror_image(mat1, row, col);
printf("\nMirror image of the matrix is\n");
print_matrix(mat1, row, col);
printf("\nSum of the matrix and its mirror image is\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j] + mat1[i][j]);
}
printf("\n");
}
getch();
}
Output
Enter the number of rows :3
Enter the number of columns :4
Enter the elements of the matrix
Element [0][0] :1
Element [0][1] :3
Element [0][2] :6
Element [0][3] :9
Element [1][0] :2
Element [1][1] :4
Element [1][2] :5
Element [1][3] :7
Element [2][0] :8
Element [2][1] :10
Element [2][2] :12
Element [2][3] :11
Matrix entered is
1 3 6 9
2 4 5 7
8 10 12 11
Mirror image of the matrix is
9 6 3 1
7 5 4 2
11 12 10 8
Sum of the matrix and its mirror image is
10 9 9 10
9 9 9 9
19 22 22 19