Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 8, Problem 8.1PE
Expert Solution & Answer
Check Mark
Program Plan Intro

Sum of elements column by column

Program Plan:

  • Include the “Scanner” package to get input values from user.
  • Define the class named “Test”.
    • Define the main method.
      • Define the object “obj” for “Scanner” class to get input.
      • Declare the multi-dimensional array named “array”, and prompt the user to get array input.
      • Define the “for” loops, that get the input values for the variable “array”.
      • Define the “for” loop, that call the method “sumColumn()” with two arguments, they are array and index of array.
    • Define the method named “sumColumn()” with two arguments. One is a array variable “m” in type of “double” and another one is “columnIndex” with integer data type.
      • Declare the variable “total” in type of “double” and initialize the variable with “0”.
      • Set the “for” loop, the loop executes from “0” to length of array “m”.
        • Add the column values and store it into the “total”.
        • Return the value of “total”.
Program Description Answer

The following JAVA code is to sum the elements of array column by column using the method “sumColumn(double[][] m, int columnIndex)”.

Explanation of Solution

Program:

//Insert package

import java.util.Scanner;

//Class definition

public class Test

{

//Main method

public static void main(String[] args)

{

//Assign the object for "Scanner" class

Scanner obj = new Scanner(System.in);

//Print statement

System.out.print("Enter a 3 by 4 matrix row by row: ");

//Declaration of variable

double[][] array = new double[3][4];

//Outer loop

for (int i = 0; i < array.length; i++)

//Inner Loop

for (int j = 0; j <array[i].length; j++)

//Get input from user

array[i][j] = obj.nextDouble();

//Loop

for (int j = 0; j < array[0].length; j++)

{

//Print statement with function call

System.out.println("Sum of the elements at column " + j + " is " + sumColumn(array, j));     

}

}

//Function definition

public static double sumColumn(double[][] m, int columnIndex)

{

//Declaration of variable

double total = 0;

//Loop

for (int i = 0; i < m.length; i++)

//Add  the array values into variable

total += m[i][columnIndex];

//Return statement

return total;

}

}

Sample Output

Enter a 3 by 4 matrix row by row:

1.5 2 3 4

5.5 6 7 8

9.5 1 3 1

Sum of the elements at column 0 is 16.5

Sum of the elements at column 1 is 9.0

Sum of the elements at column 2 is 13.0

Sum of the elements at column 3 is 13.0

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Create an original network topology consisting of at least seven routers and twelve links, assigning arbitrary positive weights to each link. Using this topology, apply Dijkstra's Link-State Algorithm to compute the shortest paths from a source router of your choice to all other routers in the network. Your topology must be entirely your own design and should not resemble any examples from the textbook, lecture slides, or other students' work. Al-generated topologies are not permitted. Create a PowerPoint presentation that follows the format and style of slides 11 to 23 from Lecture Slide Set 06 (LS06). You should copy those slides and make any necessary changes, additions, or deletions to reflect your own topology, shortest-path calculations, and update tables. Do not alter the original slide style, layout, or formatting.
Create an original network topology consisting of at least seven routers and twelve links, assigning arbitrary positive weights to each link. Using this topology, apply Dijkstra's Link-State Algorithm to compute the shortest paths from a source router of your choice to all other routers in the network. Your topology must be entirely your own design and should not resemble any examples from the textbook, lecture slides, or other students' work. Al-generated topologies are not permitted. Create
x3003 x3008 1110 0000 0000 1100 1110 0010 0001 0000 0101 0100 1010 0000 x3004 0010 0100 0001 0011 x3005 0110 0110 0000 0000 X3006 0110 1000 0100 0000 x3007 0001 0110 1100 0100 0111 0110 0000 What does the following LC-3 program do? Trace Step by Step, SHOW ALL YOUR WORK. x3001 x3002 0000 x3009 0001 0000 0010 0001 X300A 0001 0010 0110 0001 x300B 0001 0100 1011 1111 x300C 0000 0011 1111 1000 X300D 1111 0000 0010 0101 x300E 0000 0000 0000 0101 x300F 0000 0000 0000 0100 x3010 0000 0000 0000 0011 x3011 0000 0000 0000 0110 x3012 0000 0000 0000 0010 x3013 x3014 0000 0000 0000 0000 0000 0100 0000 0111 x3015 0000 0000 0000 0110 x3016 0000 0000 0000 1000 x3017 0000 0000 0000 0111 x3018 0000 0000 0000 0101
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
9.1: What is an Array? - Processing Tutorial; Author: The Coding Train;https://www.youtube.com/watch?v=NptnmWvkbTw;License: Standard Youtube License