using for loop to compute max/min and average problem?

having trouble adding first integer entered

Page 1 of 1

3 Replies - 13434 Views - Last Post: 16 February 2010 - 06:46 AM Rate Topic: -----

#1 Guest_teenagedirtbag*


Reputation:

using for loop to compute max/min and average problem?

Posted 16 February 2010 - 05:33 AM

Hello!

I'm having some trouble with a program I've tried to make, and was wondering if I could get some help!
So my program is supposed to ask the user how many integers they're entering, allow them to enter the integers, then display the average, max and min values.

Trouble is, the way my code is now, for the average computation, the program is adding up all the entries but the first one. I've tried tweaking in every way I know how, but nothing's working for me.

Here's my code:
import java.util.Scanner;
 public class integerEntry
 	{ 
 	
 	      public static void main (String [] args)
 	      {
 	      Scanner keyboard = new Scanner (System.in);
 		     	      
 	{	System.out.println ("Please input the number of integers being entered: ");
 		int numberOfEntries = keyboard.nextInt();
 		System.out.println ("Enter your integers one at at time, followed by the enter key.");		
 	    double next= keyboard.nextDouble();	  	    
 	    double max = 0.0;
 	    double min = next;
 	    double average = 0.0;
   	    double sum = 0.0;	
   	      	    
     	for (int Count = 2; Count <= numberOfEntries; Count++)
 		{
 		    next= keyboard.nextDouble();
 		         {
 			         if (next > max)
 	  		         max = next;
 	  	             else if (next < min)
 	  		         min = next;
 	  	         }
 	  	    sum = sum + next;
 	        average = sum / Count;     
 	        
 		} 	  
 	
 	System.out.println ("The average score is "+average);
 	System.out.println ("The highest score is " +max);
 	System.out.println ("The lowest score is " +min);
    }}}


Is This A Good Question/Topic? 0

Replies To: using for loop to compute max/min and average problem?

#2 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: using for loop to compute max/min and average problem?

Posted 16 February 2010 - 05:52 AM

The first number entered is being assigned to next before the for loop and is not being included in ANY of the other calculations.
      
double next= keyboard.nextDouble();  //The first number entered is being swallowed here.
double max = 0.0;
double min = next;  
double average = 0.0;
double sum = 0.0;   



To include it in the averaging calculations, try initializing average like so:
double average = next;



Side note: You have some extra braces floating around in your for loop. What are those there for? Also, please work on your indentation.

Edit: chirag is correct. I was wrong about the variable that should be changed. It should be sum, not average.

This post has been edited by xor-logic: 16 February 2010 - 06:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 chirag.jain18  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 68
  • Joined: 14-December 09

Re: using for loop to compute max/min and average problem?

Posted 16 February 2010 - 06:31 AM

replace double sum = 0.0;

by
double sum = next;

Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: using for loop to compute max/min and average problem?

Posted 16 February 2010 - 06:46 AM

Initiating "max" to 0 is not good.
what if all inputted numbers are negative? suppose you get: -1, -2, -3
the max number would be 0, but there was never such number in the input.

I would recommend on initiating both max and min to "next".

a logic mistake you made, is that you calculate the average for each iteration of the loop.
the average calculation should be made outside the for loop, and use numberOfEntries instead of count.

Here are the changes, with my comments:
import java.util.Scanner; 
 public class integerEntry 
{
   public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in); 
              //you had braces here, remove them
              //{                 
               System.out.println ("Please input the number of integers being entered: "); 
                int numberOfEntries = keyboard.nextInt(); 
                System.out.println ("Enter your integers one at at time, followed by the enter key.");           
            double next= keyboard.nextDouble();              
            double max; 
            double min;
            double sum = 0.0;
            double average =.0;
            max = min = next; 
            sum+=next;   
                     
        for (int Count = 2; Count <= numberOfEntries; Count++) 
                { 
                    next= keyboard.nextDouble(); 
                         //{ <<unneceaary braces 
                                 if (next > max) 
                                 max = next; 
                             else if (next < min) 
                                 min = next; 
                        // } <<unnecessary braces 
                    sum = sum + next; 
                      
                 
                }
          //calculating average          
         average = sum / numberOfEntries;
          System.out.println ("The average score is "+average); 
        System.out.println ("The highest score is " +max); 
        System.out.println ("The lowest score is " +min); 
    }
}


This post has been edited by japanir: 16 February 2010 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1