returning the stored value of greatest difference from ite neighbors

im having trouble implementing this http://www.cplusplus.com/reference/numeric/adjacent_difference/ to find the largest number compared to its numbers in a vector and return that number, ties are broken by index location so i tried a less high functioning code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double mostIsolated(vector<double> & number)
{
	{
	int size = number.size();
	double dist = 0;
	double temp = 0;
	double result=0.0;
	for (int i = 0; i < size-1; i++) {
		temp = number[i] - number[i + 1];
		if (temp > dist) {
			dist = temp;
			result = number[i];
		}
		
	}
	return result;
 
 
} 


Comment bellow holds more info...
Last edited on
Doesn't each (internal) element have TWO neighbours?

Isn't the "distance" always positive?
Last edited on
d1g1talarts had thread http://www.cplusplus.com/forum/general/233708/ on the apparently same problem.
There we sort of got the details sorted out.

@d1g1talarts:
On a new thread you cannot assume that others have seen your previous threads and therefore you have to supply all relevant details again. On the other hand some of us get a deja vu frustration when we read essentially the same question multiple times.

You don't have to implement std::adjacent_difference, the C++ Standard Library has already done that. You obviously like to do it for learning purposes, which is ok, but std::adjacent_difference does not actually solve your problem alone. It provides just one step of the solution.

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
double mostIsolated( const vector<double> & number )
{
  // assume that 0<number.size() and number is sorted, ascending

  if ( 1 == number.size() ) return number[0];

  vector<double> diffs( number.size() );
  std::adjacent_difference( number.begin(), number.end(), diffs.begin() );

  size_t result = 0;
  double near = diffs[result+1]; // first element has only one neighbour
  // the last element does not have to be considered due to
  // "ties may be resolved using the neighbor with the lowest index"

  for ( size_t index=1; index < number.size() - 1; ++index ) {
    double distance = // distance to nearest neighbour of number[index]
    // i.e. to number[index-1] or to number[index+1]
    // the distances to consider are in the 'diffs'

    if ( near < distance ) {
      near = distance;
      result = index;
    }
  }
  return number[result];
}


You can write your own adjacent_difference().
You can fuse (inline) adjacent_difference() into mostIsolated().
Last edited on
Sorry @Keskiverto, I must have missed the previous thread on this issue. I'll have to remember to click on the OP's name in future to see if there is any relevant past "history" on this topic.
im still having trouble. i have my dummy code working a little better but its still returning the wrong number. So from what i understand, i have to find the number in the array that is the greatest distance ( as in on a number line distance) from its nearest neighbor (again neighbor here is referencing how close on the number line)

i dont understand
std::adjacent_difference (val, val+7, result);
in your code. what is val, where did it come from, what does it do? result is also initialized after being called. does that work?
Also my signature was implemented by my instructor and i think adding const may be frowned upon.

my code now looks like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 double mostIsolated(vector<double> & number)
{
	std::sort(number.begin(),number.end());
	int size = number.size();
	double dist = 0;
	double temp = 0;
	double result=0.0;
	for (int i = 0; i < size-1; i++) {
		temp = number[i] - number[i + 1];
		temp=pow(temp,2);
		temp=sqrt(temp);
		if (temp > dist) {
			dist = temp;
			result = number[i];
		}
		
	}
	return result;
	


} 
Last edited on
Sorry about the val. A mistake paste that had slipped in. Fixed now.
My code is deliberately incomplete. It needs something on line 16.

The const is not mandatory.

I thought that in other thread you wrote that the number is already sorted? If it is, then there is no reason to sort it again.


You do sort ascending. Therefore, number[i] <= number[i + 1] must be true for all pairs.

For that follows that number[i] - number[i + 1] <= 0
and 0 <= number[i + 1] - number[i]

Your sqrt(temp*temp) does remove the sign, but so would abs(temp), moreover, number[i + 1] - number[i] would not even need that.


You still have the main issue that you are not looking for the most isolated value.

Take 0 1 4 5 9 11.

The largest single distance is between 5 and 9, the 4. Your code would return 5 as the answer. The 5 is not the most isolated. It is only 1 from 4.

Lets make a table: value (v), distance from previous (p), distance to next (n):
1
2
3
4
5
6
7
 v p n
 0   1
 1 1 3
 4 3 1
 5 1 4
 9 4 2
11 2

For each value you have to look at both distances to find out which one of them is smaller. Lets add one more column, the minimal distance (m):
1
2
3
4
5
6
7
v p n m
 0   1 1
 1 1 3 1
 4 3 1 1
 5 1 4 1
 9 4 2 2
11 2   2

Now we see that 9 and 11 are both at least 2 away from their neighbours. The 9 is the first of them and therefore the correct answer.


Can you take my code and wrote on its line 16 a statement that assigns to the 'distance' the "minimal distance"?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 if (1 == number.size()) return number[0];

		vector<double> diffs(number.size());
		std::adjacent_difference(number.begin(), number.end(), diffs.begin());

		size_t result = 0;
		double prox = diffs[result + 1]; // first element has only one neighbour
										 // the last element does not have to be considered due to
										 // "ties may be resolved using the neighbor with the lowest index"

		for (size_t index = 1; index < number.size() - 1; ++index) {
			double distance = diffs[index]+diffs[index+1]; // distance to nearest neighbour of number[index]
							  // i.e. to number[index-1] or to number[index+1]
							  // the distances to consider are in the 'diffs'

				if (prox < distance) {
					prox = distance;
					result = index;
				}
		}
		return number[result];
	}

god i feel dense now.
had some issues with the var name near so i just changed it. i think someone who created one of the classes may have done a #define.
@keskiverto
thank you so much for walking me through that, like really appreciated.
Last edited on
You are getting closer, but is a sum (s) same as minimum (m)?
1
2
3
4
5
6
7
 v p n m s
 0   1 1 1
 1 1 3 1 4
 4 3 1 1 4
 5 1 4 1 5
 9 4 2 2 6
11 2   2 2

double distance = std::min( diffs[index], diffs[index+1] );
Last edited on
Topic archived. No new replies allowed.