Concept explainers
(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 static double sumColumn(double[][] m, int columnIndex)
Write a test
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”.
- Define the main method.
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;
}
}
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?
Chapter 8 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out With Visual Basic (8th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
Programming in C
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Digital Fundamentals (11th Edition)
- Write statements to do the following: i. Write a loop that computes the sum of all elements in the array, A ii. Write a loop that finds the minimum element in the array, A iii. Write a method to find the maximum in the array, Aarrow_forward0.2 4.2 1) M= [ 6.1 8.0 5_3] , Do NOT rewrite the matrix just write a code to make 22 1.0 1.5 4.2 6.1 5.8 0.0 10 M=arrow_forward4. Given an array of ints, return the number of times that two 6's are next to each other in the array.int n[] = {2, 6, 71, 6, 6, 6, 34, 6, 6, 89}; C++arrow_forward
- How do you reverse an array?arrow_forwardWhy does this return an empty array and not 'G','H','I','J','K','B' x = ['A','B','C','D','E','F','G','H','I','J','K',]k = x[6:1]print(k)arrow_forwardMat lab code: Create A(3,3) matrix of zeroes , and A(3,3) matrix of ones? display the results for each one ?arrow_forward
- take & array from user and print all the elements after doubling its and you have to separate function for doubling operation.arrow_forwardCan we change the size of an array at run time?arrow_forwardAn array index should normally be of type float. State whether the statement is true or false. If false, explain why.arrow_forward
- Withour array pleasearrow_forwardDeclare two characters array And take input from user in first array and at last index take slach zero (/0) similarly we take input in second array and at end /0. And take another array and copy first array in this blank array and when we come to /0 then copy second array in blank array. Example First array (this is a) Second array (book) Blank array (this is a book) Don't use built-in functionsarrow_forward8. using c++, Write a function method that determines the mean of all the values in an array of integers. Your mean function should call a separate function that you write that determines the sum. Don’t use built-in sum or mean gadgets, but roll your own.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning