Introduction to Java Programming and Data Structures, Comprehensive Version Plus MyProgrammingLab with Pearson EText -- Access Card Package
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
Part B - Implement the Queue in LC-3 Now implement a similar circular queue in LC-3 assembly. Data structure in memory Use the following layout (similar style to the stack code from class): Q_BASE .FILL X4100; base address of queue array ; capacity (in elements) Q_CAPACITY.BLKW #1 Q_HEAD .BLKW #1 QTAIL .BLKW #1 Q_SIZE .BLKW #1 ; index of front element (0..capacity-1) ; index one past last element (0..capacity-1) ; current number of elements (0..capacity) We will use this calling convention: ENQUEUE Input: RO = value to enqueue Output: R5 = 1 if success R5 = 0 if failure (queue full; queue unchanged) DEQUEUE Input: none Output: If queue not empty: RO dequeued value R5 = 1 (success) If queue empty: R5 = 0 (failure; queue unchanged; RO don't care) B1. Subroutine Queuelnit Write an LC-3 subroutine Queuelnit with this behavior: Input: RO capacity (number of elements, e.g., 5) Effects: Stores capacity into Q CAPACITY Sets Q HEAD = 0 Sets Q TAIL = 0 Sets Q SIZE = 0 RO should be restored to…
A2. Trace the queue operations Draft the Queue by hand. Assume we start with an empty queue. head = 0, tail = 0, size = 0 data = [?, ?, ?, ?, ?] (contents unknown at first) Trace the following sequence step by step: enqueue(10) enqueue(20) enqueue(30) dequeue() enqueue(40)
Part C - Manual LC-3 Trace (Registers + Memory) In this part, you will simulate your LC-3 queue by hand. Assume the following initial conditions in memory: Q_BASE = x4100 (Q_BASE..Q_BASE+4) are initially unknown (don't care) Q CAPACITY = 5 Q_HEAD = 0 QTAIL = 0 Q_SIZE = 0 And assume your main program executes this sequence of calls: RO <- #5 JSR QueueInit RO <- #7 JSR ENQUEUE RO <- #3 JSR ENQUEUE RO <- #9 JSR ENQUEUE JSR DEQUEUE RO <- #5 JSR ENQUEUE JSR DEQUEUE JSR DEQUEUE JSR DEQUEUE ; one extra dequeue
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