Help on getting median

the program complies but the output in the median is 1.86 every time no matter what values I use.

[code]


#include <iostream>
#include <cstdlib>
#include <limits>
using namespace std;


double median(int , int);
double mode(int *, int);
int *makeArray(int);
void getMovieData(int *, int);
void selectionSort(int [], int);
double average(int *, int);

int main ()
{

double average;
int *students;
int numstudents;
double total = 0;
double median;

cout << "How many Students were Surveyed?" << endl;
cin >> numstudents ;

while(numstudents <= 0)
{
cout << "invalid Entry \n";
cout << "Enter number of students? \n";
cin >> numstudents;
}

students = new int [numstudents];
cout << "Enter the number of movies each Student watched \n";

for (int count = 0; count < numstudents; count++)
{
cout << " student#" << (count +1 ) << ": " ;
cin >> students[count];
}

for (int count = 0; count < numstudents; count++ )
{
total += students[count];
average = total / numstudents;
}


cout << "Students Watched on Average: " << average <<" Movies"<< endl;
cout << "the median is" << median << endl;

system("Pause");

}
double median (int numbers [], int size)
{

double median;

if ( size % 2 == 0)

double median = (int) (numbers[size /2] + numbers[size / 2 ])/2;

else

{
median = numbers[size/2];
}

}
First, format your code.

Second, you never used "median" variable. You just printed it, without assigning anything to it. Therefore, it's not set.

PS. and I recommend you not to use system(), it's not safe(you can google it if you want to know why). Also, remember to delete "students", because you allocated it with operator new, so it's your task to free its memory.
Last edited on
Topic archived. No new replies allowed.