Question

Write a java program that displays the sum of the elements in Column x of a...

Write a java program that displays the sum of the elements in Column x of a 2D array (careful with rows of different lengths!)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:-

import java.util.*;

public class HomeworkLib {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of rows and columns: ");

        int rows = sc.nextInt(); // taking the number of rows and columns from the user

        int columns = sc.nextInt();

        int data[][] = new int[rows][columns];

        System.out.println("Enter the data:- ");

        for (int i = 0; i < rows; i++) { // now taking the data from the user

            for (int j = 0; j < columns; j++) {

                data[i][j] = sc.nextInt();

            }

        }

        System.out.print("Enter the value of columns you want the sum of: ");

        int x = sc.nextInt(); // taking the value of x

        int sum = 0;

        for (int i = 0; i < rows; i++) { // priting the data of the array

            for (int j = 0; j < columns; j++) {

                System.out.print(data[i][j] + " ");

            }

            System.out.println();

        }

        for (int i = 0; i < rows; i++) {

            sum += data[i][x - 1]; // calculating the sum of the array of that column

            // by keeping the value of the column fixed

        }

        System.out.println(sum);

    }

}

FOR ANY OTHER QUERY PLEASE COMMENT.

Add a comment
Know the answer?
Add Answer to:
Write a java program that displays the sum of the elements in Column x of a...
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
  • Using your choice of Pseudocode, C# or Java write a program that displays the sum of...

    Using your choice of Pseudocode, C# or Java write a program that displays the sum of all of the even elements in a 3x3 matrix (e.g., a 2D array). If there is no such number in the matrix it will display 0.

  • Write a Java program that does the following. a. Declare an integer 2D array with 5...

    Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.

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

  • Sum elements column by column

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

  • Write a program that reads k integers and displays their sum. Write a program that reads...

    Write a program that reads k integers and displays their sum. Write a program that reads k values representing family income from the keyboard and place them into the array. Find the maximum income among these values. Count the families that make less than 10 percent of the maximum income. (JAVA)

  • java Write methods for 2d 1. a method to calculate the sum of a 2d double...

    java Write methods for 2d 1. a method to calculate the sum of a 2d double array 2. a method to calculate the sum of each row of a 2d double array 3. a method to calculate the sum of each column of a 2d double array 4. a method to calculate the average of a 2d double array 5. a method to calculate the average of each row of a 2d double array 6. a method to calculate the...

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

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

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

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