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 4 of 4

Thread: [Method] How to calculate the sum of a Row or Column in a Multi Dimensional Array

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Method] How to calculate the sum of a Row or Column in a Multi Dimensional Array

    Hello, I am working on an assignment for my Java class and I just can't figure out the last two methods. I was hoping someone here can point me in the right direction, as searching for an answer online hasn't been helpful.

    I am supposed to build a class that will fill an array and use a GUI to display different function sin the array, such as returning true if the number 7 is included. The last two methods are to return the sum of a column and a row, which column or row is not specified. I am at a loss on how to do this frankly, so hopefully I can get some help.

    //Program Name: StatsArray.java
    //Author: Joshua Rowley
    //Class: CSC110AA
    //Date: 17Oct11
    //Description: This program returns information based on a multiple dimension array.
    // Needs to be able to return sum, average, minimum, maximum, search for a specific number and generate a new array.

    import java.awt.*;
    import java.util.Random; //for our random number generator

    public class MultiStatsArray
    {
    private int rowSize; // how many rows
    private int columnSize; // how many columns
    private int[][] stats; // an array of integers

    MultiStatsArray()
    {
    rowSize = 10;
    columnSize = 5;
    stats = new int[rowSize][columnSize] ; //instantiate the array called stats
    }

    public void display(Graphics g)
    {
    int x = 50; //coordinates for displaying
    int y = 40; // displaying the array
    for(int row = 0; row < stats.length; row++)
    {
    for (int column = 0; column < stats[row].length; column++)
    {
    g.drawString("" + stats[row][column], x + column * 25, (y + 15 * row));
    }
    }
    }

    public void fillArray()
    {
    Random random = new Random();
    for(int row = 0; row < 10; row++)
    {
    for(int column = 0; column < 5; column++)
    {
    stats[row][column] = random.nextInt(100);
    }
    }
    }

    public int getSum() //add up all the values in the array
    {
    int sum = 0;
    Random random = new Random();
    for(int row = 0; row < 10; row++)
    {
    for(int column = 0; column < 5; column++)
    {
    sum += stats[row][column];
    }
    }
    return sum;
    }


    public int getMax() //return the maximum value in the array
    {
    int maximum = stats[9][4];
    for(int row = 0; row < 10; row++)
    {
    for(int column = 0; column < 5; column++)
    {
    if(maximum < stats[row][column])
    {
    maximum = stats[row][column];
    }
    }
    }
    return maximum;
    }

    public int getMin() //return the minimum value in the array
    {
    int minimum = stats[9][4];
    for(int row = 0; row < 10; row++)
    {
    for(int column = 0; column < 5; column++)
    {
    if(minimum > stats[row][column])
    {
    minimum = stats[row][column];
    }
    }
    }
    return minimum;
    }

    public double getAverage()
    {
    return (double) this.getSum() / (rowSize * columnSize);
    }

    public int countValues(int lowRange, int highRange) //count how many numbers are >= lowRange and <= highRange
    {
    int count = 0;
    for(int row = 0; row < 10; row++)
    {
    for(int column = 0; column < 5; column++)
    {
    if((stats[row][column] >= lowRange) && (stats[row][column] <= highRange))
    count++;
    }
    }
    return count;
    }

    public boolean isValueFound(int someNumber)
    {
    for(int row = 0; row < 10; row++)
    {
    for(int column = 0; column < 5; column++)
    {
    if (stats[row][column] == someNumber)
    {
    return true;
    }
    }
    }
    return false;
    }

    public int getRowSum(int row)
    {
    return 0;
    }

    public int getColumnSum(int column)
    {
    return 0;
    }

    }

    ------------------------------------------------------------------------------------------------------------------------------------------

    /* MultiStatsArrayGUI
    This is the GUI tester/controller for the MultiStatsArray class.
    The MultiStatsArray is used to hold 50 grades.

    Patricia Baker
    */

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class MultiStatsArrayGUI extends JFrame implements ActionListener
    {

    //set up buttons to control the MultiStatsArray
    private JButton maxButton;
    private JButton minButton;
    private JButton sumButton;
    private JButton avgButton;
    private JButton aButton;
    private JButton luckyButton;
    private JButton startOverButton;
    private JButton rangeButton;
    private JButton specificColButton;
    private JButton specificRowButton;

    private JPanel panel;

    /*instantiate your MultiStatsArray object called grades
    creates array of size 10 rows and 5 columns */
    private MultiStatsArray grades = new MultiStatsArray( );

    public static void main(String[] args)
    {
    MultiStatsArrayGUI demo = new MultiStatsArrayGUI();
    demo.setSize(400,400);
    demo.createGUI();
    demo.setVisible(true);
    }

    private void createGUI()
    {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window = getContentPane();
    window.setLayout(new FlowLayout());

    panel = new JPanel();
    panel.setPreferredSize(new Dimension(300, 200));
    panel.setBackground(Color.white);
    window.add(panel);

    //buttons
    startOverButton = new JButton("New Array");
    window.add(startOverButton);
    startOverButton.addActionListener(this);

    maxButton = new JButton("Maximum");
    window.add(maxButton);
    maxButton.addActionListener(this);

    minButton = new JButton("Minimum");
    window.add(minButton);
    minButton.addActionListener(this);

    sumButton = new JButton("Sum");
    window.add(sumButton);
    sumButton.addActionListener(this);

    avgButton = new JButton("Average");
    window.add(avgButton);
    avgButton.addActionListener(this);

    aButton = new JButton("Number of A's");
    window.add(aButton);
    aButton.addActionListener(this);

    luckyButton = new JButton("Lucky 7");
    window.add(luckyButton);
    luckyButton.addActionListener(this);

    rangeButton = new JButton("Range Test");
    window.add(rangeButton);
    rangeButton.addActionListener(this);

    specificColButton = new JButton("2nd column");
    window.add(specificColButton);
    specificColButton.addActionListener(this);

    specificRowButton = new JButton("3rd row");
    window.add(specificRowButton);
    specificRowButton.addActionListener(this);

    /* invokes method to fill array with random numbers between 1 and 100 */
    grades.fillArray();
    }

    public void actionPerformed(ActionEvent event)
    {
    Graphics g = panel.getGraphics();
    Object source = event.getSource();

    /*cover up old display before drawing the new values. */
    g.setColor(Color.white);
    g.fillRect(0, 0, 300, 200);
    g.setColor(Color.black);

    if (source == minButton)
    g.drawString("Minimum is: " + grades.getMin(), 50, 20);
    if (source == maxButton)
    g.drawString("Maximum is: " + grades.getMax(), 50, 20);
    if (source == sumButton)
    g.drawString("Sum is: " + grades.getSum(), 50, 20);
    if (source == avgButton)
    g.drawString("Average is: " + grades.getAverage(), 50, 20);
    if (source == aButton)
    g.drawString("Number of A's = " + grades.countValues(90, 100), 50, 20);
    if (source == luckyButton)
    g.drawString("Lucky 7 Y or N: " + ( ( grades.isValueFound( 7 ) )? 'Y' : 'N' ), 50, 20);
    if (source == specificColButton)
    g.drawString("The total of the 2nd column is: " + grades.getColumnSum(1), 50, 20);
    if (source == specificRowButton)
    g.drawString("The total of the 3rd row is: " + grades.getRowSum(2), 50, 20);
    if (source == startOverButton)
    {
    grades.fillArray();
    g.drawString("New Values" , 50, 20);
    }
    if (source == rangeButton)
    {
    g.drawString("Number of Values in Range 25 - 50: " + grades.countValues(25,50), 50, 20);
    }
    grades.display(g);
    }

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: [Method] How to calculate the sum of a Row or Column in a Multi Dimensional Array

    That is waaay too much code for us to sift through- there are hundreds of posts here, so we don't have a ton of time to wade through hundreds of lines of code. I suggest you create an SSCCE that demonstrates the problem.

    I also recommend you read through this: http://www.javaprogrammingforums.com...e-posting.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: [Method] How to calculate the sum of a Row or Column in a Multi Dimensional Array

    A good way to work on this you should create a class with just a main method and the method you want to test. Also, highlight your code with:
    [highlight=Java] code [/highlight]

  4. #4

    Default Re: [Method] How to calculate the sum of a Row or Column in a Multi Dimensional Array

    The first thing you should realize is the "direction" that the active element takes in a multidimensional array when traversing through it.

    The 2D array:
    int[][] matrix = new int[3][4];
    Assuming all are initialized to zeros, this creates:

    0 0 0 0
    0 0 0 0
    0 0 0 0

    The array can be accessed via row and column indexing.
    The first index is the row, the second is the column.

    So matrix[1][2] would get you here:
    0 0 0 0
    0 0 0 0
    0 0 0 0

    All arrays start from the upper left corner (0,0):
    0 0 0 0
    0 0 0 0
    0 0 0 0

    and end in the lower right (row.length,column.length) ... (3,4)
    0 0 0 0
    0 0 0 0
    0 0 0 0

    Movement can be achieved by manipulating the indices.
    To go right, increase the column by 1
    To go left, decrease the column by 1
    To go up, decrease the row by 1
    To go down, increase the row by 1

    You can move diagonally by combining this logic.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. Using recursion to calculate a path through an array
    By jpetticrew in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 20th, 2011, 05:21 PM
  2. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 12:01 PM
  3. Help on 2D Multi Table Array
    By clevel211 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 1st, 2011, 04:28 AM
  4. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM
  5. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM