In: Computer Science
Write a program to prompt the user to display the following menu:
Sort Matrix Quit
How many numbers: 6
Original numbers: Sorted numbers
34 2
55 11
2 34
89 55
78 78
11 89
Do you want to quit? Q
Do you want to quit? Q
If the user enters ‘Q’ or ‘’, your program should terminate the program.
C++ Please
Below is the complete C++ code. If you face any difficulty while understanding the code, Please let me know in the comments.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
char ch;
cout << "Menu:\n Sort: s or S\n Matrix: m or M\n Quit: q or
Q" << endl;
cin >> ch ;
if(ch == 's' || ch == 'S') {
int n;
cout << "How many numbers:";
cin >> n ;
int origArr[n];
int sortArr[n];
for(int i=0;i<n;i++) {
cin >> origArr[i];
sortArr[i] = origArr[i];
}
sort(sortArr, sortArr + n );
cout << "Original numbers" << " Sorted numbers"
<< endl;
for(int i=0;i<n;i++)
cout << origArr[i] << " " << sortArr[i] <<
endl ;
}else if(ch == 'm' || ch == 'M') {
int A[2][2];
cout << "Enter the elements of matrix M:\n";
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
cin >> A[i][j];
int A_transposed[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
A_transposed[i][j] = A[j][i];
int C[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
C[i][j] = A[i][j] + A_transposed[i][j];
// Print matrix A
cout << "\nMatrix A:\n";
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
cout << A[i][j] << " ";
cout << "\n";
}
// Print matrix A-transposed
cout << "\nA-Transposed matrix:\n";
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
cout << A_transposed[i][j] << " ";
cout << "\n";
}
// Print matrix C
cout << "\nMatrix C:\n";
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
cout << C[i][j] << " ";
cout << "\n";
}
} else if(ch == 'q' || ch == 'Q') {
char ch;
cout << "Do you want to quit? Q\n" ;
cin >> ch;
if(ch == 'Q')
return 0;
}
return 0;
}
Screenshots:
Output: