7 Replies - 16905 Views - Last Post: 30 January 2013 - 10:54 PM Rate Topic: -----

#1 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 309
  • Joined: 29-September 12

How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 04:22 PM

I just want to ask what should be the way to do it, not asking for code.
So currently, I declare 2 variables: largest and second largest.
I go thru the array of integer, and state:
if (array[i] > largest){
largest = *array[i];} else if (array[i] > second largest) { second largest = *array[i]}; 


Please correct me
Is This A Good Question/Topic? 0
  • +

Replies To: How to get largest and second largest in an array of integer.

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,577
  • Joined: 12-December 12

Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 05:35 PM

Sounds okay but 'second largest' cannot be two separate words - perhaps it is not in your actual code.

[I'm assuming you are initially setting largest, and secondLargest, to a small value.]

Mmm might have to check(?) - supposing the first element is the 2nd largest? It's late for me, so you'll need to check.

This post has been edited by andrewsw: 30 January 2013 - 05:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,577
  • Joined: 12-December 12

Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 05:54 PM

I think on the very first iteration I would set both the largest and secondLargest to whatever the first value is. Subsequent iterations would use your code.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 06:37 PM

You need a variant of selection sort. Google it, it's a very simple algorithm.
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,398
  • Joined: 19-February 09

Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 07:28 PM

If

largest = 5
second largest = 4

next element = 6


What is the new largest and new second largest?
Was This Post Helpful? 1
  • +
  • -

#6 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 09:41 PM

Sort the int arr[] in descending order. arr[0] and arr[1] will be the two elements you want.
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1047
  • View blog
  • Posts: 4,452
  • Joined: 09-June 09

Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 09:55 PM

Quote

Sort the int arr[] in descending order. arr[0] and arr[1] will be the two elements you want.


Sorting the entire array is overkill. Just modify one of the existing sorting algorithms to get the 2 most greater elements.

This post has been edited by jjl: 30 January 2013 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: How to get largest and second largest in an array of integer.

Posted 30 January 2013 - 10:54 PM

That is why I suggested selection sort.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1