Question
Median Function
In statistics, when a set of values is stored in ascending or descending order, its median is the middle value. If the set contains an even number of values, the median is the mean, or average, of the two middle values.
Write a function that accepts as arguments the following:
a) A vector of integers
b) An integer that indicates the number of elements in the vector
The function should determine the median of the vector. This value should be returned as a double. (Assume the values in the vector are already sorted)
2)
Mode Function
In statistics, the mode is a set of values in the value that occurs most often or with the greatest frequency.
Write a function that accepts as arguments the following:
A) A vector of integers
B) An integer that indicates the number of elements in the vector
The function should determine the mode of the vector. That is, it should determine which value in the vector occurs most often. The mode is the value the function should return. If the vector has no mode (none of the values occur more than once), the function should return -1. (Assume the vector will always contain nonnegative values).
Solution Preview
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.
int main(int argc, char** argv) {vector<int> set1;
cout << "Set 1: ";
for (int i = 1; i <= 5; i++) {
set1.push_back(i);
cout << i << " ";
}
cout << "\n" ;
cout << "Median: " << MedianFunction(set1, set1.size()) << "\n";
cout << "Mode: " << ModeFunction(set1, set1.size()) << "\n";
vector<int> set2;
cout << "Set 2: ";
for (int i = 1; i <= 6; i++) {
set2.push_back(i);
cout << i << " ";
}
cout << "\n" ;
cout << "Median: " << MedianFunction(set2, set2.size()) << "\n";
cout << "Mode: " << ModeFunction(set2, set2.size()) << "\n";
vector<int> set3;
cout << "Set 3: ";
int val;
for (int i = 1; i <= 8; i++) {
val = rand() % 4;
set3.push_back(val);
cout << val << " ";
}
cout << "\n" ;
cout << "Mode: " << ModeFunction(set3, set3.size()) << "\n";
return 0;
}...
By purchasing this solution you'll be able to access the following files:
Solution.cpp.