Subject Computer Science C-Family Programming

Question

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

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse 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;
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

$20.00

or $1 if you
register a new account!

Assisting Tutor

Related Homework Solutions

Get help from a qualified tutor
Live Chats