Question

In: Computer Science

(C++) Write a program that randomly fills in 0s and 1s into a 5-by-5 matrix,prints the...

(C++) Write a program that randomly fills in 0s and 1s into a 5-by-5 matrix,prints the matrix,and finds which row and column with more 1s and display that row and column number.            

Solutions

Expert Solution

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    srand(time(NULL));
    int arr[5][5];

    // randomly fills in 0s and 1s into a 5-by-5 matrix
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            arr[i][j] = rand() % 2;
        }
    }

    // prints the matrix
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;

    // find row with more 1's
    int maxRowIndex = 0, maxOnes = 0;
    for (int i = 0; i < 5; ++i) {
        int count = 0;
        for (int j = 0; j < 5; ++j) {
            if (arr[i][j] == 1)
                ++count;
        }
        if (count > maxOnes) {
            maxOnes = count;
            maxRowIndex = i;
        }
    }
    cout << "row " << maxRowIndex+1 << " is maximum which contains " << maxOnes << " ones" << endl;

    // find column with more 1's
    int maxColumnIndex = 0;
    maxOnes = 0;
    for (int i = 0; i < 5; ++i) {
        int count = 0;
        for (int j = 0; j < 5; ++j) {
            if (arr[j][i] == 1)
                ++count;
        }
        if (count > maxOnes) {
            maxOnes = count;
            maxColumnIndex = i;
        }
    }
    cout << "column " << maxColumnIndex+1 << " is maximum which contains " << maxOnes << " ones" << endl;
    return 0;
}


Related Solutions

Write a C# console program that fills the right to left diagonal of a square matrix...
Write a C# console program that fills the right to left diagonal of a square matrix with zeros, the lower-right triangle with -1s, and the upper left triangle with +1s. Let the user enter the size of the matrix up to size 21. Use a constant and error check. Output the formatted square matrix with appropriate values. Refer to the sample output below. Sample Run: *** Start of Matrix *** Enter the size of square (<= 21): 5 1 1...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Programming In C Write a program that prints the values in an array and the addresses...
Programming In C Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Need to write a c program Program prints a message telling the user to push a...
Need to write a c program Program prints a message telling the user to push a key to start the game. Once in game, output tells the player which key to push. The key to press should be determined randomly. Game runs continuously until the user reaches a loose condition. A wrong key is pressed
Write a C# program that prints a calendar for a given year. Call this program calendar....
Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:            0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Write a C++ program that prints the insurance fee to pay for apet according to...
Write a C++ program that prints the insurance fee to pay for a pet according to the following rules:(NOTE:You must use a switch statement to determine pet fee.)A dog that has been neutered costs $50.A dog that has not been neutered costs $80.A cat that has been spayedcosts $40.A cat that has not been spayedcosts $60.A bird or reptile costs nothing.Any other animal generates an error message.The program should prompt the user for the appropriate information: a character code for...
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
c# language Write a program that takes in a number from the user. Then it prints...
c# language Write a program that takes in a number from the user. Then it prints a statement telling the user if the number is even or odd. If the number is odd, it counts down from the number to 0 and prints the countdown on the screen, each number on a new line. If the number is even, it counts down from the number to 0, only even numbers. For example, if the user enters 5, the output will...
Write a C program that prints the Grade of each student in a class based on...
Write a C program that prints the Grade of each student in a class based on their mark. The program must also print the average mark of the class. The program must prompt (ask) the user for the mark of a student (out of 100). The program must then print the mark entered and the grade received based on the table below. Grade Range A 85 to 100 inclusive B 75 to 85 inclusive C 60 to 70 inclusive D...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT