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 Plus MyProgrammingLab with Pearson EText -- Access Card Package
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)
- 3-Write a test program that reads a n-by-m matrix Reads a real numbers and computes the sum and average.arrow_forwardCreate a program for Matrix with the following requirements: 1. Allow the user to select what operation to perform like: finding eigenvalues and eigenvectors, sketching the Gershgorin circles, and diagonalization. 2. Allow the user to input any size of the matrices and their elements. 3. Display the matrix input and the result of the operation.arrow_forward2-Write a test program that reads a 4-by-4 matrix and displays the multiply of each rowarrow_forward
- Task 01: Assume P(x,y) is (x+y>4). Write a function/method that can validate the nested quantifier Vy P(x,y) for elements from two arrays X and Y. i. hint: The method returns l if statement is valid (the sum of all pairs of values from X and Y is greater than 4), and returns 0 otherwise. Valid case: For arrays X={2,3,4} and Y={3,4,5), the method returns 1(since xEX and býyEY, xi+y>4) Invalid case: For arrays X={2,3,4} and Y={1,4.5}, the method returns 0 (since 2EX and l EY but 2+1<4)arrow_forwardWrite method body of following methods: DeleteNum(int position) // This method will delete element from array at position entered by the user arrange the data after that position to left. (Using While loop) DeleteNum(int from, int to) // This method will delete elements from array at range given by the user, as the user inputs two positions i.e start and end, the program should than delete all numbers in that range. (Use C++ to code)arrow_forward(Two dice are rolled 100 times (you can do this with two random values from 1 to 6 in your program). After rounding 100 times, keep the iteration value of how many times each state occurs in a two-dimensional array. For example, the index number of [1,1] must follow the number of times the dice rolls 1 to 1).arrow_forward
- (FYI: Pseudocode is required (Not any programming language) Design a pseudocode program that loads an array with the following 7 values. Add one more word (of your own choosing) for a total of 8 words. biffcomelyfezmottleperukebedraggledquisling Be sure to use lowercase, as shown above. This will make the processing easier. Ask the user to enter a word Search through this array until you find a match with the word the user entered. Once you find a match, output "Yes, that word is in the dictionary". If you get to the end of the array and do NOT find a match, output "No, that word is not in the dictionary". The program should work with any set of words in the arrays. If I were to change the words in the arrays, it should still work. If you need help, look at the search example in your textbook.arrow_forwardAlert dont submit AI generated answer.arrow_forwardJava Objective:Design and implement simple matrix manipulation techniques.Details:Your java program should use 2D arrays to implement simple matrix operations.Your program should do the following:• Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order• Print M1 to the console; make sure you format as a matirx• Repeat the previous steps for a second matrix M2• Create a matrix M3 that is the transpose of M1 and print it to the console• Check if M1 and M2 can be added (should have the same dimensions). If possible, add M1 and M2 and print the result to the console. Otherwise print an error message.• Multiply M1 and M2 if possible and print to the console. If the matrices cannot be multiplied, print an error message. Implementation requirements:• Use a helper method for reading a positive integer using an input validation loop.• Use a helper method for printing…arrow_forward
- what is correct answer?arrow_forward(Random sentences and story writer) Write an app that uses random-number generation to create sentences. Use four arrays of strings, called article, noun, verb andprepostion. Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article, noun.As each word is picked, concatenate it to the previous words in the sentence. The words should be separated by spaces. When the sentence is output, it should start witha capital letter and end with a period. The program should generate 10 sentences and output them to a text box. The arrays should be filled as follows:The article array should contain the articles "the", "a", "one", "some" and "any";The noun array should contain the nouns "boy", "girl", "dog", "town", "car";The verb array should contain the past tense verbs "drove", "jumped", "ran", "walked" and "skipped";The preposition array should contain the preposotions "to", "from", "over", "under" and "on";arrow_forward(Compute total marks for each student) Suppose the marks obtained by all students are stored ina two-dimensional array. Each row records a student’s marks for five tests. For example, the followingarray stores the test marks for eight students. Write a program that displays students and the totalmarks they obtained in five tests, in decreasing order of the total marks.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education