The Artima Developer Community
Sponsored Link

Java Answers Forum
Need min w/o it including the 0 I use to quit

4 replies on 1 page. Most recent reply: Apr 5, 2002 6:03 AM by Patti

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Need min w/o it including the 0 I use to quit Posted: Apr 4, 2002 3:15 PM
Reply to this message Reply
Advertisement
I can't get the min without it including the O to quit. It just gives me the min as 0. I've tried everything.

import java.io.*;
 
class Histogram {
	
	//----------------------------------------------------------
	// Program to display histogram of range of entered numbers.
	//----------------------------------------------------------
	
	
	private static BufferedReader stdin;
	private static int number, max, min, average;
	private static int sum = 0;
    private static int add = 0;		
	
  public static void main (String[] args) throws IOException{
 
		int[] count = new int [10];
	
	   
	   stdin = new BufferedReader (new InputStreamReader(System.in));
	   
       System.out.println( "Enter numbers between 1 and 100, or 0 to quit");
          number = Integer.parseInt (stdin.readLine());        
          count[(number-1)/10]++; // to utilize first entered number
          add++;
          sum+= number;
          
      while (number != 0) { //sentinal to stop entering numbers                
          number = Integer.parseInt (stdin.readLine());
               	  
            if (number != 0) // to load array             
            count[(number-1)/10]++;
            
            
            if (number != 0) // to figure average
            add++;
            sum+= number;
            average = sum/add; 
            
       
            if(number > max) // to figure max
             max = number;
              if (number < min)
               min = number;          
                                                             
          }
                   
     
         // Loop to generate labels of rows
        for (int row = 0; row < count.length; row++){
          System.out.print ((row * 10 + 1) + "-" + ((row + 1) * 10) + " | ");          
           // Loop to print asterisks
           for(int i = 0; i < count[row]; i++)
          {
           System.out.print('*');
          }
           System.out.println();
          }
          
           System.out.println();
           System.out.println ("The minimum number is: " + min);
           System.out.println ("The maximum number is: " + max);
           System.out.println ("The average number is: " + average);
           
   }//main method
}//class Histogram
 


Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Need min w/o it including the 0 I use to quit Posted: Apr 4, 2002 3:20 PM
Reply to this message Reply
For some reason it uploaded part of my program wrong-
that if (number &gt; max) is not what I wrote-it's
if (number > max). I'm having a very bad day here.

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Need min w/o it including the 0 I use to quit Posted: Apr 4, 2002 3:21 PM
Reply to this message Reply
Jeez! It did it again. That reply is not what I wrote. I give up.

steve strongheart

Posts: 12
Nickname: stronghear
Registered: Apr, 2002

Re: Need min w/o it including the 0 I use to quit Posted: Apr 4, 2002 9:07 PM
Reply to this message Reply
min is decalred as a global class variable and class variables are always implicitly initialized when not explicitly initialize - null, 0, '', and false.
(local variables however are uninitialized)

min therefore starts out as 0,which is lower than any valid input.

try initializing min and max like such...

min=Integer.MAX_VALUE;
max=Integer.MIN_VALUE;

or better, read the first number outside of the loop, set min and max to the first number, (the only time you will need to compare both min and max to number. ) this may tak an extra line of code ( great for Bradbury style coding "why say it eloquently in a single line when two would do? - he got paid by the word. " ), but if you type really fast, (or use a faster input stream ) you could save a gonculation per loop, and keep your cpu nice and cool.

number = Integer.parseInt (stdin.readLine());
min=number;  max=number;
 
while(number!=0)
{
    number = Integer.parseInt (stdin.readLine());
    if(number=0) break; 
 
    if(min > number)min=number;
    else if(max < number)max=number;
/* 
 adding else should cut the if(max<number) checks by  an average of 50%

Now, kick back for a few nanoseconds. 
*/
 
//  etc
}


Now min will be sure to change at the first checkpoint, min will never be zero.

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Thank you Posted: Apr 5, 2002 6:03 AM
Reply to this message Reply
Thank you so much !!!

Flat View: This topic has 4 replies on 1 page
Topic: question on jtextarea Previous Topic   Next Topic Topic: Hashtable

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use