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 void sort(T* arr, int size) { for (int i = 1; i < size; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } }

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 long double calcMedian(T* arr, int size)

Now, I've searched online and there is a function called "sort" using #include in the source.cpp.

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 long double calcMedian(T* arr, int size) { // First we sort the array sort(arr, arr + size); // check for even case if (size % 2 != 0) return (T)arr[size / 2];

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 #include #include "ArrayFunctions.h"

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 long double calcAverage(T* arr, int size) { T average; long double total = 0; for (int i = 0; i < size; i++) total = total + arr[i]; average = total / size; return average; }

template T findMax(T* arr, int size) { T max = 0; for (int i = 0; i < size; i++) { if (*(arr + i) > max) { max = arr[i]; } } return max; }

template T findMin(T* arr, int size) { T min = arr[0]; for (int i = 0; i < size; i++) { if (*(arr + i) < min) { min = arr[i]; } } return min; }

Sorry this is long.. and thank you.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!