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...
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!
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.
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...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT