• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

arrays?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need the largest number in the array...must the array be sorted to get the largest number?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i don't get it why don't u use noraml sorting matters?
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You can't expect to wield supreme executive power just because
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic