80.6k views
3 votes
Write a function named countOutliers that has a vector parameter (vector of integers), and two single integer parameters. The function should count all values in the vector parameter that lie outside the values of the 2 integer parameters, and return that count. If the vector passed in is empty, the returned count will be 0.

The order of the integer arguments passed in should not matter. In other words, your program should perform exactly the same if the 1st integer argument is larger than the 2nd, or vice-versa (though you may want to enforce a specific order of the paramters inside your function by swapping them if needed).

1 Answer

4 votes

Final answer:

The 'countOutliers' function counts elements in a vector that are outside the range of two integer parameters, adjusting for their order and returning 0 for an empty vector.

Step-by-step explanation:

The countOutliers function is designed to find and count elements within a vector that are outside the range defined by two integer parameters. The function should work regardless of the order of the two integer bounds. If the vector is empty, the function returns 0. This task involves concepts of programming, functions, and iteration over data structures.

To write this function, you would start by ensuring that the lower bound is less than the upper bound, swapping them if necessary. Then, iterate through the vector, incrementing a count for each element that is less than the lower bound or greater than the upper bound. Finally, return the count.

Here's a generic example in pseudocode:

function countOutliers(vector, int1, int2) {
lowerBound = min(int1, int2)
upperBound = max(int1, int2)
count = 0
for each value in vector {
if value < lowerBound or value > upperBound {
count = count + 1
}
}
return count
}
User Evertiro
by
8.1k points