I cannot get the correct Average using Random Generator method
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: I cannot get the correct Average using Random Generator method

1. Junior Member
Join Date
Jun 2014
Posts
3

## I cannot get the correct Average using Random Generator method

Create an array that will store 7 temperatures.
Populate the array with 7 random temperatures from 1 to 100 degrees. (hint use a for loop and a Random number Generator)
After the temperatures are in the array, calculate the average of the temperatures in the array.
Print out the average.
Print out each temperature in a statement comparing it to the average such as:
The average temperature is 48.94

Temperature 1 is 5.0 and is below average.

Temperature 2 is 67.8 and is above average.

Code:
```import java.util.Random;

public class ArrayOfTemperatures
{
public static void main(String[] args)
{
// Declare an array
int[] randomtemps = new int[7];
temps[0] = 45;
temps[1] = 32;
temps[2] = 55;
temps[3] = 75;
temps[4] = 89;
temps[5] = 42;
temps[6] = 25;

// record temperatures and find average
int counter = 0;
for (int i = 0; i < temperature; i++)
{
temps[i] = random.nextInt();
counter++;
}
double average = (double) counter / temperature;

// count days
int above = 0;
for (int i = 0; i < temps.length; i++)
{
if (temps[i] > average)
{
above++;
}
}

// report results
System.out.println();
System.out.println("Average = " +  average);
System.out.println(above + " days average");
}
}```

2. Elite Member
Join Date
Jun 1999
Location
SW Missouri
Posts
3,463

## Re: I cannot get the correct Average using Random Generator method

Does that code compile? I don't see where the variable: temperature is defined.

#### Posting Permissions

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