hey guys I'm having some troubles
My assignment is to
Write a method that returns the sum of all the elements in a specified column in a matrix using the following header:
public stat double sumColumn(double[][] m, int columnIndex
Also write a test program that reads a 3-by-4 matrix and displays the sum of each column.
And this is what I have so far
Code Java:
import java.util.Scanner;
public class Matrix{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a 3 by 4 matrix row by row: ");
double[][] m = new double[3][4];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
m[i][j] = input.nextDouble();
}
public static double sumColumn(double[][] m, int columnIndex) {
int sum = 0;
for (int i = 0; i < m.length; i++)
total += m [i][j];
System.out.println("Sum for column" + i + " is " + total);
}
}
Re: hey guys I'm having some troubles
{What your program should do} -check
{What you have done so far} -check (although you should keep indentation in the code when posting)
{Your question} -I do not see your question.
Re: hey guys I'm having some troubles
how would you Write a method that returns the sum of all the elements in a specified column in a matrix using the following header:
public stat double sumColumn(double[][] m, int columnIndex
Re: hey guys I'm having some troubles
Hi kenamine
I will try to help you understand how to solve the problem.
You are given instructions to:
Quote:
Write a method that returns the sum of all the elements in a specified column in a matrix using the following header:
public stat double sumColumn(double[][] m, int columnIndex
Also write a test program that reads a 3-by-4 matrix and displays the sum of each column.
So think about how you would solve it without using a computer.
You will need the matrix which has the elements to be added.
You will need to know the column number to work with.
Both of these things are going to be provided when the method is called as per instructions.
Now you need to find some way to iterate through each element in the correct column of the matrix.
As you go through each one, add the value to a running total until you have gone through the whole column.
Return the total once that has completed.
Re: hey guys I'm having some troubles
Start by getting the class to print the column you need, then move on from there.