Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Vector Code Write a function named countOutliers that has a vector parameter

ID: 3573896 • Letter: C

Question

C++ Vector Code

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).

For example, when the function is called, if the vector argument has the values {1, 8, 4, 9, 3, 7, 100, -4, 2, 5, 0} and the two integer arguments are 9 and 2 (or 2 and 9), then the function will return 4 (since {1,100, -4, 0} lie outside the range {2, 9}).

Give an example of the invocation of this function; you must show the declarations of any variables needed for this purpose, but you do not need to "populate" them - i.e. do not show where they are assigned values.

Explanation / Answer

#include<stdio.h>
#include <vector>

// declaration
int countOutliers(std::vector<int>,int,int);

// main method - giving sample input and calling countOutliers function
main(){
   static const int arr[] = {1, 8, 4, 9, 3, 7, 100, -4, 2, 5, 0};
   std::vector<int> numbers (arr, arr + sizeof(arr)/sizeof(arr[0]));
   int lowerBound = 2, upperBound = 9;
   printf("%d",countOutliers(numbers,lowerBound,upperBound));
}

// Function which gives the count of numbers not in given range
int countOutliers(std::vector<int> nums, int lowerBound, int upperBound){
   int count = 0;
   std::vector<int>::iterator it;

   for (it = nums.begin(); it!=nums.end(); ++it) {
   if(*it<lowerBound || *it>upperBound){
       count++;
       }
   }
   return count;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote