Matrix Manipulator Create a program that will manipulate a m

Matrix Manipulator.

Create a program that will manipulate a matrix.
The main menu should look as follows:
Main Menu
1) Enter Matrix
2) Manipulate Rows
3) Manipulate Columns
4) Print Matrix
5) Exit

If user chooses 1 from the main menu, the code should prompt the user for dimensions of the matrix, then prompt the user for the data. User should enter a row at a time with whitespace in between and return at end of each row.
if users chooses 2 (or 3) to manipulate rows/cols a second menu should be displayed that looks as follows:

Manipulate the Matrix
1) add two rows/cols and replace second row
2) multiply row/col by constant
3) return to main menu

if user chooses 4 from the main menu the matrix should be displayed in a reasonable fashion.
if user chooses 5 from the main menu the program should exit

For manipulating rows/cols, refer to the example interaction below. User input is shown in bold.

Main Menu
1) Enter Matrix
2) Manipulate Rows
3) Manipulate Columns
4) Print Matrix
5) Exit
Please enter your choice:
1
Please enter the number of rows in the matrix:
3
Please enter the number of columns in the matrix:
4

Please enter the data for row 1:
1 2 3 9
Please enter the data for row 2:
2 -1 1 8
Please enter the data for row 3:
3 0 -1 3

Main Menu
1) Enter Matrix
2) Manipulate Rows
3) Manipulate Columns
4) Print Matrix
5) Exit
Please enter your choice:
2

Manipulate the Rows of the Matrix
1) add two rows and replace second row
2) multiply row by constant
3) return to main menu

Please enter your choice:

2

Which row would you like to multiply, followed by the constant you would like to use:

1 -2

Matrix is now:

manipulations would continue, as per the example on this website

https://www.math.hmc.edu/calculus/tu   -t     ori    als/linearsystems/

Eventually the user would see:

Main Menu
1) Enter Matrix
2) Manipulate Rows
3) Manipulate Columns
4) Print Matrix
5) Exit
Please enter your choice:

4

The matrix is now:

In which case if this were a system of 3 equations and 3 variables, we would know the values of the three variables in the system, so the user could exit the program.

Main Menu
1) Enter Matrix
2) Manipulate Rows
3) Manipulate Columns
4) Print Matrix
5) Exit
Please enter your choice:

5

Good Bye, Thanks for using the Matrix Manipulator

The manipulate columns main menu function will operate similarly to the manipulate rows function, but operate on the columns instead of the rows.

You can assume the matrix will never be larger than 15x15 and declare an appropriate sized array in your code.

Solution

#include <iostream>
#include <stdio.h>

using namespace std;

int main(){
  
int manipulate, constant; // variable which will be used later in the function
int row = 20, col = 20; // row and col variable is defining current matrix size in row x col
int mat[20][20];
// initialize matrix to zero
for(int i = 0;i < row;++i){
for(int j = 0;j < col;++j){
mat[i][j] = 0;
}
}

// infinite Loop until user press 5(Exit)
while(1){   
// Display Main Menu
printf(\"Main Menu\ 1) Enter Matrix\ 2) Manipulate Rows\ 3) Manipulate Columns\ 4) Print Matrix\ 5) Exit\ \");
int choice;
printf(\"Please enter your choice:\ \");
scanf(\"%d\",&choice); // Enter your choice

// Switch case to choose the menu depending on the user input
// We could use if else too but switch looks more elegant
// and easy in this scenario
switch(choice){
case 1:
printf(\"Please enter the number of rows in the matrix:\ \");
scanf(\"%d\",&row);
printf(\"Please enter the number of columns in the matrix:\ \");
scanf(\"%d\", &col);

//now new row and col is changed

//Take the input of each row
for(int i = 0;i < row;++i){
printf(\"Please enter the data for row %d:\ \",i + 1);
for(int j = 0;j < col;++j){
scanf(\"%d\",&mat[i][j]);
}
}
break;
case 2:
// to manipulate the rows
printf(\"Manipulate the Rows of the Matrix\ 1) add two rows and replace second row\ 2) multiply row by constant\ 3) return to main menu\ \");
printf(\"Please enter your choice:\ \");
scanf(\"%d\",&manipulate);
  
// Another switch case to decide what operation we should perform
switch(manipulate){
case 1:
printf(\"Please enter the two row you want to add separated by space\ \");
int a,b;
scanf(\"%d%d\",&a,&b);
--a;--b;// making it 0 based
// add the value of row b to row a
//check if a and b is between 0 and row
if(a >= row || a < 0 || b >= row || b < 0){
printf(\"Invalid input\ Return to Main Menu\ \");
break;
}
for(int j = 0;j < col;++j){
mat[a][j] += mat[b][j];
}
//To delete the row b, we need to shift up all the row below b and decrement row size
for(int i = b + 1;i < row;++i){
for(int j = 0;j < col;++j){
mat[i - 1][j] = mat[i][j];
}
}
--row;
break;
case 2:
printf(\"Which row would you like to multiply, followed by the constant you would like to use:\ \");
int r;
scanf(\"%d%d\",&r,&constant);
// check if r is valid or not
--r;
if(r >= row || r < 0){
printf(\"Invalid input\ Return to Main Menu\ \");
break;
}

// multiply each value of row r with that constant
for(int j = 0;j < col;++j){
mat[r][j] *= constant;
}
break;
default:
break;
}
break;
case 3:
// to manipulate the columbns
printf(\"Manipulate the Columns of the Matrix\ 1) add two Columns and replace second Column\ 2) multiply Column by constant\ 3) return to main menu\ \");
printf(\"Please enter your choice:\ \");
scanf(\"%d\",&manipulate);
  
// Another switch case to decide what operation we should perform
switch(manipulate){
case 1:
printf(\"Please enter the two Columns you want to add separated by space\ \");
int a,b;
scanf(\"%d%d\",&a,&b);
--a;--b;// making it 0 based
// add the value of Column b to Column a
//check if a and b is between 0 and col
if(a >= col || a < 0 || b >= col || b < 0){
printf(\"Invalid input\ Return to Main Menu\ \");
break;
}
for(int j = 0;j < row;++j){
mat[j][a] += mat[j][b];
}
//To delete the Column b, we need to shift left all the Columns right of b and decrement Column size
for(int i = b + 1;i < col;++i){
for(int j = 0;j < row;++j){
mat[j][i - 1] = mat[j][i - 1];
}
}
--col;
break;
case 2:
printf(\"Which Column would you like to multiply, followed by the constant you would like to use:\ \");
int c;
scanf(\"%d%d\",&c,&constant);
--c;
// check if r is valid or not
if(c >= col || c < 0){
printf(\"Invalid input\ Return to Main Menu\ \");
break;
}

// multiply each value of Column c with that constant
for(int j = 0;j < row;++j){
mat[j][c] *= constant;
}
break;
default:
break;
}
break;
case 4:
printf(\"Current Matrix is -\ \");
if(row == 0 || col == 0){
printf(\"Matrix is empty\ \");
}
for(int i = 0;i < row;++i){
for(int j = 0;j < col;++j){
printf(\"%d \", mat[i][j]);
}
printf(\"\ \");
}
break;
default:
printf(\"Good Bye, Thanks for using the Matrix Manipulator\ \");
return 0;
}   
}
return 0;
}

I have implemented the above problem in C++ and has explained each possible operation through comments. Please go through it and if you face any trouble then comment it. And also try some example as it a program so it might have tiny mistake though I have checked it on own on small example.

Matrix Manipulator. Create a program that will manipulate a matrix. The main menu should look as follows: Main Menu 1) Enter Matrix 2) Manipulate Rows 3) Manipu
Matrix Manipulator. Create a program that will manipulate a matrix. The main menu should look as follows: Main Menu 1) Enter Matrix 2) Manipulate Rows 3) Manipu
Matrix Manipulator. Create a program that will manipulate a matrix. The main menu should look as follows: Main Menu 1) Enter Matrix 2) Manipulate Rows 3) Manipu
Matrix Manipulator. Create a program that will manipulate a matrix. The main menu should look as follows: Main Menu 1) Enter Matrix 2) Manipulate Rows 3) Manipu
Matrix Manipulator. Create a program that will manipulate a matrix. The main menu should look as follows: Main Menu 1) Enter Matrix 2) Manipulate Rows 3) Manipu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site