help with function template: range function

The question is: Write a function template that returns the range of values stored in a vector, that is, the difference between the largest value and the smallest value, where operator < is assumed to be defined for type T.

This is what I have:
1
2
3
4
5
6
7
8
9
10
11
template <typename T>
int Range (vector<T> vec)
{
  for(int i = 0; i < (vec.size() - 1); i++)
    {
      while (vec[i] < vec[i + 1])
       {
         return (vec.begin() - vec.end());
       }
    }
}


Does this look ok? Feedback is appreciated. Thank you.
Why the nested loops? You are going to return only 1 value.
Shouldn't the return type be the same as the vector itself?
vec.begin( ) - vec.end( ) is always the same result ...

1. Find the largest value in the vector
2. Find the smallest value in the vector
3. Compute difference
4. Return difference
could i just do 1 - 4 with iterators?
1 - 4 seems a bit... constant, no?

There are two ways to do it: the efficient way and the short way. The efficient way is using two independent loops to go over each element in order, comparing it to the next (if it exists, make sure to check this!) to find the greatest (in one loop) and the smallest (in the other loop) elements. The short way is to use std::sort and get the front() and back() of the vector.
http://www.cplusplus.com/reference/algorithm/sort/

Take your pick!

-Albatross
How's this look?
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
template <typename T>
int Range(vector<T> vec>
{
   int max;
   for (int i = 0; i < vec.end(); i++)
    {
       if (vec[i +1] > vec[i])
          max = vec[i + 1];
       else
          max = vec[i];
     }

   int min;
   for (int i = 0; i < vec.end(); i++)
    {
        if (vec[i + 1] < vec[i])
           min = vec[i + 1];
        else
           min = vec[i];
    }
 
   int difference = max - min;

   return difference;
}

Better, but think about your if statements for a bit. What would happen if (given your Range function and given that it compiles, which unfortunately it doesn't) I ran this program?

1
2
3
4
5
6
7
int main()
{
	std::vector<int> ints;
	for (int i = 10; i > 0; --i)
		ints.push_back(i);
	std::cout << Range(ints) << "\n";
}


Also... you can't just compare integers and iterators. Sorry. :(

Finally, why not check to see if a program works on your own? Debugging programs yourself is an invaluable skill, you know. ;)

-Albatross
Last edited on
This is not supposed to be a running program...just a function
How's this look?
That's not the way how to find the largest/smallest value.

max:
Initialize max to INT_MIN and compare each element with max. If element larger: max = element

min:
Initialize min to INT_MAX and compare each element with min. If element smaller: min = element
Like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template <typename T>
double Range (vector<T> & vec)
{
   int max = 0;
   for (int i = 0; i < vec.end(), i++)
     {
         vec[i];
         if (vec[i] > max)
            max = vec[i];
     }

   int min = 0;
   for (int i = 0; i < vec.end(); i++)
     {
        vec[i];
        if (vec[i] < min)
           min = vec[i];
     }

   int difference = max - min;
   return difference;
} 
Although you have a working solution, you might want to generalize it as possible:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <typename T>
T Range (std::vector<T> & vec)
{
	if(!vec.empty())
	{
		std::vector<T>::const_iterator constItr = vec.begin();
	    T min = *constItr,  
		 max = *constItr;
		for (; constItr < vec.end(); ++constItr)
		{
		   if (*constItr > max)  max = *constItr;
		   if (*constItr < min)  min = *constItr;
		}
		return max-min;  //requires that T has semantics of overloaded operator -  
	}
	return T();  // or throw some special error code to indicate empty container.
}
Last edited on
Topic archived. No new replies allowed.