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

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

1. ## [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;
}

{
for(int row = 0; row < 10; row++)
{
for(int column = 0; column < 5; column++)
{
{
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;

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);

//buttons
startOverButton = new JButton("New Array");

maxButton = new JButton("Maximum");

minButton = new JButton("Minimum");

sumButton = new JButton("Sum");

avgButton = new JButton("Average");

aButton = new JButton("Number of A's");

luckyButton = new JButton("Lucky 7");

rangeButton = new JButton("Range Test");

specificColButton = new JButton("2nd column");

specificRowButton = new JButton("3rd row");

/* invokes method to fill array with random numbers between 1 and 100 */
}

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)
{
g.drawString("New Values" , 50, 20);
}
if (source == rangeButton)
{
g.drawString("Number of Values in Range 25 - 50: " + grades.countValues(25,50), 50, 20);
}
}

}

2. ## 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

3. ## 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. ## 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.