Wow, I got it. So the trick is to ASSUME: counter 0 = max, THEN compare counter 0 with counter 1 if counter 1 >counter 0 => compare counter 1 with counter 2 and so on
Thanks for the fast response both of you! On Aug 3, 1:23 am, Eyas Kopty <[email protected]> wrote: > Hi there... > the following lines > if((counter == 0) || (num[counter] > max)) > max = num[counter]; > is a condition that check if you are in the first element of the array then > and for that the we assuem that this first elemen is the max number we're > searching for. > "||" which mean "OR" if the current number in the num array num[counter] is > *larger* (num[counter] > max) that our previously founded max number then > its(num[counter]) becoms our newmax number. > > Instead of max define min variable with value 0 , and the condition shoudl > be changed accordingly - > if((counter == 0)||(num[counter] < min)) > min = num[counter]; > if you are in the first element of the array then and for that the we > assuem that this first elemen is the min number we're searching for. "||" > which mean "OR" if the current item(number) in the num array is > *smaller*(num[counter] < > min) than the our previously founded min number then its(num[counter]) > becoms our new min number. > > also you need to change the informative messages and string you type - in > order to show the minimum number. > > Cheers > Eyas > > > > On Mon, Aug 3, 2009 at 11:05 AM, Howie <[email protected]> wrote: > > > Hi all, > > I'm a bit behind on my homework here. For lab 1036, I have a few > > questions: > > > 1. First, for this piece of code below to find the max number, can > > somebody explain to me what these two lines mean ( if((counter == 0)|| > > (num[counter] > max)) > > max = num[counter]; ) > > and how do we modify it to get the minimum instead of the max ? > > > import javax.swing.JOptionPane; > > > public class GreatestNumber { > > > public static void main(String[] args) { > > int[] num = new int[10]; > > int counter; > > int max = 0; > > int totalnumber = 3; > > > // Prompt a user to enter numbers > > for(counter = 0; counter < totalnumber; counter++){ > > > num[counter] = Integer.parseInt > > (JOptionPane.showInputDialog("Enter numbers until > > " + totalnumber + " numbers are entered")); > > > // Compute the greatest number up to this point > > if((counter == 0)||(num[counter] > max)) > > max = num[counter]; > > } > > > // Display the greatest number. > > JOptionPane.showMessageDialog(null,"The number with the > > greatest value is " + max); > > } > > > }- Hide quoted text - > > - Show quoted text - --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---
