Find highest value in List

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Find highest value in List

 
0
  #1
Oct 1st, 2008
I am using a List where I wonder how it is possible to find the MaxValue searching trough
Value1[0] until Value1[3].
In this case it should return: 12
I have found a ::Max function but this will only consider the 2 values inside the paranthes wich then will return: 100.
For the std:: you can use std::max_element( , ) for this purpose to search through a range of elements.

  1. List<double>^ Value1= gcnew List<double>();
  2. Value1->Add(12);
  3. Value1->Add(2);
  4. Value1->Add(7);
  5. Value1->Add(4);
  6.  
  7. double MaxValue1 = 0;
  8.  
  9. MaxValue1 = System::Math::Max( 0, 100 );
Last edited by Jennifer84; Oct 1st, 2008 at 11:02 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Find highest value in List

 
0
  #2
Oct 1st, 2008
Well You can use the sort function to list and get the max value .

For that you will need to write a bool comp() function for your datatype.

Comparison function that, taking two values of the same type than those contained in the list object, returns true if the first argument goes before the second argument in the specific order (i.e., if the first is less than the second), and false otherwise.

After sorting. The item in your list will be the greatest i guess.
Last edited by Sky Diploma; Oct 1st, 2008 at 11:07 am.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Find highest value in List

 
0
  #3
Oct 1st, 2008
I understand. I have a wondering here. If you would use a for loop to iterate through a series of elements to replace a greater value to find the greatest value.
Will this be as fast as the sortingmethod or perheps this is difficult to say ?
Why I wonder is because I am not 100 % sure how to write that comparison before I begin.
Last edited by Jennifer84; Oct 1st, 2008 at 11:14 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Find highest value in List

 
0
  #4
Oct 1st, 2008
Well I dint see it before. But seems that you have a list of doubles in the list.

So you can just use

  1. Value1.sort();

And then print out the last item in the list.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,858
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Find highest value in List

 
1
  #5
Oct 1st, 2008
Originally Posted by Sky Diploma
Well You can use the sort function to list and get the max value .

For that you will need to write a bool comp() function for your datatype.

Comparison function that, taking two values of the same type than those contained in the list object, returns true if the first argument goes before the second argument in the specific order (i.e., if the first is less than the second), and false otherwise.

After sorting. The item in your list will be the greatest i guess.
Erm, what's wrong with a simple loop?
  1. double max = Value1[0];
  2.  
  3. for each ( double value in Value1 ) {
  4. if ( value > max )
  5. max = value;
  6. }
>Will this be as fast as the sortingmethod or perheps this is difficult to say ?
I'd be surprised if the sorting method was faster.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Find highest value in List

 
0
  #6
Oct 1st, 2008
Originally Posted by Narue View Post
Erm, what's wrong with a simple loop?
  1. double max = Value1[0];
  2.  
  3. for each ( double value in Value1 ) {
  4. if ( value > max )
  5. max = value;
  6. }
>Will this be as fast as the sortingmethod or perheps this is difficult to say ?
I'd be surprised if the sorting method was faster.

Well I dint see that . The List was containing Double's I thought that it is some datatype . I stand corrected though. I guess i already have replied too.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Find highest value in List

 
0
  #7
Oct 1st, 2008
I think I will go with the loop then, I found out it was very fast also.
I did iterate a ListValue 200000000 times and it only took about 1 second.

/j
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1615 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC