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

Expert's answer

#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;

}

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;

}

## Comments

## Leave a comment