No global variables. Please solve this using the sort method given within the fi
ID: 3869065 • Letter: N
Question
No global variables.
Please solve this using the sort method given within the findMedian function.
Write a function called findMedian that takes two parameters - an array of int and the size of the array. The function should return the median of the array, which will require sorting the array. This will change the original array, but that's okay for this assignment. If you are testing your code by calling it from a different file, remember to have a function prototype for findMedian in that file.
Sort method:
Explanation / Answer
#include <bits/stdc++.h>
#include<iostream>
int findMedian(int *array, int size) {
std::sort(array, array + size);
for (int i = 0; i < size; i++)
std::cout << array[i] << ' ';
if (size%2 == 1)
return array[size/2];
return (array[size/2] + array[size/2 - 1])/2 ;
}
here you go champ!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.