# Thread: Sending Arrays into methods then dividing for an average.

1. Member
Join Date
Feb 2011
Posts
5
Rep Power
0

## Sending Arrays into methods then dividing for an average.

Hello, I am trying to get the average temperature for the week, from figures entered in by the user, by passing the array into a method then dividing by seves. I am having difficulty getting the syntax down so any help will be greatly appretiated.

import javax.swing.JOptionPane;

public class Assign1
{
public static void main(String[] args)
{
// Declare variables and Array List
final int tdays = 7;
String[] days = {"Monday", "Tuesday", "Wenesday", "Thursday", "Friday", "Saturday", "Sunday"};
String input = null;
double[] message, message2;
double message1;
double[] temp = new double[tdays];

for (int i = 0; i <days.length ; i++)
{
JOptionPane.showInputDialog("Please enter" + days[i] + "temperature in Fahrenheit.");
temp[i] = Double.parseDouble(input);
}
// Calculate Average
message = calcAverage(temp);

// Convert Temperature to celcius
message1 = convertTemp(temp);

// Display Results
displayResults(message, message1);
}

public static double[] calcAverage(double[] array)
{
double[] temp;
double average;
average = temp / 7;
return average;
}
public static double convertTemp(double[] array)
{
float celcius =((temp-32)*5)/9;
return celcius;
}

2. Member
Join Date
Jan 2011
Location
Gainesville, FL
Posts
45
Rep Power
0
Java Code:
```for (int i = 0; i <days.length ; i++)
{
JOptionPane.showInputDialog("Please enter" + days[i] + "temperature in
Fahrenheit.");
temp[i] = Double.parseDouble(input);
}```

JOptionPane returns an input, but you don't store that input into any variable. You need to have your input variable equal this JOptionPane.

calcAverage is a mess of syntax errors.
Java Code:
```public static double[] calcAverage(double[] array)
{
double[] temp;
double average;
average = temp / 7;
return average;
}```
Your formal parameter is 'double[] array' and your actual parameter is 'double[] temp'. This means that when you call calcAverage(temp), 'array' now references the 'temp' array for your use in the calcAverage method. You cannot re-declare temp in the calcAverage method and expect the values to carry over. To access the 'temp' array that you want, you simply use the 'array' array.

Furthermore, "average = temp / 7;" makes no sense, logically. temp is an array of doubles, not a double itself; it's like saying "yellow / 7". You must loop through each element in the array and sum them up, then divide by 7 outside the loop, and return that.

Note that the data-type before the method name (for calcAverage, you have double[]) is the return type. You are only returning a double, so the return type should be double, not double[].

3. Member
Join Date
Feb 2011
Posts
5
Rep Power
0
Oh I see yes temp / 7 does not make a whole lot of sense. I think I have looped the array properly but it is saying my return type is not initialized.

public static double calcAverage(double[] array)
{
double total = 0; // Initialize the accumalator
double average; // will hold the average
for (int index = 0; index < array.length; index++)
{
total += array[index];
average = total / array.length;
}
return average;
}

4. Member
Join Date
Jan 2011
Location
Gainesville, FL
Posts
45
Rep Power
0
Now the return type is correct but you're storing the returned value into the wrong type.

Java Code:
`message = calcAverage(temp);`

calcAverage returns a double. What datatype is message?

Java Code:
`double[] message, message2;`

Edit: Junky also brought up a couple other points that I missed.
Last edited by Vase; 02-03-2011 at 10:10 PM.

5. Originally Posted by freebirdcal
but it is saying my return type is not initialized.
Local variables do not have a default value. You must give it an intial value just as the error message says.

Also, why do you calculate average everytime around the loop. If I gave you five numbers how many times would you calculate the average?

6. Member
Join Date
Feb 2011
Posts
5
Rep Power
0
It is supposed to a double so I can have a place to store the return type that I will eventually have to pass on to a displayResults method.

import javax.swing.JOptionPane;

public class Assign1
{
public static void main(String[] args)
{
// Declare variables and Array List
final int tdays = 7;
String[] days = {"Monday", "Tuesday", "Wenesday", "Thursday", "Friday", "Saturday", "Sunday"};
String input = null;
double message;
float message1;
double[] temp = new double[tdays];

for (int i = 0; i <days.length ; i++)
{
input = JOptionPane.showInputDialog("Please enter" + days[i] + "temperature in Fahrenheit.");
temp[i] = Double.parseDouble(input);
}
// Calculate Average
meesage = calcAverage(temp);

// Convert Temperature to celcius
message1 = convertTemp(temp);

// Display Results
displayResults(message, message1);
}

public static double calcAverage(double[] array)
{
double total = 0; // Initialize the accumalator
double average; // will hold the average
for (int index = 0; index < array.length; index++)
{
total += array[index];
average = total / array.length;
}
return average;
}
public static float convertTemp(double[] array)
{
double total = 0; // For accumalator
for (int index = 0; index < array.length; index++)
{

}

}
public static void displayResults(double[] memssage,float message1)
{

7. That solved one problem. What about the other two problems that I mentioned?

8. Member
Join Date
Feb 2011
Posts
5
Rep Power
0
I would only want to average it one time so I should do the averaging after the loop. wow thanks!

9. Member
Join Date
Feb 2011
Posts
5
Rep Power
0
Its still a work in progress but I thank you for your support

import javax.swing.JOptionPane;

public class Assign1
{
public static void main(String[] args)
{
// Declare variables and Array List
final int tdays = 7;
String[] days = {"Monday", "Tuesday", "Wenesday", "Thursday", "Friday", "Saturday", "Sunday"};
String input = null;
double message;
float message1;
double[] temp = new double[tdays];

for (int i = 0; i <days.length ; i++)
{
input = JOptionPane.showInputDialog("Please enter" + days[i] + "temperature in Fahrenheit.");
temp[i] = Double.parseDouble(input);
}
// Calculate Average
message = calcAverage(temp);

// Convert Temperature to celcius
message1 = convertTemp(message);

// Display Results
displayResults(message, message1);
}

public static double calcAverage(double[] array)
{
double total = 0; // Initialize the accumalator
double average; // will hold the average
for (int index = 0; index < array.length; index++)
{
total += array[index];
}
average = total / array.length;
return average;
}
public static float convertTemp(double fahrenheit)
{
float celcius = ((fahrenheit - 32)*(5))/(9);
}