Finding the MODE from an array

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Finding the MODE from an array

Postby JB » Wed Sep 24, 2003 10:09 pm

Hi,

I'm new to programming, I need to find the mode of integers from sorted array,

I don't get the logic behind this, in other words I can display the numbers
But I can't compare the nombers next to each other...

Here is what I have so far, any help will be appreciated...

Thank you,
JB

----------------------
void mode(int array[99], int count)
{
int track, temp;
track=0;
count=0;

cout<<"\nMODE List "<<array[count];
cout<<endl;

for (int i=1; i<=count; i++)
{
if (array[i]==array[i+1])
track++; //HERE i'm trying to track how many times I find the number
else (i==i+1);

cout<<"\n TRACK "<<track; //small test to see if it works

// cout<<list[count+1];
}
JB
 

Postby Guest » Wed Sep 24, 2003 11:00 pm

First set up an array with frequenct numbers
say from 1 to 10 or whatever you think would be the most possible
number of instances of any given bumber.
Then, sort the array with all your numnbers.
then set up a for loop to look for the number og instances of that number:
// to summarize your frequencies:
for (int i = 0; i < arraySize; i++)
++frequencyArray[sortedArray[i]];
outside the for loop, loop through yhe frequeny array
using i as your subscript value am int called biggest and put the
biggest frequency in that value
and anoyjer one for the value and you will have your mode.
Guest
 

Postby Wizard » Thu Sep 25, 2003 6:55 am

Code: Select all
for (int i=1; i<=count; i++)

arrays start at 0, and go up to size-1. In your example array[99], it can be indexed from array[0] to array[98]. If count == 99, and you are going up to and including 99, it could do something unwanted when it tries to access array[99].
Code: Select all
if (array[i]==array[i+1])

very dangerous. Again, if i=98, then i+1 will be 99, which may do weird things.
Code: Select all
track++; //HERE i'm trying to track how many times I find the number

Can I suggest that, not only do you increase the track number, but also set some kind of variable which is the actual number that has that count. Know what I'm saying?
Code: Select all
else (i==i+1);

Ok, this doesn't do anything. I think what you were looking for was
Code: Select all
else (track=0);

to reset the track number.

You'll also need some way of remembering the number that had the highest track (something like maxNum, and maxTrack) and then compare track to maxTrack every so often. If track > maxTrack then set new maxNum and new maxTrack. Understand?
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Postby Guest » Thu Sep 25, 2003 11:27 am

// It computes the mode of the data.
// sort array
bubbleSort( response, responseSize );
// process response
mode( frequency, response, responseSize );
// determine most frequent response
void mode( int freq[], int answer[], int size )
{
int largest = 0; // represents largest frequency
int modeValue = 0; // represents most frequent response
// initialize frequencies to 0
for ( int i = 1; i <= 9; i++ )
freq[ i ] = 0;
// summarize frequencies
for ( int j = 0; j < size; j++ )
++freq[ answer[ j ] ];
// output results
for ( int rating = 1; rating <= 9; rating++ ) {
cout << setw( 8 ) << rating << setw( 11 )
<< freq[ rating ] << " ";
// keep track of mode value and largest fequency value
if ( freq[ rating ] > largest ) {
largest = freq[ rating ];
modeValue = rating;
} // end if
// output histogram bar representing frequency value
for ( int k = 1; k <= freq[ rating ]; k++ )
cout << '*';
cout << '\n'; // begin new line of output
} // end outer for
// display the mode value
cout << "The mode is the most frequent value.\n"
<< "For this run the mode is " << modeValue
<< " which occurred " << largest << " times." << endl;
} // end function mode
Guest
 


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 2 guests