1. Member Join Date
Dec 2011
Posts
1
Rep Power
0

## 2D Array Help Please

Hey everyone, I'm new to this forum. This is my first semester of programming and I'm working on a project. I was hoping someone could take a look at my code and help me figure out what I'm doing wrong.

Note: The program compiles just fine, however, one of the operations isn't performing correctly on output.

I have a program that is a 2 dimensional array (4 x 3). It holds numbers that the user can enter. I have to create 3 functions. On the first function I have get to pass the array into the method and then get the total of all of the numbers that were put into the array. I did that fine, no problem.

The second method is to pass the array into it and then get the average for all of the numbers. I did that no problem as well.

The third method is where my problem comes. I am supposed to get the total of a row, however, the user gets to choose which row that is. This method has to have two arguments, one for the array, of course, and then one for the users choice of the row. In the main method I collected input to total the row that the user wanted totaled. It doesn't work correctly though. It totals up the first row of numbers every time and displays that. I don't know how to control it I guess you would say.

I would really appreciate it if someone could take a look at this and help me out. Thanks.

CODE:

Java Code:
```import java.util.Scanner;
import java.text.DecimalFormat;

/**
This program will include a two-dimensional array to hold test data and perform
mathematical operations on them.
*/

public class testData
{
public static void main(String[] args)
{
final int ROWS = 4;			//4 rows of test data
final int COLS = 3;			//3 columns of test data
int total = 0, cc;

//Create an array to hold the test data for each
//row.
double[][] testData = new double[ROWS][COLS];

//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

//Display an introduction.
//System.out.println("Introduction");

//Nested loops to fill the array with test data
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
System.out.println("Row " + (row + 1) +
", Column " + (col + 1) +
": ");
testData[row][col] = keyboard.nextDouble();
}

}
System.out.println("What row do you want the total for? (1,2,3 or 4): ");
cc = keyboard.nextInt();
System.out.println();		//Print blank line.
System.out.println("The total grade is: " + getTotal(testData));
System.out.println("The average grade is: " + getAverage(testData));
System.out.println("The total of the rows is: " + getRowTotal(testData, total));
}

//getTotal Method
public static double getTotal(double[][] testData)
{

//Nested loops to add all the elements of the array.
for (int row = 0; row < testData.length; row++)
{
for (int col = 0; col < testData[row].length; col++)
{
}
}
}

//getAverage Method
public static double getAverage(double[][] testData)
{
double totalLength = 0.0;
double rows = 4;
double cols = 3;

//Nested loops to add all the elements of the array.
for (int row = 0; row < testData.length; row++)
{
for (int col = 0; col < testData[row].length; col++)
{
totalLength = rows * cols;
}

}
}

//getRowTotal Method
public static double getRowTotal(double[][] testData, int cc)
{
double totalRow = 0.0;
int cols = 0;

for (int row = 0; row < testData.length; row++)
{
totalRow += testData[cc][row];
}
}
}```  Reply With Quote

2. Member Join Date
Oct 2011
Posts
90
Rep Power
0

## Re: 2D Array Help Please

Add System.out.println(cc+" "+row) to your for loop. Take a look at what you're actually printing.  Reply With Quote

3. ## Re: 2D Array Help Please

Where do you call the method? What are the values of the arguments that you pass to the method?
Print them out so you see what they are.  Reply With Quote

#### Posting Permissions

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