Question: Hello. Learning c++ here. Problem I have right now is trying to figure out how to use sort function to calculate the median of an
Hello. Learning c++ here.
Problem I have right now is trying to figure out how to use sort function to calculate the median of an array.
My teacher provided us a function in the separate header file using template, the function she provided us is:
template
Or, she said we could create our own sort function to find the median of an array.
The reason she provided us this sort function is because she wanted us to find the median of an array such as grades or temperature.
She also provided us with the function delcaration for the calcMedian which is:
template
Now, I've searched online and there is a function called "sort" using #include
However, she wants all the function in the separate header file called ArrayFunctions.h.
But I run into another problem. When I use #include in an separate header file, it seem to not identify the "sort" in that header file to calculate the median.
The code I've written in the separate header file is:
#ifndef _ArrayFunctions_ #define _ArrayFunctions_ #include
template
return (T)(arr[(size - 1) / 2] + arr[size / 2]) / 2.0; }
#endif
When I use this code in the source.cpp file it works fine to calculate the median, but it does now work in the separate header file.h.
Need to figure out if there is a way to use the sort function to calculate the median or find a way to use the #include
Here is my main source.cpp code:
#include
int main() { int num; //read the number of grades cout << "Enter the number of Grades : "; cin >> num; cin.ignore(100, ' '); //Bullet Proofing while (num < 1 || num > 100) { cout << "Input error. Please try again: "; cin >> num; cin.clear(); cin.ignore(100, ' '); } double grades[100]; //read the elements into the array for (int i = 0; i < num; i++) { cout << "Enter " << (i + 1) << " Grade : "; cin >> grades[i]; } //call the functions and display the result cout << "Average : " << calcAverage(grades, num) << endl; cout << "Max : " << findMax(grades, num) << endl; cout << "Min : " << findMin(grades, num) << endl; cout << "Median : " << calcMedian(grades, num)<< endl; }
and here is what I have in the separate header file:
#ifndef _ArrayFunctions_ #define _ArrayFunctions_ #include
template
template
template
Sorry this is long.. and thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
