Results 1 to 8 of 8
  1. #1
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Copying Single Arrays to 2-D Arrays

    How do you copy 2 single arrays to make one Two-Dimensional Array?

    The problem I'm working on has 2 predefined arrays (test1 and test2), which I need to compute the average of to create a new array (which I'm calling average). There are no defined amount of elements in the predefined arrays.

    Thank you in advance for your help!!
    ~Jaime

  2. #2
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Copying Single Arrays to 2-D Arrays

    I figured that part out.

    Now I need to add the first array at each index to the second array of the corresponding index, find the average and put into the new array for printing...

  3. #3
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Copying Single Arrays to 2-D Arrays

    Can someone help me get through the end of this code? I'm only receiving 1 error when I compile....

    Here's the code:
    Java Code:
    public class Grades
    {
       /**
          This method examines the two arrays of test grades (double)
          identified by the two parameters, and creates an array
          containing the average values of these two arrays.
          The test scores for each student (given in the same row in each test
          array) are combined to find the average score for each student. 
          This average is placed in the corresponding row in the new array.
          @param test1, the array of scores for the first test
          @param test2, the array of scores for the second test
          @return, the array containing the average scorres
       */
       public static double[] makeAverage(double[] test1, double[] test2)
       {
    		// your work here
    
          // declare double array
    		double[][] averageArray = new double[test1.length][test2.length];
    		// loop though theArray, computing each student's average
    		double average = 0;
    		for (int j = 0; j < test1.length; j++)
    		{
    			average = (test1[j] + test2[j]) / test1.length;
    		}
    		// your work here
    
    		// return array of averages
    		System.out.println(averageArray[j]);
       }
    }
    Here's my error:

    Grades.java:29: error: cannot find symbol
    System.out.println(averageArray[j]);
    ^
    symbol: variable j
    location: class Grades
    1 error


    Any help would be greatly appreciated!
    ~Jaime

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Copying Single Arrays to 2-D Arrays

    You have defined variable j in the header of your for-loop so it's only visible in that loop but you're trying to use the variable when the loop has finished.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Copying Single Arrays to 2-D Arrays

    That makes sense. Should I move the println up or declare the variable outside of the loop? I always get confused on which is the "right" way.

    I tired moving it up but then got an error that I needed a return statement. I tried averageArray and average but both times got an incompatible type error so I'm thinking I need to declare the int j outside of the loop.....................?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Copying Single Arrays to 2-D Arrays

    Why don't you try and compile your code? It's free ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Copying Single Arrays to 2-D Arrays

    I made the following changes and it compiles without error. However, when I put it into LabRat (for grading), all it returns is 0.0, 0.0, 0.0, 0.0, 0.0.

    Other than the obvious problem (all 0.0???), I also need to make the double[] average initialize with a non-defined number. The last set of test data only gives 2 sets of scores. If it prints out 5 sets of scores in the array it will be marked as incorrect. Not sure how to do that since I thought you always needed to declare an arrays' length.

    Here's what it returns:
    Method Parameters Actual Return Expected Return Outcome

    makeAverage [87, 76, 94, 82, 72],[91, 76, 95, 86, 67] [0.0, 0.0, 0.0, 0.0, 0.0] [89.0, 76.0, 94.5, 84.0, 69.5] fail

    makeAverage [70, 80, 90, 85, 65],[70, 80, 90, 85, 65] [0.0, 0.0, 0.0, 0.0, 0.0] [70.0, 80.0, 90.0, 85.0, 65.0] fail

    makeAverage [70],[0.0] [0.0, 0.0, 0.0, 0.0, 0.0] [35.0] fail


    Here's the updated code:
    Java Code:
    public class Grades
    {
       /**
          This method examines the two arrays of test grades (double)
          identified by the two parameters, and creates an array
          containing the average values of these two arrays.
          The test scores for each student (given in the same row in each test
          array) are combined to find the average score for each student. 
          This average is placed in the corresponding row in the new array.
          @param test1, the array of scores for the first test
          @param test2, the array of scores for the second test
          @return, the array containing the average scorres
       */
       public static double[] makeAverage(double[] test1, double[] test2)
       {
    		// your work here
    
          // declare double array
    		double[][] averageArray = new double[test1.length][test2.length];
    		// loop though theArray, computing each student's average
    		double[] average = new double[5];
    		double averages = 0;
    		for (int j = 0; j < test1.length; j++)
    		{
    			averages = (test1[j] + test2[j]) / test1.length;
    			System.out.println(averageArray[j]);
    		}
    		// your work here
    
    		// return array of averages
    		return average;
       }
    }
    Thank you in advance.
    ~Jaime
    Last edited by jmscarlet9; 04-01-2012 at 09:32 PM.

  8. #8
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Copying Single Arrays to 2-D Arrays

    Hello Java Gods,

    I'm looking for help wrapping up (I hope) a program I'm working on for my intro to programming class. Can anyone take a look and help me figure out why I'm only getting 0's back when the homework tester runs it? I know it has something to do with my denominator, but I don't know what it should change to. Plus, if there are more problems then that, I'm clueless.

    Thank you in advance!

    Here's the code:
    Java Code:
    public class Grades
    {
       /**
          This method examines the two arrays of test grades (double)
          identified by the two parameters, and creates an array
          containing the average values of these two arrays.
          The test scores for each student (given in the same row in each test
          array) are combined to find the average score for each student. 
          This average is placed in the corresponding row in the new array.
          @param test1, the array of scores for the first test
          @param test2, the array of scores for the second test
          @return, the array containing the average scorres
       */
       public static double[] makeAverage(double[] test1, double[] test2)
       {
    		// your work here
    
          // declare double array
    		double[][] averageArray = new double[test1.length][test2.length];
    		// loop though theArray, computing each student's average
    		double[] average = new double[5];
    		for (int j = 0; j < test1.length; j++)
    		{
    			averageArray[j] = (test1[j] + test2[j]) / averageArray.length;
    			System.out.println(average[j]);
    		}
    		// your work here
    
    		// return array of averages
    		return average;
       }
    }
    Here are my errors when compiled:

    Grades.java:24: error: incompatible types
    averageArray[j] = (test1[j] + test2[j]) / averageArray.length;
    ^
    required: double[]
    found: double
    1 error

    Here's what the HW test is using for inputs.
    Copying Single Arrays to 2-D Arrays-return.png

Similar Threads

  1. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  2. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 02:28 PM
  3. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02: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
  •