14
Jan
Jan
Let's say I have an array that contains 5 numbers (repeats allowed). How do I print out the largest number?
Answer:
You can have a variable that stores the current max value (initialized with the first element). Then iterate through the array and compare it with each element, if the current element is greater - update the max value:
int maxValue = arr[0];
for (int i = 0; i < 5; i ) {
if (arr[i] > maxValue) maxValue = arr[i];
}
System.out.println("the max number is: " maxValue);
Answer:
you can try creating another dummy variable that is initialized to 0 then use conditional statements to compare the contents of the indexes and use looping statements to compare the contents of the indexes until the amount of the contents of the index is reached . ex. if index 0 is greater than the dummy that is initialized to 0 then the larger number will be the content of the dummy variable, it will again loop comparing the content of index1, so if the content of index 1 is larger than that of the dummy it will again be assigned to dummy. continue until the loop ends Book Mark it-> del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Netscape | Yahoo | BlinkList
This entry was posted
on Monday, January 14th, 2008 at 10:12 pm and is filed under Programming.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or TrackBack URI from your own site.