943,822 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3009
  • C++ RSS
Oct 1st, 2008
0

Find highest value in List

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Oct 1st, 2008
0

Re: Find highest value in List

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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 1st, 2008
0

Re: Find highest value in List

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.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Oct 1st, 2008
0

Re: Find highest value in List

Well I dint see it before. But seems that you have a list of doubles in the list.

So you can just use

C++ Syntax (Toggle Plain Text)
  1. Value1.sort();

And then print out the last item in the list.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 1st, 2008
1

Re: Find highest value in List

Quote 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?
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 1st, 2008
0

Re: Find highest value in List

Click to Expand / Collapse  Quote originally posted by Narue ...
Erm, what's wrong with a simple loop?
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 1st, 2008
0

Re: Find highest value in List

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
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: .rfind for Managed
Next Thread in C++ Forum Timeline: How to store a genogram in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC