Results 1 to 3 of 3
Thread: 2D Array Help Please
- 12-07-2011, 06:01 PM #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 double totalGrade = 0.0; //Accumulator 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) { double totalGrade = 0.0; //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++) { totalGrade += testData[row][col]; } } return totalGrade; } //getAverage Method public static double getAverage(double[][] testData) { double totalGrade = 0.0; double averageGrade = 0.0; 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++) { totalGrade += testData[row][col]; totalLength = rows * cols; averageGrade = totalGrade/totalLength; } } return averageGrade; } //getRowTotal Method public static double getRowTotal(double[][] testData, int cc) { double totalRow = 0.0; int cols = 0; for (int row = 0; row < testData[0].length; row++) { totalRow += testData[cc][row]; } return totalRow; } }
- 12-07-2011, 06:20 PM #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.
- 12-07-2011, 06:34 PM #3
Similar Threads
-
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
create a 2d char array from a 1D string array
By jschmall12 in forum New To JavaReplies: 1Last Post: 04-27-2010, 09:01 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks