Question
Create a Java program with the following requirements: Ask the user to enter the size of an array (int value) Allocate a 2D array of
Create a Java program with the following requirements:
- Ask the user to enter the size of an array (int value)
- Allocate a 2D array of int that size (if the user enters in 5, then allocate a 5x5 array)
- Using a Random, initialize each element of the array to be either 0 or 1
- Output the array in a table format (see below for an example)
- Output the fraction of your array that is ones and the fraction that is zeros. For example, suppose the user asked for a 3x3 array and after initialization that array happened to contain 4 ones and 5 zeros. The fraction of ones would be 4/9 and the fraction of zeros would be 5/9.
- Calculate and output the sum of each row. Output the index of the largest row sum.
- Calculate and output the sum of each column. Output the index of the largest column sum.
- Out rows win if the largest row sum is greater than the largest column sum. Otherwise, output columns win
- Calculate the diagonal sum. Use the diagonal starting at location [0][0] and going down to [N-1][N-1] for an array of size N.
- Output diagonals rule if the diagonal sum is greater or equal to the largest row sum and greater than or equal to the largest column sum.
- Below are three example runs.
Run #1:
enter the size of the array...
4
0110
1100
1111
0011
fraction of ones = 10/16
fraction of zeros = 6/16
row 0 sum = 2
row 1 sum = 2
row 2 sum = 4
row 3 sum = 2
row 2 has the largest row sum!
col 0 sum = 2
col 1 sum = 3
col 2 sum = 3
col 3 sum = 2
col 1 has the largest col sum!
rows win!
diagonal sum is 3
Run #2:
enter the size of the array...
5
01000
01001
01110
01010
11011
fraction of ones = 12/25
fraction of zeros = 13/25
row 0 sum = 1
row 1 sum = 2
row 2 sum = 3
row 3 sum = 2
row 4 sum = 4
row 4 has the largest row sum!
col 0 sum = 1
col 1 sum = 5
col 2 sum = 1
col 3 sum = 3
col 4 sum = 2
col 1 has the largest col sum!
columns win!
diagonal sum is 4
Run #3:
enter the size of the array...
5
10100
11011
00101
11100
00101
fraction of ones = 13/25
fraction of zeros = 12/25
row 0 sum = 2
row 1 sum = 4
row 2 sum = 2
row 3 sum = 3
row 4 sum = 2
row 1 has the largest row sum!
col 0 sum = 3
col 1 sum = 2
col 2 sum = 4
col 3 sum = 1
col 4 sum = 3
col 2 has the largest col sum!
columns win!
diagonal sum is 4
diagonals rule
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started