Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: hey guys I'm having some troubles

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post 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


    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);
     
     
    } 
    }
    Last edited by kenamine; October 4th, 2012 at 11:07 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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:
    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.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: hey guys I'm having some troubles

    Start by getting the class to print the column you need, then move on from there.

Similar Threads

  1. [SOLVED] Troubles with My code
    By grimrader22 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 21st, 2011, 02:46 AM
  2. binarySearch troubles
    By pottsiex5 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 4th, 2011, 11:09 AM
  3. Counter troubles
    By GinoGore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2011, 09:28 AM
  4. [SOLVED] ArrayList Troubles.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2010, 10:19 AM
  5. JSP Troubles
    By sdkeslar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2010, 02:26 PM