Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
Can anyone guide me how to find the difference between the highest value and each member in the set of nb_tuple.

C++
nb_tuple* max = NULL;
	int max_r = 0;
	for (set<int>::iterator it = rs.begin(); it != rs.end(); it++) {
		int r = *it;
		if (r > 0) {
	for (std::vector<nb_tuple*>::iterator it2 = reachability[r].begin();
		it2 != reachability[r].end();it2++) {
                nb_tuple* x = *it2;
		if (max == NULL || x->power() > max->power()) {
						max = x;
						max_r = r;
					}
                                         ...
			}
		}


Many thanks
Pare
Posted
Comments
[no name] 14-Apr-12 23:10pm    
You have not provided enough information. What is an nb_tuple? What condition does one nb_tuple have to satisfy to be greater than another? Otherwise we are just guessing.

I am not sure to understand you correctly but a way to find the max and the difference between max and all numbers would be...

Create a variable "max" and assign zero
do a loop to determine the highest value
evaluate if actual number is bigger than max
if yes: assign the actual value to max
if not: check the next number
Optional: you can save the position of the max as well if you want/need to


Once you have the max value you make another loop
you get the diference of each position just substracting both values
(if you want it without sign, then use the absolute value)
the difference can be saved in another set or array or whatever you need

If this is not what you are looking for... then please use the "improve question" right side below your message and explain it better.

Addition: Doing it on the easy way.

C++
int max = 0;
int i;

for (i = 0; i < original_set_length; i++)
{
   if (original_set[i] > max)
   {
      max = original_set[i];
   }
}

for (i = 0; i < original_set_length; i++)
{
   differences_set[i] = max - original_set[i];
}
 
Share this answer
 
v3
Comments
Aescleal 15-Apr-12 3:11am    
Hi, while I thought your algorithm was perfectly fine, and perhaps the only way to solve the questioner's problem, I didn't think it really suited C++ that well so I chopped a star off the rating.

That may seem a bit mean but see my solution below for what I'd do instead and why I think it's superior. It actually uses the same algorithm yours does but doesn't require you to write any explicit loops.

Cheers,

Ash
Aescleal 16-Apr-12 5:56am    
Bumped you to a 5 for not screaming and running when you saw the code that was presented above...
Nelek 16-Apr-12 18:04pm    
XDDD, thank you. You made my night. :thumbsup:
What I wonder is... why did he accepted his own failing answer?
If Nelek's assumption about what the question means is true then there's a lazy coder way of performing his algorithm:-

- use std::max_element to work out what the maximum value in the set is
- use std::transform to work out what the max_value - each element is and copy them into a new collection or to a stream somewhere

About the only code you have to write is the functor for doing the subtraction.

There's a moral here - every time you start writing a specific loop have a quick scan over the standard library algorithms and see if there's any that fit what you're trying to do.

Cheers,

Ash
 
Share this answer
 
Comments
Nelek 15-Apr-12 6:16am    
Fair enough. I used the loop option because he already has two in the source code he posted (although I think that code has nothing to do with the question). My 5, lazyness is good ;)
thank you for your all comments.You understand correct what I want to find.Sorry for my explanation that not quite clear.
 
Share this answer
 
Comments
Nelek 15-Apr-12 7:54am    
You are welcome. Offtopic: if a solution helps you, please accept is as answer.
I wrote like this but when i run it state segmentation fault.can u help me what did I do wrong?I find the maximum value in the first loop and then find the different at second times.

C++
typedef struct nb_tuple {
        nsaddr_t nb_main_addr_;
        double reserved_;
        inline nsaddr_t&	nb_main_addr()	{ return nb_main_addr_; }
	inline double& reserved(){return reserved_;}
}nb_tuple;

   nb_tuple* max = NULL;
   nb_tuple* maxEg = NULL;
   int threshold=2;
   double diff = NULL;
   int max_r = 0;
	for (set<int>::iterator it = rs.begin(); it != rs.end(); it++) {
	   int r = *it;
	   if (r > 0) {
	      for (std::vector<nb_tuple*>::iterator it2 =reachability[r].begin();
	       it2 != reachability[r].end();it2++) { 
               nb_tuple* tuple= *it2;
	          if(tuple->reserved() > maxEg->reserved())
                   maxEg = tuple;
	      }
  
              for (std::vector<nb_tuple*>::iterator it2 = reachability[r].begin();
		it2 != reachability[r].end();
		it2++) {
	        nb_tuple* tuple = *it2;
	        diff = (maxEg->reserved()- tuple->reserved());
	            if ( diff > threshold)
	 	    { max = nb_tuple;
		      max_r = r;
		    }

</int>
 
Share this answer
 
v3
Comments
Nelek 15-Apr-12 17:29pm    
I think you are complicating it more than necessary. Why are you running the loops inside another loop? I think you should first get the algorithm working in a simple way, and then adapt it to your requirements. I'm editing my solution have a look

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900