This week's giveaway is in the Android forum. We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line! See this thread for details.

i've a question involving reading a user inputted value to set the length of an array then showing the largest number from the array. how do i make the program show the largest number after sorting? i keep getting errors on this...

Are you asking how to find the largest number in the array or how to find the largest array index (aka the size of the array)? Mindy's post answered the size of array question. If you want to find the largest value in the array try something like this: int largest = 0 ; for( int i=0; i < ArrayVar.largest; i++) {<br /> if( ArrayVar[ i ] > largest) { largest = ArrayVar[ i ] ; } } (I didn't compile this so it may need to be fixed a little.)

Please ignore post, I have no idea what I am talking about.

Lydia Su
Greenhorn

Joined: Apr 27, 2001
Posts: 13

posted

0

i need the largest number in the array...must the array be sorted to get the largest number?

No, if you just want the largest number you don't need to sort the array. To get the largest number use something like the code in my previous post. If you want the largest number to be at the first, or last, element in the array than you need to sort.

for(i = 0, i < arr.length - 1; i++)<br /> {<br /> for(j=i+1; j = arr.length; j++)<br /> {<br /> if(arr[i] > arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } This should sort ur array ..then take the last element of sorted array