In: Computer Science
Problems Background
work with multidimensional arrays. Your C program should enable the end-user to create any 2D matrix of n number of rows and m number of columns. The end-user could either use random numbers to initialize the matrix data or enter the values of his/her matrix using standard input methods such as a keyboard. After setting up the matrix data using an automated random method or manual method, the end-user could perform any following operations.
We will use the following matrix as an example to demonstrate the different operations.
4 |
6 |
32 |
12 |
1 |
9 |
5 |
7 |
3 |
Figure 1: Example of an Input Matrix
Switch Rows
This is a straightforward operation that enables the end-user to switch the data of any two rows. For example switching rows 1 and 2 of the matrix mat in Figure 1 will result in the following matrix.
This is a straightforward operation that enables the end-user to
switch the data of any two columns. For example, switching columns
0 and 2 of the matrix mat in Figure 1 will result in the following
matrix. Switch Columns
Rotate Matrix
This operation enables the end-user to rotate or shift the matrix’s data to the left or right k times. For example, rotate the matrix mat in Figure 1 to the right 1 times ( k= 1) will result in the following matrix
Each value moved 1 position to the right because k =1
Now rotate the matrix mat in Figure 1, to the left 2 times ( k= 2) will result in the following matrix.
Each value moved 2 positions to the left because k =2
Matrix Compress
The matrix compress operation is a data loss operation. It will compress any n by m matrix mat into either a vector (one-dimensional array) of size n in case of row-wise reduction or a vector of size m in case of column-wise reduction. The compression function is a simple digit sum. Recall from lab 2. digit sum of a number, say 152, is just the digits' sum, 1+5+2=8. If the sum of the digits is greater than nine, then the process is repeated. For example, the sum of the digits for 786 is 7+8+6=21, and the sum of the digits for 21 is 3 so the digit sum of 786 is
3.
For example, the row-wise compression of the matrix mat in Figure 1 will result in
For example, the column compression of the matrix mat in Figure 1 will result in
Implement a C program to enable the end-user to create, initialize a matrix and functions needed to perform the operations listed above. Make sure to use the coding temple provided
Overall you should have one program containing one main and 10 other functions. The functions are called based on an interactive user menu in a separate function, as shown in the code template. Feel free to create any additional functions if needed.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// generate random integer between lower and upper values
int GenerateRandomInt(int lower, int upper){
int num =(rand()% (upper - lower+1))+lower;
return num;
}
// use random number to set the values of the matrix
void InitializeMatrix(int row, int column, int mat[][column]){
}
// switch the data of rows with indices src and dst
void SwitchRow(int row, int column, int mat[][column], int src, int dst){
}
// switch the data of columns with indices src and dst
void SwitchColumn(int row, int column, int mat[][column], int src, int dst){
}
// enable the user to rotate the data of to the right by k positions
void RotateMatrixRight(int row, int column, int mat[][column], int src, int dst){
}
// enable the user to rotate the data of to the left by k positions
void RotateMatrixLeft(int row, int column, int mat[][column], int k){
}
// Use digit sum to compress the matrix by rows
void CompressMatrixByRows(int row, int column, int mat[][column], int k){
}
// Use digit sum to compress the matrix by columns
void CompressMatrixByColumn(int row, int column, int mat[][column], int k){
}
void PrintMatrixData(int row, int column, int mat[][column]){
}
void AppMenu( ){
int option;
do{
printf("Select any number between 1 and 7\n");
printf("1 Initialize Matrix Using Random Numbers \n");
printf("2 Enable The User Set the Matrix Values \n");
printf("3 Switch Data of Two Rows \n");
printf("4 Switch Data of Two Columns \n");
printf("5 Rotate the Matrix Right by K Times \n");
printf("6 Rotate the Matrix Left by K Times\n");
printf("7 Compress Matrix by Rows Using Digit Sum \n");
printf("8 Compress Matrix by Columns Using Digit Sum\n");
printf("9 Print Matrix\n");
scanf("%d", &option);
switch(option){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
default: ;//do nothing
}
}while(option > 1 && option < 6);
}
int main() {
// use current time as seed for random generator, should only be called once.
srand((unsigned int)time(NULL));
int const max_row = 100;
int const max_column =100;
int matrix[max_row][max_column];
int vector[max_column];
int row, column;
printf("Enter the number of rows of your matrix: ");
scanf("%d", &row);
printf("Enter the number of columns of your matrix: ");
scanf("%d", &column);
//call App Menu to interact with the end user
AppMenu();
return 0;
}
// use random number to set the values of the matrix 1 . void InitializeMatrix(int row, int column, int mat[][column])
void InitializeMatrix(int row, int column, int mat[][column]) { int lower = ----; int upper = ----; for(int i=0;i<row;i++) { for(int j=0;j<column;j++) { mat[i][j] = GenerateRandomInt(lower,upper); } } }
2. // switch the data of rows with indices src and dst void SwitchRow(int row, int column, int mat[][column], int src, int dst)
void SwitchRow(int row, int column, int mat[][column], int src, int dst) { // swapping of element between source and destination rows for (int i = 0; i < column ; i++) { int t = mat[src][i]; mat[src][i] = mat[dst][i]; mat[dst - 1][i] = t; } }
3.// switch the data of columns with indices src and dst void SwitchColumn(int row, int column, int mat[][column], int src, int dst)
void SwitchColumn(int row, int column, int mat[][column], int src, int dst) { // swapping of element between source and destination columns for (int i = 0; i < row; i++) { int t = mat[i][src]; mat[i][src] = mat[i][dst]; mat[i][dst] = t; } }
4.// enable the user to rotate the data of to the right by k positions void RotateMatrixRight(int row, int column, int mat[][column], int k)
void RotateMatrixRight(int row, int column, int mat[][column], int k) { int temp[column]; // check to keep within the size of matrix k = k % column; for (int i = 0; i < row; i++) { // copy first column-k elements to temporary array for (int t = 0; t < column - k; t++) temp[t] = mat[i][t]; // copy the elements from k to end to starting for (int j = column - k; j < column; j++) mat[i][j - column + k] = mat[i][j]; // copy elements from temporary array to end for (int j = k; j < column; j++) mat[i][j] = temp[j - k]; } }
I have solved first four parts.