Sorting values?


I have a program to enter a number of values and their respective probabilities as vectors/arrays. I am trying to figure out how to write a loop saying(for every value entered):

if value[] is the min, prob =
if value[] is greater than the min and less than the max, prob = xxx
else prob =


im not sure if i can do a sort type of function and reassign the values in order, but keep their probabilities with the value?

If I have entered values 10, 1, 2, for example, with prob 0.2,0.3, 0.5 respectively.
1. I want to find the lowest value, and enter an equation based on only that
prob.
2. I want to find the second lowest value and enter an eqn based on (1- the
prob of the LOWEST)
3.I want to find the third lowest value(assuming possibility of more than 3
values could be entered) and enter an eqn based on
(1 - prob of lowest value - prob of 2nd lowest value)


Appreciate any help/advice/ideas of how to write this.
Thanks!

Use the standard algorithm sort() or stable_sort() (whichever you need) and either overload the < operator or write your own comparison function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <algorithm>
#include <deque>
using namespace std;

struct value_t
  {
  int    value;
  double probability;
  };

inline bool operator < ( const value_t& a, const value_t& b )
  {
  return a.value < b.value;
  }

int main()
  {
  deque <value_t> values = my_generate_values_func();

  stable_sort( values.begin(), values.end() );

  cout << "The smallest value "    << values[ 0 ].value
       << " has a probability of " << values[ 0 ].probability << endl;

  cout << "The largest value "     << values[ values.size() -1 ].value
       << " has a probability of " << values[ values.size() -1 ].probability << endl;

  return 0;
  }

Hope this helps. :-)

[edit] BTW, this isn't really a 'nix-specific question... ;-)
Last edited on
Topic archived. No new replies allowed.