Question

2. 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 (6 pts) o Test multiple user inputs (3 x 2, 4 x 4, 6 x 2, etc) A sum should be created for each row, each column, and the total for the matrix (9 pts) O Ex.: How big would you like your matrix? Rows - ? 3 Columns -? 2 Please enter your row 1? Column 1 - 3 Column 2 - 4 Please enter your row 2? Column 1 -7 Column 2- 40 Table 1 - An example of a 3 x 2 matrix 40 Please enter your row 3? Column 1 - 20 Column 2 - 12 20 12 Your total for row 1 is -7 Your total for row 2 is - 47 Your total for row 3 is -32 Your total for column 1 is - 30 Your total for column 2 is -56 Your total for the 3 x 2 matrix is 86

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

/*java program that calculate the sum of a variable sized matrix using a two dimensional array*/

import java.util.Scanner;
class ArraySum
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int rows,columns,i=0,j=0;
int sum1=0,sum2=0;
int a[][]=new int[10][10];
System.out.println("How big would you like your matrix?");
System.out.println("Rows");
rows=s.nextInt(); /* reading row value*/
System.out.println("Columns");
columns=s.nextInt(); /* reading column value*/

for(i=0;i<rows;i++) /* reading values into matrix*/
{
System.out.println("\n please enter your row "+(i+1));
for(j=0;j<columns;j++)
{
System.out.println(" Column "+(j+1));   
a[i][j]=s.nextInt();
}
}
System.out.println();

System.out.println("the matrix entered is");
for(i=0;i<rows;i++) /* displaying the matrix*/
{
System.out.println();
for(j=0;j<columns;j++)
{
System.out.print("\t");
System.out.print(a[i][j]);
}
}
System.out.println(); /* to give one line of space*/
System.out.println();
for(i=0;i<rows;i++) /* calculating sum of each row */
{
sum1=0;
for(j=0;j<columns;j++)
{
sum1=sum1+a[i][j];
}
System.out.println("your total for row "+(i+1)+" is "+sum1);
  
}
System.out.println();

for(i=0;i<columns;i++) /* calculating sum of each column */
{
sum2=0;
for(j=0;j<rows;j++)
{
sum2=sum2+a[j][i];
}
System.out.println("your total for columns "+(i+1)+" is "+sum2);
  
}
System.out.println();
int sum=0;

for(i=0;i<rows;i++) /* calculating total of all elements in the matrix*/
for(j=0;j<columns;j++)
sum=sum+a[i][j];
System.out.println("your total for given matrix is"+sum);
}
}

Output 1:

  

Output 2:

Output 3:

Add a comment
Know the answer?
Add Answer to:
Write a Java program that calculates the sum of a variable sized matrix using a two...
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
  • Write a program that reads a matrix from the keyboard and displays the summations of all...

    Write a program that reads a matrix from the keyboard and displays the summations of all its rows on the screen. The size of matrix (i.e. the number of rows and columns) as well as its elements are read from the keyboard. A sample execution of this program is illustrated below: Enter the number of rows of the matrix: 3 Enter the number of columns of the matrix: 4 Enter the element at row 1 and chd umn 1: 1...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e....

    Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e. a two-dimensional array), number of rows and number of columns as input and prints the matrix after rotating it 90 degrees clockwise. Example: void rotate-matrix (int* matrix, int row, int column) Inputs: row 4, column-6 and the pointer to the matrix below: 2 3 6 5 8 0 Output:

  • 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...

  • JAVA Write a method that returns the sum of all the elements in a specified column...

    JAVA Write a method that returns the sum of all the elements in a specified column in a matrix using the following header: public static double sumColumn(double [][] m, int columnIndex) Write another method that returns the sum of all the elements in a specified row in a matrix using the following header: public static double sumRow( double [][] m, int rowIndex) Write a test program that reads a 3-by-4 matrix and displays the sum of each column and sum...

  • /*************************************************** Name: Date: Homework #7 Program name: HexUtilitySOLUTION Program description: Accepts hexadecimal numbers as input. Valid...

    /*************************************************** Name: Date: Homework #7 Program name: HexUtilitySOLUTION Program description: Accepts hexadecimal numbers as input. Valid input examples: F00D, 000a, 1010, FFFF, Goodbye, BYE Enter BYE (case insensitive) to exit the program. ****************************************************/ import java.util.Scanner; public class HexUtilitySOLUTION { public static void main(String[] args) { // Maximum length of input string final byte INPUT_LENGTH = 4; String userInput = ""; // Initialize to null string Scanner input = new Scanner(System.in); // Process the inputs until BYE is entered do {...

  • Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix...

    Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...

  • We use JAVA. Thanks.    Create an application whose main method asks the user to enter...

    We use JAVA. Thanks.    Create an application whose main method asks the user to enter an n by m integer matrix that contains nm integer numbers. n and m should be between 1 and 10. If the user enters a             number less than 1 or greater than 10, the program will continue to ask the user to enter an integer number between 1 and 10. The program should print the sum of the boundary elements of the matrix....

  • 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:...

  • 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...

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