Results 1 to 3 of 3
  1. #1
    prognewb is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default 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;
    	}
    }

  2. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: 2D Array Help Please

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

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default 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.

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 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
  •