Question

In: Computer Science

I need to fix this code, and could you please tell me what was the problem...

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;
}

Solutions

Expert Solution

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.


Related Solutions

Could someone please tell me what corrections I should make to this code. (Python) Here are...
Could someone please tell me what corrections I should make to this code. (Python) Here are the instructions. Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating I can't get the find function to work and I have no idea how to even go about it. For example, when type in 'find' and...
Can you please tell me what more information I need to provide to solve below problem?...
Can you please tell me what more information I need to provide to solve below problem? 1. Find out below using R Hints: Use iris dataset from R (built in data set in R) a) Create a new data frame called virginica.versicolor (that only contains these two species) the command I used: virginica.versicolor <- iris[iris$Species %in% c("versicolor", "virginica"), ] b) What is your null hypothesis regarding sepal lengths for the two species (virginica.versicolor) ? And what is your alternate hypothesis?...
Could you please tell me what it is?use what theroy? Rather than exporting or FDI I...
Could you please tell me what it is?use what theroy? Rather than exporting or FDI I can negotiate a contract that gives another company the right to make my product in their country.This is known as: When an immigrant sends money to his or her family back in their home country. (it’s difficult to measure) When more expensive goods sourced from inside a customs union replace the consumption of less expensive goods from outside the customs union. A situation in...
Can anyone please tell me what function in excel I could use in this instance: I...
Can anyone please tell me what function in excel I could use in this instance: I have two spreadsheets with sales of products for a company. I am needing to create a new spreadsheet with gross sales by month and by region. It haven't included all the worksheets, because I truly just need pointed in the right direction.
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
What are the main point and theory in glucose metabolism? Could you please tell me in...
What are the main point and theory in glucose metabolism? Could you please tell me in details ?
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
please correct the error and fix this code: (i need this work and present 3 graphs):...
please correct the error and fix this code: (i need this work and present 3 graphs): Sampling_Rate = 0.00004; % which means one data point every 0.001 sec Total_Time = 0:Sampling_Rate:1; % An array for time from 0 to 1 sec with 0.01 sec increment Omega = 49.11; % in [rad/s] zeta=0.0542; %unitless Omega_d=49.03; % in [rad/s] Displacement_Amplitude = 6.009; % in [mm] Phase_Angle = 1.52; % in [rad] Total_No_of_Points = length(Total_Time); % equal to the number of points in...
What I need is for you diagnose, Tell me why you chose that diagnosis. Signs, Symptoms,...
What I need is for you diagnose, Tell me why you chose that diagnosis. Signs, Symptoms, Age, lifestyle, any other factors to lead you to your conclusion. Give some diagnostic tests to confirm your Dx. Then a treatment plan. Patient presents no symptoms occurring at visit. Patient states he has started training for a marathon but has to stop due to tightness in chest and coughing. Patient does not know if there is any wheezing as he runs with an...
What I need is for you diagnose, Tell me why you chose that diagnosis. Signs, Symptoms,...
What I need is for you diagnose, Tell me why you chose that diagnosis. Signs, Symptoms, Age, lifestyle, any other factors to lead you to your conclusion. Give some diagnostic tests to confirm your Dx. Then a treatment plan. Patient presents with a sore on his right big toe. Patient states sore has been there for 10 days and is not getting better if anything he thinks it is looking worse and fears infection. Patient also states his leg has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT