Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Random Generator method is not giving me the correct average, Help Needed

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Random Generator method is not giving me the correct average, Help Needed

    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.




    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. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Random Generator method is not giving me the correct average, Help Needed

    Surely you know how to calculate an average!

    (n1 + n2 + n3...) / n

    Your code isn't close to doing this. I just realised, how does this even compile? Where have you declared temperature? How about sorting out your array. randomtemps or temps???
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Yeah there's no average calculation there in that code, counter / temperature is not logically correctly.

    You don't need a counter as you say there's 7 temperatures so you should just divide it by 7.

    There's a lot more to do I think.

Similar Threads

  1. This code is not giving correct values
    By Metamorphoseon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 24th, 2014, 09:30 PM
  2. Random Number Generator
    By Rugby_Thompson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2013, 12:58 AM
  3. For loop not giving the correct output
    By Kattracks32 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 28th, 2013, 04:41 AM
  4. Random Number Generator Always gives 0
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 03:09 PM
  5. HELP random generator with roll() and print() method
    By disc_dido in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 18th, 2011, 01:50 PM