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...
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...
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
(C++) Write a program that prints a table in the following format given a "x" read...
(C++) Write a program that prints a table in the following format given a "x" read from the keyboard. For example, if x is 4, it prints out 0 0 1 1 2 4 3 9 4 16 To avoid mismatch, put 8 white spaces between two numbers.
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT