#include <cstdlib> #include <fstream> #include <iostream> #include <iomanip> using namespace std; const int MAX = 50; const int N = 1000; void read_values ( ifstream& in_f, int v[], int& n ); void count_values ( const int v[], int n, int f[] ); int find_mode ( const int frs[] ); double find_mean(int frs[]); int main() { ifstream in_f; // The input file stream int values[N]; // The array of values int freqs[MAX+1]; // The frequency array int count; // The number of values read in int mode; // The mode of the values double mean; double sum; char filename[80]; cout << "Please enter the filename: "; cin >> filename; in_f.open(filename); // Open the input file. if ( ! in_f ) { cout << "Error opening the input file." << endl; exit(100); } read_values ( in_f, values, count ); // Read the input values. in_f.close(); count_values ( values, count, freqs ); // Build the frequency array. mode = find_mode ( freqs ); // Find the mode of the values. mean = find_mean( freqs ); cout << "The mode of the values is " << mode << "." << endl; cout << "The mean of the values is " << mean << "." << endl; cout << "It appears " << freqs[mode] << " times." << endl; return EXIT_SUCCESS; } void read_values ( ifstream& in_f, int v[], int& n ) { int number; n = 0; in_f >> number; while ( n < N && ! in_f.eof() ) { v[n] = number; ++n; in_f >> number; } } void count_values ( const int v[], int n, int f[] ) { int i; // An indexing variable // Initialize the frequencies to 0. for (i=0; i<=MAX; ++i) f[i] = 0; // Count the values in v[]. for (i=0; i<n; ++i) if ( 0 <= v[i] && v[i] <= MAX ) f[v[i]] += 1; } int find_mode ( const int frs[] ) { int k; // Loop index int mode; // Most popular value so far int max; // Count of the most popular value so far mode = 0; max = frs[0]; for (k=1; k<=MAX; ++k) if ( frs[k] > max ) { max = frs[k]; mode = k; } return mode; } double find_mean(int frs []) { int i = 0; double sum = 0.0; //Computes the mean and returns it to the main fucntion. for (i = 0; i<=9; i++) { sum = sum + frs[i]; } return (sum/10.0); }

## 8 Replies - 8950 Views - Last Post: 13 November 2007 - 11:25 PM

### #1

# Finding Mean

Posted 13 November 2007 - 06:57 PM

I am trying to get this program to output the mean. I got it to output mode correctly but that is it. For some reason it will not pass the mean back it just outputs 0. Any help would be appreciated. I'm not sure where the error is in my code.

##
**Replies To:** Finding Mean

### #2

## Re: Finding Mean

Posted 13 November 2007 - 07:01 PM

Try passing in the values array to your function, not the frequency array.

mean = find_mean( values );

### #3

## Re: Finding Mean

Posted 13 November 2007 - 07:28 PM

### #4

## Re: Finding Mean

Posted 13 November 2007 - 07:36 PM

jayman9, on 13 Nov, 2007 - 07:01 PM, said:

Try passing in the values array to your function, not the frequency array.

mean = find_mean( values );

I have one more questions for you guys. Now that I am finding the mean, I am having trouble finding the median. I believe I should just have to sort the array. I chose bubble sort for this. But I am lost after that.

### #5

## Re: Finding Mean

Posted 13 November 2007 - 07:55 PM

Once you have a sorted array, the median is the middle element in your list. If you have an odd number of elements, this is exactly the middle element; if you have an even number of elements, this is the average of the two elements surrounding the "middle" of the list (since the middle lies between two elements in a list with an even number of elements).

If you're asking about the sorting algorithm itself, take a look at the snippets section for C++ - there are a number of implementations of sorting algorithms there for you to base your off of.

If you're asking about the sorting algorithm itself, take a look at the snippets section for C++ - there are a number of implementations of sorting algorithms there for you to base your off of.

### #6

## Re: Finding Mean

Posted 13 November 2007 - 10:02 PM

jjhaag, on 13 Nov, 2007 - 07:55 PM, said:

Once you have a sorted array, the median is the middle element in your list. If you have an odd number of elements, this is exactly the middle element; if you have an even number of elements, this is the average of the two elements surrounding the "middle" of the list (since the middle lies between two elements in a list with an even number of elements).

If you're asking about the sorting algorithm itself, take a look at the snippets section for C++ - there are a number of implementations of sorting algorithms there for you to base your off of.

If you're asking about the sorting algorithm itself, take a look at the snippets section for C++ - there are a number of implementations of sorting algorithms there for you to base your off of.

I'm just trying to get it to output any median without a separate function at the moment. I presorted the values in my input file. But im not sure what I should be throwing into it. I figured I would need the frequency array of the values and I tried a couple more. It is right above the output in the program. Any help to get me on the right track would be appreciated!

#include <cstdlib> #include <fstream> #include <iostream> #include <iomanip> using namespace std; const int MAX = 50; const int N = 1000; void read_values ( ifstream& in_f, int v[], int& n ); void count_values ( const int v[], int n, int f[] ); int find_mode ( const int frs[] ); double find_mean(int frs[], int count); int main() { ifstream in_f; // The input file stream int values[N]; // The array of values int freqs[MAX+1]; // The frequency array int count; // The number of values read in int mode; // The mode of the values double mean; double sum; char filename[80]; int median; cout << "Please enter the filename: "; cin >> filename; in_f.open(filename); // Open the input file. if ( ! in_f ) { cout << "Error opening the input file." << endl; exit(100); } read_values ( in_f, values, count ); // Read the input values. in_f.close(); count_values ( values, count, freqs ); // Build the frequency array. mode = find_mode ( freqs ); // Find the mode of the values. mean = find_mean( values, count ); median = ((freqs[values]/2)+(freqs[values]+1)/2)/2; //Output of the results cout << "The mode of the values is " << mode << "." << endl; cout << "The mean of the values is " << mean << "." << endl; cout << "The median of the values is " << median << "." << endl; cout << "It appears " << freqs[mode] << " times." << endl; cout << count << endl; return EXIT_SUCCESS; } void read_values ( ifstream& in_f, int v[], int& n ) { int number; n = 0; in_f >> number; while ( n < N && ! in_f.eof() ) { v[n] = number; ++n; in_f >> number; } } void count_values ( const int v[], int n, int f[] ) { int i; // An indexing variable // Initialize the frequencies to 0. for (i=0; i<=MAX; ++i) f[i] = 0; // Count the values in v[]. for (i=0; i<n; ++i) if ( 0 <= v[i] && v[i] <= MAX ) f[v[i]] += 1; } int find_mode ( const int frs[] ) { int k; // Loop index int mode; // Most popular value so far int max; // Count of the most popular value so far mode = 0; max = frs[0]; for (k=1; k<=MAX; ++k) if ( frs[k] > max ) { max = frs[k]; mode = k; } return mode; } double find_mean( int frs[], int count ) { int i = 0; double sum = 0.0; //Computes the mean and returns it to the main fucntion. for (i = 0; i<=29; i++) { sum = sum + frs[i]; } return (sum/count); }

This post has been edited by **DewK**: 13 November 2007 - 10:04 PM

### #7

## Re: Finding Mean

Posted 13 November 2007 - 10:16 PM

The median has nothing to do with frequencies. It is the value that separates the higher half of a distribution from the lower half.

If your array of elements (NOT your frequency array) is already sorted, the median is the "middle" value in the array. To get the middle value, you'll have to figure out if you have an even or odd number of elements.

An example:

Even number of elements:

Odd number of elements:

All it takes to find the median value is a count of the number of elements, and an ordered array. No frequencies. You just need to implement a code version of the graphical examples given above.

If your array of elements (NOT your frequency array) is already sorted, the median is the "middle" value in the array. To get the middle value, you'll have to figure out if you have an even or odd number of elements.

An example:

Even number of elements:

1 1 2 3 5 7 9 9 ^ median = (3+5)/2 = 4;

Odd number of elements:

1 1 2 5 7 9 9 ^ median = 5;

All it takes to find the median value is a count of the number of elements, and an ordered array. No frequencies. You just need to implement a code version of the graphical examples given above.

### #8

## Re: Finding Mean

Posted 13 November 2007 - 11:05 PM

jjhaag, on 13 Nov, 2007 - 10:16 PM, said:

The median has nothing to do with frequencies. It is the value that separates the higher half of a distribution from the lower half.

If your array of elements (NOT your frequency array) is already sorted, the median is the "middle" value in the array. To get the middle value, you'll have to figure out if you have an even or odd number of elements.

An example:

Even number of elements:

Odd number of elements:

All it takes to find the median value is a count of the number of elements, and an ordered array. No frequencies. You just need to implement a code version of the graphical examples given above.

If your array of elements (NOT your frequency array) is already sorted, the median is the "middle" value in the array. To get the middle value, you'll have to figure out if you have an even or odd number of elements.

An example:

Even number of elements:

1 1 2 3 5 7 9 9 ^ median = (3+5)/2 = 4;

Odd number of elements:

1 1 2 5 7 9 9 ^ median = 5;

All it takes to find the median value is a count of the number of elements, and an ordered array. No frequencies. You just need to implement a code version of the graphical examples given above.

I tried applying this concept but I am unfortunatly still stuck. I think I am making this harder than it is. I think I've been staring at the code for so long I'm just throwing myself off.

### #9

## Re: Finding Mean

Posted 13 November 2007 - 11:25 PM

Well, there's your problem right there - you shouldn't be looking at the code right now. You should at most be looking at some pseudocode, but you'd probably be best off walking away from your computer for a few minutes. This is because you're having trouble with the algorithm itself, not with how to implement it in code.

You know how many elements there are in the array, correct? And the array is sorted? If that's the case...

You've got your array

If you have an odd number of elements, the middle element is found exactly at count/2. Let's say you have 5 elements in your array. Because of the peculiarites of integer division, when you take 5/2, this gives you 2, since the numbers beyond the decimal are dropped when you divide an integer by an integer. And since array indices are zero-based in C/C++, the element with the index value of 2 is in the middle of the array {0,1,

Now let's consider an array with an even number of elements. Since there is no element that is exactly in the middle of the array, you need to take the average of the two elements on either side of the midpoint. Let's say the array has 6 elements (count=6), with indices given by {0,1,2,3,4,5}. If you were to divide count by 2, this gives you 6/2=3. And the element at index 3 (the fourth element in the array) is the position to the right of the midpoint in your array. So to get the "midpoint" value, you would average the elements at positions count/2 and (count/2)-1 - that means add them and divide by two.

So to find the median, you'll first need to test whether you have an even or odd number of elements. Then, you'll need to translate the above algorithm from plain english into code, putting the two parts in the appropriate sections of an if-else statement.

Hopefully, that will give you enough to tackle this .

-jjh

You know how many elements there are in the array, correct? And the array is sorted? If that's the case...

You've got your array

*values*, and you know that there are*count*elements in it. So how do you find the middle element of an array of*count*elements? Well, as a first approximation, that would be count/2, giving you the same number of elements above and below the median.If you have an odd number of elements, the middle element is found exactly at count/2. Let's say you have 5 elements in your array. Because of the peculiarites of integer division, when you take 5/2, this gives you 2, since the numbers beyond the decimal are dropped when you divide an integer by an integer. And since array indices are zero-based in C/C++, the element with the index value of 2 is in the middle of the array {0,1,

**2**,3,4}.Now let's consider an array with an even number of elements. Since there is no element that is exactly in the middle of the array, you need to take the average of the two elements on either side of the midpoint. Let's say the array has 6 elements (count=6), with indices given by {0,1,2,3,4,5}. If you were to divide count by 2, this gives you 6/2=3. And the element at index 3 (the fourth element in the array) is the position to the right of the midpoint in your array. So to get the "midpoint" value, you would average the elements at positions count/2 and (count/2)-1 - that means add them and divide by two.

So to find the median, you'll first need to test whether you have an even or odd number of elements. Then, you'll need to translate the above algorithm from plain english into code, putting the two parts in the appropriate sections of an if-else statement.

Hopefully, that will give you enough to tackle this .

-jjh

Page 1 of 1