Largest integer?

Write a program to read in a list of nonnegative integers and to ouput

Page 1 of 1

6 Replies - 18503 Views - Last Post: 13 July 2008 - 03:13 PM Rate Topic: -----

#1 rnmariae  Icon User is offline

  • New D.I.C Head

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

Largest integer?

Post icon  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-->");
	num = SavitchIn.readLineInt();
	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  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • 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-->");
		num = SavitchIn.readLineInt();
//	   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
Was This Post Helpful? 0
  • +
  • -

#3 rnmariae  Icon User is offline

  • New D.I.C Head

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

Re: Largest integer?

Posted 12 July 2008 - 10:31 PM

Thanks so much sir pbl. :]
Was This Post Helpful? 0
  • +
  • -

#4 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: Largest integer?

Posted 13 July 2008 - 05:39 AM

View Postrnmariae, 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.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Largest integer?

Posted 13 July 2008 - 10:41 AM

View PostJeroenFM, 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.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Largest integer?

Posted 13 July 2008 - 02:34 PM

View Postpbl, on 13 Jul, 2008 - 01:41 PM, said:

View PostJeroenFM, 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; }
}


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Largest integer?

Posted 13 July 2008 - 03:13 PM

View Postbaavgai, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1