Question

In: Computer Science

Write a C++ program that : 1. Allow the user to enter the size of the...

Write a C++ program that :

1. Allow the user to enter the size of the matrix such as N. N must be an integer that is >= 2 and < 11.

2. Create an vector of size N x N.

3. Call a function to do the following : Populate the vector with N2 distinct random numbers. Display the created array.

4. Call a function to determines whether the numbers in n x n vector satisfy the perfect matrix property. Look at the sample run for the exact output

5. Repeat steps 1 - 4 until the user terminates the program.

The program needs a main and 2 functions

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

#include <vector>

using namespace std;

bool isExisted(vector<vector<int>> matrix, int value);

void generateMatrix(vector<vector<int>> &matrix);

void printMatrix(vector<vector<int>> matrix);

bool isPerfectMatrix(vector<vector<int>> matrix);

int main()

{

    int N;

    string choice;

    do{

        cout<<"Enter the size of the matrix: ";

        cin>>N;

        while(N<2 || N>11){

            cout<<"Size must be >= 2 and < 11. Re-enter: ";

            cin>>N;

        }

        vector<vector<int>> matrix(N);

        for(int i=0; i<N; i++)

            matrix[i] = vector<int>(N);

        generateMatrix(matrix);

        cout<<"Generated Matrix is: "<<endl;

        printMatrix(matrix);

        if(isPerfectMatrix(matrix))

            cout<<"Perfect Matrix"<<endl;

        else

            cout<<"NOT a Perfect Matrix"<<endl;

        cout<<"\nDo you want to continue? ";

        cin>>choice;

    }while(choice=="Y" || choice=="y");

}

bool isExisted(vector<vector<int>> matrix, int value){

    for(int i=0; i<matrix.size(); i++){

        for(int j=0; j<matrix.size(); j++){

            if(matrix[i][j]==value)

                return true;

        }

    }

}

void generateMatrix(vector<vector<int>> &matrix){

    int size = matrix.size();

    for(int i=0; i<size; i++){

        for(int j=0; j<size; j++){

            int num = (rand()%1000)+1;

            if(!isExisted(matrix, num))

                matrix[i][j] = num;

        }

    }

}

void printMatrix(vector<vector<int>> matrix){

    for(int i=0; i<matrix.size(); i++){

        for(int j=0; j<matrix.size(); j++){

            cout<<matrix[i][j]<<"\t";

        }

        cout<<endl;

    }

}

bool isPerfectMatrix(vector<vector<int>> matrix)

{

    int sum = 0;

    int size = matrix.size();

    for (int i = 0; i < size; i++)

        sum = sum + matrix[i][i];

    for (int i = 0; i < size; i++)

    {

        int rowSum = 0;

        for (int j = 0; j < size; j++)

            rowSum += matrix[i][j];

        if (rowSum != sum)

            return false;

    }

    for (int i = 0; i < size; i++)

    {

        int colSum = 0;

        for (int j = 0; j < size; j++)

            colSum += matrix[j][i];

        if (sum != colSum)

            return false;

    }

    return true;

}


Related Solutions

Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it...
Forms often allow a user to enter an integer. Write a program that takes in a...
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or any string with a non-integer character, the output is: no PYTHON 3
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
C Program 1. Write a program that prompts the user to enter 3 integers between 1...
C Program 1. Write a program that prompts the user to enter 3 integers between 1 and 100 from the keyboard in function main and then calls a function to find the average of the three numbers. The function should return the average as a floating point number. Print the average from main.The function header line will look something like this:float average(int n1, int n2, int n3) STOP! Get this part working before going to part 2. 2. Create a...
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
create a program that will allow the user to enter a start value from 1 to...
create a program that will allow the user to enter a start value from 1 to 5, a stop value from 10 to 12 and a multiplier from 1 to 4. the program must display a multiplication table from the values entered. for example if the user enters: start 2, stop 10 and multiplier 3, the table should appear as follows: 3*2=6 3*3=9 3*4=12 . . . 3*10=30
Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT