Largest integer?

Page 1 of 1

6 Replies - 25778 Views - Last Post: 13 July 2008 - 03:13 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=57392&amp;s=b4c99128b82d070da4696a732a71b3d3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 rnmariae

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 12-July 08

Largest integer?

Posted 12 July 2008 - 09:32 PM

Write a program to read in a list of nonnegative integers and to ouput the largest integer, smallest interger, an the average of the integers. The end of the input is indicated by the user entering a negative sentinel value.

```public class UwagzKa {
public static void main (String[] args){

int a = 0;
int min = 0;
int max = 0;
int num = 0;

do
{
System.out.print("Enter numbers-->");
if(num==0)
System.out.println("Numbers must be positive");
}
while(num>0);

while(num>0)

if (num > 0){
min = num;
max = num;
} else if (num < min){
min = num;
} else if (num > max){
max = num;
}

System.out.println("Max " +max);
System.out.println("Min " +min);

}
}
```

This is what I have done so far, but it doesn't work. My minimum and maximum doesn't work. I'm still fourteen so I can't still understand some things. Thanks so much everyone. :]

Is This A Good Question/Topic? 0

Replies To: Largest integer?

#2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Largest integer?

Posted 12 July 2008 - 10:09 PM

```public class UwagzKa {
public static void main (String[] args){

int a = 0;				// I dont know what you have that variable "a" for
int max = 0;		   // good if number > max you can update your max
int min = 9999999; // to find the smallest one you have to init min to a big value
int num;
int total = 0;		   // to compute the average you need the total
int nb = 0;			  // to compute the average you need the total numbers entered

for(;;)					// infinite loop
{
System.out.print("Enter numbers-->");
//	   if(num==0)			 I think that 0 is a valid value (you can compute the average for the score in a class)
//	System.out.println("Numbers must be positive");
if(num < 0)		   // negative number
break;			   // exit the infine loop

if(num > max)	   // find bigger one
max = num;
if(num < min)		// find smallest one
min = num;
total = total + num;	// the total for average
nb++;					   // and the number entered

}  // end of infinite loop

System.out.println("Max " +max);
System.out.println("Min " +min);
System.out.println("Total: " + total + " Nb entered: " + nb);
System.out.println("Average: " + (total / nb));
}
}

```

Happy coding

#3 rnmariae

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 12-July 08

Re: Largest integer?

Posted 12 July 2008 - 10:31 PM

Thanks so much sir pbl. :]

#4 JeroenFM

Reputation: 18
• Posts: 195
• Joined: 30-June 08

Re: Largest integer?

Posted 13 July 2008 - 05:39 AM

rnmariae, on 12 Jul, 2008 - 10:31 PM, said:

Thanks so much sir pbl. :]

May I suggest using Integer.MAX_VALUE as the initial of min. That way the code will still work if the input values are 2 and 3 billion.

#5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Largest integer?

Posted 13 July 2008 - 10:41 AM

JeroenFM, on 13 Jul, 2008 - 05:39 AM, said:

May I suggest using Integer.MAX_VALUE as the initial of min. That way the code will still work if the input values are 2 and 3 billion.

JeroenFM,

that is what I would have done in my own code. But I guess that this 14 years old has already enought to deal with that it was not neccessary to add explanations on what that static value in class Integer was. But feel free to elaborate on the topic.

#6 baavgai

• Dreaming Coder

Reputation: 7181
• Posts: 14,969
• Joined: 16-October 07

Re: Largest integer?

Posted 13 July 2008 - 02:34 PM

pbl, on 13 Jul, 2008 - 01:41 PM, said:

JeroenFM, on 13 Jul, 2008 - 05:39 AM, said:

May I suggest using Integer.MAX_VALUE as the initial of min. That way the code will still work if the input values are 2 and 3 billion.

JeroenFM,

that is what I would have done in my own code. But I guess that this 14 years old has already enought to deal with that it was not neccessary to add explanations on what that static value in class Integer was. But feel free to elaborate on the topic.

I don't like having a preset min and max for this sort of thing. If the list is length zero, give them an exception. Otherwise, it's usually better to just key off the data. e.g.

```if (nb==0) {
min = max = num;
} else {
if (num > max) { max = num; }
if (num < mmin) { min = num; }
}

```

#7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Largest integer?

Posted 13 July 2008 - 03:13 PM

baavgai, on 13 Jul, 2008 - 02:34 PM, said:

I don't like having a preset min and max for this sort of thing. If the list is length zero, give them an exception. Otherwise, it's usually better to just key off the data. e.g.

```if (nb==0) {
min = max = num;
} else {
if (num > max) { max = num; }
if (num < mmin) { min = num; }
}

```

Very good idea. indeed... no no need to know about Integer.MAX_VALUE and all that stuff
Thanks baavgai