Question

Row and columns sum Create a java program that: Input: Receive as input in command line...

Row and columns sum

Create a java program that:

  • Input:
  • Receive as input in command line the sizes m and n of a two dimensional array
  • Receive as input in command line the minValue and maxValue between which the random numbers will be generated for your two dimensional array
  • Generate:
  • Generate a two-dimensional with numbers between minValue and maxValue (inclusive) with the given size (m x n)
  • Compute:
  • Compute the sum for each row and store the results in a new array.
  • Compute the sum of sums for rows and store the results in a variable.
  • Compute the sum for each column and store the results in a new array.
  • Compute the sum of sums for columns and store the results in a variable.
  • Print:
  • Print the array as a table with the computed sums per rows and columns.
  • Print the two sums of sums computed and check and print if they match

Run the program 5 times with representative sizes

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

Code -

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   //reading from command line arguement , give 4 value in command line arguemnet
   //m for row
   int m = Integer.parseInt(args[0]);
   //n for column
   int n = Integer.parseInt(args[1]);
   //min value
   int minValue = Integer.parseInt(args[2]);
   //max value
   int maxValue = Integer.parseInt(args[3]);
   //create random object
   Random r = new Random();
   //variable and arrays decalare to use
int i, j;
int a[][] = new int[m][n];
int sumRow[] = new int[m];
int sumRows=0;
int sumCol[] = new int[n];
int sumCols=0;
//generate random value and store in array a
for(i = 0;i<m;i++){
for(j = 0;j<n;j++){
a[i][j] =r.nextInt(maxValue-minValue) + minValue;
}   
}
//calculate sum of rows and store in sumRow
for(i = 0;i<m;i++){
for(j = 0;j<n;j++){
sumRow[i] += a[i][j];
}   
}
//calculate sum of sum of rows
for(i = 0;i<m;i++){
sumRows+=sumRow[i];
}

//calculate sum of columns and store in sumCol
for(i = 0;i<m;i++){
for(j = 0;j<n;j++){
sumCol[i] += a[j][i];
}   
}
//calculate sum of cols
for(i = 0;i<m;i++){
sumCols+=sumCol[i];
}
//print the matrix as sum
for(i = 0;i<m;i++){
for(j = 0;j<n;j++){
System.out.printf("%4d ", a[i][j]);
if(j == n-1){
System.out.print(" | "+sumRow[i]);
}
}   
System.out.println();
}
System.out.println("------------------------");
for(i = 0;i<n;i++)
System.out.printf("%4d ",sumCol[i]);
System.out.printf(" |%4d",sumRows);
System.out.println("\n\n");
System.out.println("Sum rows is "+sumRows);
System.out.println("Sum columns is "+sumCols);
   }
}

Screenshots -

13 11 16 10 8 9 13 136 12 1 31 8 1 33 -- -- --- --- --- -- 40 27 33 | 100 Sum rows is 100 Sum columns is 100

14 16 19 15 1 18 3 133 4 1 28 12 1 31 - -- --- -- --- -- --- 24 49 19 192 Sum rows is 92 Sum columns is 92

12 15 14 1 13 7 9 7 1 1 22 1 35 1 22 41 21 17 I 79 Sum rows is 79 Sum columns is 79

8 4 2 16 4 14 12 19 12 1 36 | 27 1 28 - -- --- -- --- --- - -- 14 34 43 91 Sum rows is 91 Sum columns is 91

8 10 2 10 12 11 9 14 1 12 27 36 25 20 33 35 1 88 Sum rows is 88 Sum columns is 88

Add a comment
Know the answer?
Add Answer to:
Row and columns sum Create a java program that: Input: Receive as input in command line...
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
  • Sum of two matrices

    Create a program that:Receives as input in command line the sizes [rows] and [cols] of a two dimensional array, and [minValue] and [maxValue] between which the random numbers will be generated. Assume maxValue <= 999Generates two random two dimensional array with numbers between minValue and maxValue (inclusive) with the given size (rows x cols), and store them in m1 and m2Compute the sum of the two matrices and stores the result in a new matrix mSum.

  • this language is JAVA 2. Design a class named Location for locating a maximum value and...

    this language is JAVA 2. Design a class named Location for locating a maximum value and its location in a two-dimensional array. The class contains public data fields row, column, and maxValue that store the maximal value and its indices in a two-dimensional array with row and column as int types and maxValue as a double type. Write the following method that returns the location of the largest element in a two-dimensional array: Public static Location locateLargest(double [][] a) Create...

  • Write a Python program that reads user input from the command-line. The program should define a...

    Write a Python program that reads user input from the command-line. The program should define a function read Position, which reads the values for t, v0, and h0. If there is an IndexError, print 'Please provide the values for t, vO, and hO on the command line.'. If t, v0, or h0 are less than 0. print 't = # is not possible.' for each variable respectively. Note that the # represents the number entered on the command-line by the...

  • create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array...

    create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows. Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Use a method containing a nested for loop to compute the average of the doubles in each row. Use a method to...

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • Write a C++ program that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60...

    Write a C++ program that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the update separately as...

  • Write a Java program that calculates the sum of a variable sized matrix using a two...

    Write a Java program that calculates the sum of a variable sized matrix using a two dimensional array. (ArraySum.java) The user should provide the number of rows and columns Test multiple user inputs (3 times 2, 4 times 4, 6 times 2, etc) A sum should be created for each row, each column, and the total for the matrix Ex.: How big would you like your matrix? Rows - ? 3 Columns -? 2 Please enter your row 1? Column...

  • Write code to create a two-dimensional array named numbers containing 500 random numbers from 1 to...

    Write code to create a two-dimensional array named numbers containing 500 random numbers from 1 to 1000 inclusive. The array has 50 rows. Then walk through the array and output the random numbers 10 to a line. Format the numbers in right-aligned columns. Use the command prompt window for output. Note: do NOT create a class or main.

  • Code in Java and implement (source code) a program (name it WeeklyHours) to compute the total...

    Code in Java and implement (source code) a program (name it WeeklyHours) to compute the total weekly hours for 3 employees. The program main method defines a two-dimensional array of size 3x7 to store employers’ daily hours for the week. Each row represents one employee and each column represent one day of the week such that column 0 designates Monday, column 1 designates Tuesday, etc. The program main method populates the array with random numbers between 0 and 10 and...

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

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
ADVERTISEMENT