Please write program in C/C++ and explain the steps for every logic of program b
ID: 3918348 • Letter: P
Question
Please write program in C/C++ and explain the steps for every logic of program because i am not understanding and getting confused.
Write a program to declare a square matrix A[ ] [ ] of order (M x M) where ‘M’ is the number of rows and the number of columns
(a) write a function void mirrorMatrix() to print the MIRROR IMAGE MATRIX
for Example
ORIGINAL MATRIX
4 16 12
8 2 14
4 1 3
MIRROR IMAGE MATRIX
12 16 4
14 2 8
3 1 6
(b)write a function void waterMatrix() to print the WATER IMAGE MATRIX
Water Image is :
1 2 3
4 5 6
7 8 9
- - - - -
7 8 9
4 5 6
1 2 3
(c) write a function void diagnoalMatrix() to print the Mirror of matrix across diagonal
Explanation / Answer
====================================ANSWERS OF OPTION C=======================
// Simple CPP program to find mirror of
// matrix across diagonal.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
void imageSwap(int mat[][MAX], int n)
{
// for diagonal which start from at
// first row of matrix
int row = 0;
// traverse all top right diagonal
for (int j = 0; j < n; j++) {
// here we use stack for reversing
// the element of diagonal
stack<int> s;
int i = row, k = j;
while (i < n && k >= 0)
s.push(mat[i++][k--]);
// push all element back to matrix
// in reverse order
i = row, k = j;
while (i < n && k >= 0) {
mat[i++][k--] = s.top();
s.pop();
}
}
// do the same process for all the
// diagonal which start from last
// column
int column = n - 1;
for (int j = 1; j < n; j++) {
// here we use stack for reversing
// the elements of diagonal
stack<int> s;
int i = j, k = column;
while (i < n && k >= 0)
s.push(mat[i++][k--]);
// push all element back to matrix
// in reverse order
i = j;
k = column;
while (i < n && k >= 0) {
mat[i++][k--] = s.top();
s.pop();
}
}
}
// Utility function to print a matrix
void printMatrix(int mat[][MAX], int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
// driver program to test above function
int main()
{
int mat[][MAX] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int n = 4;
imageSwap(mat, n);
printMatrix(mat, n);
return 0;
}
===============================================================================
================================ANSWER OF OPTION A & B=======================
=================================================================================
===================OPTION A AND B ARE ANSWERED In same program because of less time
=============================================================================
PLEASE RATE ME THUMBS UP
// Simple CPP program to find mirror of
// matrix across diagonal.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
void imageSwap(int mat[][MAX], int n)
{
// for diagonal which start from at
// first row of matrix
int row = 0;
// traverse all top right diagonal
for (int j = 0; j < n; j++) {
// here we use stack for reversing
// the element of diagonal
stack<int> s;
int i = row, k = j;
while (i < n && k >= 0)
s.push(mat[i++][k--]);
// push all element back to matrix
// in reverse order
i = row, k = j;
while (i < n && k >= 0) {
mat[i++][k--] = s.top();
s.pop();
}
}
// do the same process for all the
// diagonal which start from last
// column
int column = n - 1;
for (int j = 1; j < n; j++) {
// here we use stack for reversing
// the elements of diagonal
stack<int> s;
int i = j, k = column;
while (i < n && k >= 0)
s.push(mat[i++][k--]);
// push all element back to matrix
// in reverse order
i = j;
k = column;
while (i < n && k >= 0) {
mat[i++][k--] = s.top();
s.pop();
}
}
}
// Utility function to print a matrix
void printMatrix(int mat[][MAX], int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
// driver program to test above function
int main()
{
int mat[][MAX] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int n = 4;
imageSwap(mat, n);
printMatrix(mat, n);
return 0;
}
===============================================================================
================================ANSWER OF OPTION A & B=======================
#include <bits/stdc++.h> using namespace std; #define R 3 #define C 3 void display(int a[R][C]) { for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) printf("%d ", a[i][j]); printf(" "); } } void mirror(int a[R][C]) // Row wise reversing { for (int i = 0; i < R; i++) { for (int j = 0; j < C/2; j++) swap(a[i][j], a[i][C - j - 1]); } } void water(int a[R][C]) // Column wise reversing { for (int j = 0; j < C; j++) { for (int i = 0; i < R/2; i++) swap(a[i][j], a[R- i - 1][j]); } } int main() { int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; mirror(a); display(a); // water(a); // display(a) =================================================================================
===================OPTION A AND B ARE ANSWERED In same program because of less time
=============================================================================
PLEASE RATE ME THUMBS UP