|
-
April 22nd, 2003, 12:07 PM
#1
I need some help w/a programming project...
Okay...don't laugh...
I am a freshman C.S. major at my college and I am starting out learning c++ on a linux console machine. We are currently writing a program that reads in a list of numbers from a file and populates an array with those values. Then, it sorts the values and computes the mean, median, mode, and standard deviation. I am having one slight problem. The function I wrote for finding the mode only returns the highest value(ie, if there are two or three modes it only returns one of them - the highest value). I have included my humble little piece of code and if it isn't too much trouble with for you gurus I would greatly appreciate some assistance...here are the questions I need answered:
1. Is there a way to simply modify my existing code(i.e. changing the type to void and adding another loop over the top)
2. Do I need to use another array to determine the frequency of each value and if so, how do I implement it?
3. If I do need to use an array for frequency, how could I compare the two arrays to find all of the correct frequency indices?
If I am posting in the wrong forum, sorry...if you know where I could get help on this I would love to know...anyway...here is my function for the mode:
(I realize I will probably have to change its type to void since I need to find multiple modes...as it is, it returns the highest value mode regardless of how many there are)
Code:
int mode(int array[], int SIZE_OF_ARRAY)
{
using namespace std;
int counter = 0;
int max = 0;
int i;
int mode_index = 0;
for (int i = 0; i < SIZE_OF_ARRAY; i++)
{
if (array[i] == array[i + 1])
{
counter++;
}
else
{
if (counter > max)
{
max = counter;
}
counter = 0;
}
}
counter = 0;
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
if (array[i] == array[i + 1])
{
counter++;
}
else
{
if (counter == max)
{
mode_index = i;
}
counter = 0;
}
}
return array[mode_index];
}
Last edited by xsyntricity; April 22nd, 2003 at 01:06 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|