# find minimum and maximum

Page 1 of 1

## 5 Replies - 3793 Views - Last Post: 22 October 2009 - 11:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=133832&amp;s=e07dc6e35adc6083f1ad1ac3e4e8fee6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 yingkang

Reputation: 0
• Posts: 51
• Joined: 24-March 09

# find minimum and maximum

Posted 22 October 2009 - 10:06 PM

the program should find the minimum andhe maxmum from the 1000 ramdom numbers. It needs to be done in the method of enter( double dblItem ) , in Statistics class. can anyone help please? thanks
```public class StatisticsTest {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
double dblNum;
Statistics statisticsObj = new Statistics();

for(int i = 0; i < 1000; i++)
{
dblNum = Math.random() * 100;
statisticsObj.enter(dblNum);
}
System.out.println(statisticsObj);

}

}
import java.text.DecimalFormat;
public class Statistics
{
private int	intCount;
private double dblMax;
private double dblMin;
private double dblSum;
private double dblMean;

public Statistics()
{
this.intCount = 0;
this.dblMax = 0;
this.dblMin = 0;
this.dblSum = 0;
}
public int count()
{
return this.intCount;

}
public double min()
{
return this.dblMin;

}
public double max()
{
return this.dblMax;
}

public double sum()
{
return this.dblSum;
}
public double mean()
{

return dblMean;
}

void enter( double dblItem )
{

dblSum += dblItem;
intCount++;
// here I need to find the minimum and maxmum, but i cant figure out how

dblMean = this.dblSum / (double)this.intCount;
}

public String toString()
{
DecimalFormat fmt = new DecimalFormat("0.####");
return "Counter = " + intCount + "\nMinimum = " + fmt.format(dblMin)
+ "\nMaximum = " + fmt.format(dblMax) + "\nSum = " + fmt.format(dblSum) +
"\nAverage = " + fmt.format(dblMean);
}
}

```

Is This A Good Question/Topic? 0

## Replies To: find minimum and maximum

### #2 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: find minimum and maximum

Posted 22 October 2009 - 10:17 PM

What problem(s) are you encountering?

### #3 yingkang

Reputation: 0
• Posts: 51
• Joined: 24-March 09

## Re: find minimum and maximum

Posted 22 October 2009 - 10:21 PM

Locke, on 22 Oct, 2009 - 09:17 PM, said:

What problem(s) are you encountering?

the ramdom generator generate 1000 numbers, and I need to find the minimum and maxmum. how can I do that?. by the way, i havent learnt array yet, so it wont use any array stuff. thank you for help

### #4 virgul

• D.I.C Regular

Reputation: 44
• Posts: 269
• Joined: 18-March 09

## Re: find minimum and maximum

Posted 22 October 2009 - 10:38 PM

no need for an array, all you need is a variable that is a min and max variable, youll probably want to initialize them such that they WILL be edited

```int min = 9001, max = -9001;//unless you got some impossible values this should be just fine
//...code...\\

for(int i = 0; i < 1000; i++)
{
dblNum = Math.random() * 100;
statisticsObj.enter(dblNum);
if(min > dblNum)
min = dblNum;
if(max < dblNum)
max = dblNum;

}

```

### #5 yingkang

Reputation: 0
• Posts: 51
• Joined: 24-March 09

## Re: find minimum and maximum

Posted 22 October 2009 - 10:57 PM

virgul, on 22 Oct, 2009 - 09:38 PM, said:

no need for an array, all you need is a variable that is a min and max variable, youll probably want to initialize them such that they WILL be edited

```int min = 9001, max = -9001;//unless you got some impossible values this should be just fine
//...code...\\

for(int i = 0; i < 1000; i++)
{
dblNum = Math.random() * 100;
statisticsObj.enter(dblNum);
if(min > dblNum)
min = dblNum;
if(max < dblNum)
max = dblNum;

}

```

Thanks Virgul I revised my program according to your suggestion. It works perfectly now. thank you very much

### #6 Munkon

Reputation: 5
• Posts: 61
• Joined: 13-August 09

## Re: find minimum and maximum

Posted 22 October 2009 - 11:46 PM

Hi virgul,

The random method will give you only numbers between 0 and 100 and then you are in trouble..

I'd do something else:

```for(int i = 0; i < 1000; i++)
{
dblNum = Math.random() * 100;
if (i = 0)
{
min = dblNum;
max = dblNum;
}
else
{

statisticsObj.enter(dblNum);
if(min > dblNum)
min = dblNum;
if(max < dblNum)
max = dblNum;
}
}

```

Good luck,

Munkon.