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

  • Write a C Program that finds the row totals and column totals of a Matrix. The...

    Write a C Program that finds the row totals and column totals of a Matrix. The user is prompted to enter the numbers to fill in a two-dimensional array of any size (say 3x3). The program then finds the sum of row elements and prints them along-side rows, and finds the sum of columns and prints them along-side corresponding columns. You will need nested for loops please help!

  • For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...

    For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...

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

  • *****In C++***** Exercise #1: Develop a program that prints out the sum of each column of...

    *****In C++***** Exercise #1: Develop a program that prints out the sum of each column of a two-dimensional array. The program defines method sumColumn() takes a two-dimensional array of integers and returns a single-dimensional array that stores the sum of columns of the passed array. The program main method prompts the user to enter a 3-by-4 array, prints out the array, and then calls method sumColumns(). Finally, it prints out the array retuned by method sumColumns(). Document your code, and...

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

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