Answer to Question #4219 in C++ for Mz Jackee

Question #4219
Determine the median selling price of all homes in a subdivision named Botany Bay sold during one year. Allow the user to enter the number of houses sold and store their selling prices in an array. The median of a list of N numbers is as follows:

a. The middle number of the sorted list, if N is off
b. The average of the two middle numbers in the sorted list, if N is even

(Hint: after inputting the prices into an array, sort that array.)
1
Expert's answer
2011-09-18T16:00:50-0400
#include <iostream>
using namespace std;

int main() {
int num;
cout<<"enter number"<<endl;
cin>>num;
int arr[num];
cout<<"enter prices"<<endl;
for (int i = 0; i<num; i++)
cin>>arr[i];
for (int i = 0; i<num; i++)
for (int j = 0; j<num-1; j++)
if (arr[i]<arr[j]) {
int buf = arr[i];
arr[i] = arr[j];
arr[j] = buf;
}
cout<<"result"<<endl;
if ((num % 2) == 0)
cout<<(arr[num/2] + (float)arr[(num/2)-1])/2<<endl;
else
cout<<(arr[num/2]);
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS