Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default Trying to sum a column of a 2d array....

    if you guys can help that would be great. thanks
    what i need help with is bolded.

    public class projectTwo
    {

    public static void main (String [] args)
    {
    int [][] testData = { { 100, 99, 98, 97},
    { 96, 95, 94, 93 } };

    int row=0;
    int col=0;

    int total= getTotal(testData);
    float average= getAverage(testData);
    int rowTotal= getRowTotal(testData);
    int columnTotal= getColumnTotal(testData);
    int highestInRow= getHighestInRow(testData);
    int lowestInRow= getLowestInRow(testData);


    System.out.println("Test scores total:" + total + " " + "Average score:" + average + " " + "Row total:" +
    rowTotal +" " + "Column total:" + columnTotal +" " + "Highest:" + highestInRow +" " + "Lowest:" + lowestInRow);

    }


    public static int getTotal(int [][] testData)
    {
    int total=0;
    for (int row=0; row<testData.length; row++)
    {

    for (int col=0; col<testData[row].length; col++)
    { total+= testData[row][col];
    }
    }
    return total;
    }


    public static float getAverage(int [][] testData)
    {float total = 0;

    for (int row = 0; row < testData.length; row++)
    {
    for (int col = 0; col<testData [row].length; col++)
    total +=testData[row][col];
    }

    return (total/8);
    }


    public static int getRowTotal (int[][] testData)
    { int rowTotal;
    return rowTotal;

    }


    public static int getColumnTotal (int [][] testData)
    {int columnTotal;
    return columnTotal;}




    public static int getHighestInRow (int [][] testData)
    {int highestInRow = testData[0][0]; for (int row = 0; row < testData.length; row++)
    { for (int col = 0; col < testData[row].length; col++)
    {
    if (testData[row][col] >= highestInRow)
    highestInRow = testData[row][col]; }
    }
    return highestInRow;

    }


    public static int getLowestInRow (int [][] testData)
    { int lowestInRow = testData[0][0];
    for (int row = 0; row < testData.length; row++)
    { for (int col = 0; col < testData[row].length; col++)
    { if (testData[row][col] <= lowestInRow)
    {
    lowestInRow = testData[row][col]; }
    }
    }
    return lowestInRow;
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Trying to sum a column of a 2d array....

    What are the row total(s) and colum total(s) supposed to be when you calculate them by hand?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 11
    Last Post: 10-21-2014, 05:46 AM
  2. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 07:10 PM
  3. Replies: 1
    Last Post: 03-03-2011, 07:43 AM
  4. Replies: 1
    Last Post: 07-06-2010, 05:50 PM
  5. Extracting a Row/Column from An Array
    By besweeet in forum New To Java
    Replies: 10
    Last Post: 04-18-2010, 08:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •