Question

In: Computer Science

Problems Background work with multidimensional arrays. Your C program should enable the end-user to create any...

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;

}

Solutions

Expert Solution

// 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.


Related Solutions

Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Create a class called clsTextProcessor.java. Your program should deliver the following: Asks the user for a...
Create a class called clsTextProcessor.java. Your program should deliver the following: Asks the user for a location of a text file, with (FileNotFound) exception handling: Keep asking for a valid file name and location If an invalid file is provided, display an error message. Once provided, proceed. Opens the text file and finds the following: Minimum value Maximum value The average value of all number in the text Prints a message displaying the values in step No. 2
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
create a program that will verify a user's login credentials. The program should prompt the user...
create a program that will verify a user's login credentials. The program should prompt the user to enter his/her username and password at the keyboard. Then it should read the data from a data file named "login.txt". The file "login.txt" will contain a list of 3 valid usernames and passwords to verify the login information supplied by a user.  If the username and password entered by the user matches one of the sets read from the file, the program should print...
Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...
Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART A: (Minusculo Inn) Minusculo Inn has only eight guest rooms: Room Number Amenities 101 1 king size bed 102, 103,104 2 double beds 201, 202 1 queen size bed 203, 204 1 double bed & sofa bed Write a program that asks for the room number and displays the amenities. If the user enters a room number that does...
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
Language: C# Create a new Console Application. Your Application should ask the user to enter their...
Language: C# Create a new Console Application. Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric input that is entered to make...
Create a C++ program that will ask the user for how many test scores will be...
Create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. (no fstream) The data needs to include the student’s first name, student number test score the fields should be displayed with a total width of 15. The prompt should be printed with a header in the file explaining what each is: ex. First Name student number Test Score 1) mike 6456464   98 2) phill...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT