Calculating the median from a set of numbers

I am supposed to compute the median from a data file with sorted numbers and display the contents of the data file on the screen as well as the median. I think i got my algorithm wrong someone please advise.The data files contain the following info:

File1:
1 4 6 12 14 18 29 33 37 40 45 47 49 51 55 56 59 60 63
File2:
3 6 7 9 13 16 19 21 26 33 39 41 47 51 58 65 77 80

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>
using namespace std;
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cmath>

void calcMedian(ifstream& in_stream, double median, int count);
//calculates the median of set of numbers provided in the input file

int main()
{
ifstream fin;
double median = 0; ;
int count;
//Open the input file.
cout << "Opening the input file..." << endl;
fin.open("File1.txt");
if (!fin)
{
cout << "Error accessing input file." << endl;
exit(1);
 }
//Calculate the median of the numbers in the file.
  calcMedian(fin, median, count);
//Reset to the beginning of the file.
fin.close();
fin.open("File2.txt");
if (!fin)
{
cout << "Error accessing input file." << endl;
exit(1);
 }
//Calculate the median of the numbers in the second file.
calcMedian(fin, median, count);
 //Close the file.
fin.close();
 //Output the median of the file.
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The Median of the numbers in the  first file is "<< median << "." << endl << "The Median of the numbers in the second file is " << median << "." << endl;
return 0;
}
void calcMedian(ifstream& in_stream, double median, int count)
 {
 double next;
 count = 0;
while(in_stream >> next)
{
next = next + 1;
 count++;
}
  median = count/ 2;

 }
Hello nomat,

Without testing the first things I see are:

Line 14 you should use 0.0 to initialize a double and there is an extra semi-colon at the end.

The "cacMedian function is all wrong. You are passing "median" by value, so it is only a copy. When the function ends you loose the value of "median" and "median" defined in main is still 0.0. If you want the change to be reflected in main then you need to pass "median" by reference
void calcMedian(ifstream& in_stream, double& median, int& count) and the same may be needed for "count" if it is used in main.


The contents for "calcMedian" is not the way to calculate the median. I do not deal with the median very often, so I will have to research how it is done.

Righr off I do not see anything terribly wrong until I have a chance to load it and test it.

Hope that helps for now,

Andy
@nomat,

The median is the "middle" of a sequence of N numbers put in sorted order. Specifically:
- take the middle value if N is odd;
- take the average of the two central values if N is even.

You aren't doing anything like that at the moment. You are merely counting the number of items in the file.

I suggest:
- Read your data into a vector<double>; the size() member function will then tell you N.
- Sort this vector using the sort() algorithm; (your example files are already sorted, but you can't rely on that).
- If N is odd (i.e. N % 2 == 1 ) take the middle value; with integer division (and noting that array indices go from 0 to N-1) this will actually be the N/2 (with deliberate integer division) element;
- If N is even then you want the average of the N/2-1 and N/2 elements (again noting that arrays index from 0 to N-1).

Either change your function to a return type double and return the median, or, as Andy pointed out, pass median by reference. (Personally I would do the former).

Since there are many ways of coming up with an array of elements I would split the reading of a file into a vector and the calculating of a median from a vector into two separate tasks (and hence distinct functions), but it is up to you.

The median of the data in file file1.txt is 40
The median of the data in file file2.txt is 29.5

Last edited on
Hello nomat,

lastchance has given you a good explanation of how to find the median. The only thing I can add to that is about the last "cout" statement in main. You are using one variable in two places to print two lines and it does not work. Either you need a second variable to use in your "cout" statement or you need to break up the one "cout" into two, one after each call to "calcMedian".

I used an extra variable because it was quick and it was late last night. Otherwise I would have used two "cout" statements.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.