Question

this assignment need to be done in java only Requirements: Ask the user to enter the...

this assignment need to be done in java only

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.

please comments on the code so i can understand

0 0
Add a comment Improve this question Transcribed image text
Answer #1
this is the solution

import java.util.*;
/* create a class named solution*/
class Solution{
    public static void main(String args[]){
        /* declare variables*/
        int n;/*size of the array*/
        int i,j;/*variables to travaerse the array*/
        Scanner s=new Scanner(System.in);
        Random rd = new Random();/* to generate random numbers*/
        System.out.println("Enter the size of the array");
        n=s.nextInt();
        /*to generate an 2d array of random numbers*/
        int arr[][]=new int[n][n];/*2d array named arr of size n*n is created*/
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                arr[i][j]=rd.nextInt(2);/* range 2 means from 0 to 1*/
               
            }
        }
        /*to print the nymbers in a tabular form */
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                System.out.print(arr[i][j]+" ");
            }
            System.out.println("");
        }

        /*to find the fraction of ones and zeroes*/

        int ones=0;/*stores count of 1's*/
        int zeroes=0;/*stores count of 0's*/
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                if(arr[i][j] == 1){
                    ones=ones+1;
                }
                else{
                    zeroes=zeroes+1;
                }
            }
           
        }

        int size=ones+zeroes;/*total number of elements in the array*/
       
        System.out.println("The fraction of ones is "+ones+"/"+size);
        System.out.println("The fraction of zeroes is "+zeroes+"/"+size);
        int rowsum;
       
        int columnsum;
       
        int maxrow=0,index1=0,index2=0;
        /*to find the row sum*/
        for(i=0;i<n;i++){
            rowsum=0;
            for(j=0;j<n;j++){
                rowsum=rowsum+arr[i][j];
               
            }
            System.out.println("the sum of row "+(i+1)+" is "+rowsum);
            /*finding the maximum of the rowsum and storing its index in index 1 and 2*/
            if(rowsum>maxrow){
                maxrow=rowsum;
                index1=i;
                index2=j;
            }
           

        }
        System.out.println("maximum row sum is at the row index "+(index1+1)+" column index "+(index2));
        int maxcolumn=0;
        index1=0;
        index2=0;
        /*finding the maximum of the columnsum and storing its index in index 1 and 2*/
        for(i=0;i<n;i++){
            columnsum=0;
            for(j=0;j<n;j++){
                columnsum=columnsum+arr[j][i];
            }
            System.out.println("the sum of column "+(i+1)+" is "+columnsum);
            if(columnsum>maxcolumn){
                maxcolumn=columnsum;
                index1=i;
                index2=j;
            }
           
        }
        System.out.println("maximum column sum is at the row index"+(index1+1)+" column index"+(index2));

        if(maxrow>maxcolumn){
            System.out.println("rows wins");

        }
        else if(maxrow<maxcolumn){
            System.out.println("column wins");
        }
        else{
            System.out.println("equal");/*there is a occurence where both the rowsum and column sum are equal.so this
                                        will print equal.you can remove if you dont want this*/
        }

        int diagsum=0;
        for(i=0;i<n;i++){
   
                diagsum=diagsum+arr[i][i];/*to add the all diagnol elements*/

        }
        System.out.println("the diagnol sum is "+diagsum);

        if((diagsum>=maxrow) && (diagsum>=maxcolumn)){
            System.out.println("diagnols rules");
        }

    }

}

The out put is

Add a comment
Know the answer?
Add Answer to:
this assignment need to be done in java only Requirements: Ask the user to enter the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...

  • How do I modify this to let the user input the size of the matrix? The...

    How do I modify this to let the user input the size of the matrix? The lowest size is 4 and the largest size is 16. import java.util.*; public class Test1 {      public static void main(String[] args)      { int[][] matrix = new int[4][4];    // create 4 by 4 matrix (need user input???)           // generate 1's and 0's for each each rows and columns      // and track largest row index with the most ones         int largestRI...

  • Magic Squares; Java code

    Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, ..., n2 is a magic square if the sum ofthe elements in each row, in each column, and in the two diagonals is the same value. For example,16 3 2 135 10 11 89 6 7 124 15 14 1Write a program that reads in n2 values from the keyboard and tests whether they form a magic squarewhen arranged as a square matrix. You...

  • please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class...

    please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class Chapter9a_FillInTheCode { public static void main(String[] args) { String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"}, {"Detroit", "Newark", "Boston", "Seattle"}}; // ------> exercise 9a1 // this code prints the element at row at index 1 and column at index 2 // your code goes here System.out.println("Done exercise 9a1.\n"); // ------> exercise 9a2 // this code prints all the states in the...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • I want to change this C code so that instead of prompting the user to enter...

    I want to change this C code so that instead of prompting the user to enter the argument value, or requiring the argument be entered after by pressing the “enter” key, instead I want the code changed so that the program opens a file, (which include the input values) read in the elements and pass in the input file as a command line argument. (the input file includes the size of the matrix and the matrix values) Here's an example...

  • I need help as quick as possible, thanks beforehand. Please provide the test output The Lo...

    I need help as quick as possible, thanks beforehand. Please provide the test output The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

  • Using Java In this assignment we are working with arrays. You have to ask the user...

    Using Java In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT