Reputation: 147
I am having problems getting my class to compile. It is giving me errors such as "double cannot be dereferenced" and "cannot find symbol".
The diver is:
public class MinilabArraysDriver{
public static void main(String[ ] args){
//create a 1d array of ints
int[ ] array1d = { 8, 8, 7, 5, 3 };
//call mean1d Method to find the mean (note the static call...)
double theMean = MinilabArrays.mean1d(array1d);
//print the original array and the mean
System.out.print("The mean of: { ");
for (int i=0; i<array1d.length; i++){
if (i != 0)
System.out.print(" ");
//print first for separation (except before first element)
System.out.print(array1d[i]);
}
System.out.print(" } is: " + theMean);
//------------------------------------------------------------
System.out.println("\n\n");
//create a 2d array of doubles
double[ ][ ] array2d = {{ 3.4, 5.1, 8.0},
{ 5.23, 8.2 },
{ 10.7 },
{ 2.9 }
};
//call sum2d to get the sum
double total = MinilabArrays.sum2d(array2d);
//print the 2D array
for (int row=0; row<array2d.length; row++){
System.out.println();
for (int col=0; col<array2d[row].length; col++)
System.out.print(array2d[row][col] + "\t");
}
//print the result
System.out.println("\n\nTotal of 2d array is: " + total);
System.out.println("\n\n");
}
}
And here is the class that I am trying to write:
public class MinilabArrays{
public static double mean1d(int[ ] theMean){
double total = 0;
for (int i = 0 ;i < theMean.length ; i++){
total = total + theMean [ i ];
total = total / theMean.length;
return total;
}
}
public static double sum2d(double [ ] theSum){
double total2, total3, total4 = 0;
for(int row=0 ; row < theSum.length ; row++){
for (int col=0 ; col<theSum[row].length ; col++)
total2 = total2 + theSum[col];
}
total3 = total3 + theSum[row];
total4 = total3 + total2;
return total4;
}
}
For "mean1d" I am trying to find the mean or average of the given number, and for "sum2d" I am trying to find the total of the given number.
Sorry if my code is stupid or dumb. I cannot check it if it won't let me compile.
Thank you for helping me!
Upvotes: 0
Views: 105
Reputation: 490
The first error comes from this line for (int col=0 ; col<theSum[row].length ; col++)
. You are using a two-dimensional array theSum[][]
but declared a one-dimensional array as method parameter.
The other error comes from the use of row
outside of its scope. You declared it inside this for-loop for(int row=0 ; row < theSum.length ; row++)
but used it outside of the for-loop.
Upvotes: 1
Reputation: 3881
You are calling sum2d method by passing 2D array in parameter and declared this method as a 1D array as parameter.
MinilabArrays.java
public static double sum2d(double[][] theSum) {
double total2 = 0;
for (int row = 0; row < theSum.length; row++) {
for (int col = 0; col < theSum[row].length; col++){
total2 = total2 + theSum[row][col];
}
}
return total2;
}
Upvotes: 1
Reputation: 2598
MinilabArrays.sum2d(array2d) requires a double[] not the double[][] here. And also at this point total3 = total3 + theSum[row]; cannot access the variable row outside of the for loop.
Upvotes: 1