• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrays?

 
Lydia Su
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the largest number you can do this
int intLargestNum = array[array.length-1]
if you sort your number ascending order.
Hope this help!
Mindy
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Lydia Su
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need the largest number in the array...must the array be sorted to get the largest number?
 
sharad goswami
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i don't get it why don't u use noraml sorting matters?
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
deep venu
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic