# Thread: Copying Single Arrays to 2-D Arrays

1. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

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

~Jaime

2. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## 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. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## 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)
{

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

// return array of averages
System.out.println(averageArray[j]);
}
}```
Here's my error:

System.out.println(averageArray[j]);
^
symbol: variable j
1 error

Any help would be greatly appreciated!
~Jaime

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

5. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## 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. ## Re: Copying Single Arrays to 2-D Arrays

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

kind regards,

Jos

7. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## 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)
{

// 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]);
}

// return array of averages
return average;
}
}```
~Jaime
Last edited by jmscarlet9; 04-01-2012 at 09:32 PM.

8. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

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

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

// 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]);
}

// return array of averages
return average;
}
}```
Here are my errors when compiled:

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

Here's what the HW test is using for inputs.

#### Posting Permissions

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