In: Computer Science
I need to fix this code, and could you please tell me what was the problem
options 1 and 9 don't work
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// generate a random integer between lower and upper
values
int GenerateRandomInt(int lower, int upper){
int num =(rand()% (upper -
lower+1))+lower;
return num;
}
// use random numbers to set the values of the matrix
void InitializeMatrix(int row, int column, int dimension, int
mat[][dimension]){
for(int i =0; i<row; i++){
for(int j=0;
j<column; j++){
mat[i][j] = GenerateRandomInt(0,10);
}
}
}
// allow the user to set the values of the matrix
void SetMatrixData(int row, int column, int dimension, int
mat[][dimension]){
printf("Please enter integers numbers to
initialize your matrix\n");
for(int i =0; i<row; i++){
for(int j=0;
j<column; j++){
printf("matrix[%d , %d]= ",i,j);
scanf("%d",&mat[i][j]);
}
}
}
// switch the data of rows with indices src and dst
void SwitchRow(int row, int column, int dimension, int
mat[][dimension], int src, int dst){
int temp;
for(int j=0; j<column; j++){
temp =
mat[src][j];
mat[src][j] =
mat[dst][j];
mat[dst][j] =
temp;
}
}
// switch the data of columns with indices src and dst
void SwitchColumn(int row, int column, int dimension, int
mat[][dimension], int src, int dst){
int temp;
for(int i=0; i<row; i++){
temp =
mat[i][src];
mat[i][src] =
mat[i][dst];
mat[i][dst] =
temp;
}
}
// enable the user to rotate the data of to the right by k
positions
void RotateMatrixRight(int row, int column,int dimension, int
mat[][dimension], int k){
int temp;
for(int r =0; r<k%column; r++) {
for (int i = 0; i
< row; i++) {
temp = mat[i][column-1];
int j;
for ( j = 1; j < column; j++) {
mat[i][column-j] = mat[i][column-(j+1)];
}
mat[i][column-j] = temp;
}
}
}
// enable the user to rotate the data of to the left by k
positions
void RotateMatrixLeft(int row, int column, int dimension, int
mat[][dimension], int k){
int temp;
for(int r =0; r<k%column; r++) {
for (int i = 0; i
< row; i++) {
temp = mat[i][0];
int j;
for ( j = 0; j < column-1; j++) {
mat[i][j] = mat[i][j+1];
}
mat[i][j] = temp;
}
}
}
// calculate some of digits
int SumOfDigits(int n){
int sum =0;
while(n/10 != 0){
sum += n%10;
n = n/10;
if(n/10 == 0){
sum +=n;
n = sum;
sum = 0;
}
}
sum = n;
return sum;
}
// Use digit sum to compress the matrix by rows
void CompressMatrixByRows(int row, int column, int dimension, int
mat[][dimension]){
int sum;
for(int i =0; i<row; i++){
sum =0;
for(int j=0;
j<column; j++){
sum += mat[i][j];
}
sum =
SumOfDigits(sum);
printf("row[%d] =%d \n",
i, sum);
}
}
// Use digit sum to compress the matrix by columns
void CompressMatrixByColumn(int row, int column, int dimension, int
mat[][dimension]){
int sum;
for(int i =0; i<column; i++){
sum =0;
for(int j=0; j<row;
j++){
sum += mat[j][i];
}
sum =
SumOfDigits(sum);
printf("column[%d] = %d
\t", i, sum);
}
printf("\n");
}
// Display the matrix data on the screen
void PrintMatrixData(int row, int column, int dimension, int
mat[][dimension]){
printf("The current state of the
matrix:\n\n");
for(int i =0; i<row; i++){
for(int j=0;
j<column; j++){
printf("matrix[%d , %d]= %d \t",i,j, mat[i][j]);
}
printf("\n");
}
}
// Display text menu to all the user to interact with the
program
/*
* I discovered a run time error.
* If the user type a character (e.g. 'p') instead of a a valid row
or column value when try to ...
* the program will enter infinite loop
*/
void AppMenu( int r, int c, int max_column, int
mat[][max_column]){
int option;
int src;
int dst;
int k;
do{
printf("\nSelect any
number between 1 and 9\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:
InitializeMatrix(r, c, max_column, mat);
break;
case 2:
SetMatrixData(r,c,max_column,mat);
break;
case 3:
printf("Enter src row:");
scanf("%d", &src);
printf("Enter src row:");
scanf("%d", &dst);
SwitchRow(r,c,max_column,mat,src,dst);
break;
case 4:
printf("Enter src column:");
scanf("%d", &src);
printf("Enter src column:");
scanf("%d", &dst);
SwitchColumn(r,c,max_column,mat,src,dst);
break;
case 5:
printf("Enter rotation value to rotate to the right:");
scanf("%d", &k);
RotateMatrixRight(r,c,max_column,mat,k);
break;
case 6:
printf("Enter rotation value to rotate to the left:");
scanf("%d", &k);
RotateMatrixLeft(r,c,max_column,mat,k);
break;
case 7:
CompressMatrixByRows(r,c,max_column,mat);
break;
case 8:
CompressMatrixByColumn(r,c,max_column,mat);
break;
case 9:
PrintMatrixData(r,c,max_column, mat);
break;
default: ;//do nothing
}
}while(option > 0 && option
<10);
}
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 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(row, column, max_column, matrix);
return 0;
}
Both the options are working and giving required results. There might be some problem with the compiler you are using, it happens sometimes. I also used two compilers for this same code. It was having problem in one compiler but working fine in other one.