# arrays and functions (beginner q)

• 07-12-2002
eazhar
arrays and functions (beginner q)
edit since this question is more important than my other now:

Code:

``` void mode( int freq[], int answer[], int size ) {   int rating, largest = 0, modeValue = 0;   cout << "\n********\n  Mode\n********\n";   for ( rating = 1; rating <= 9; rating++ )       freq[ rating ] = 0;   for ( int j = 0; j < size; j++ )       ++freq[ answer[ j ] ];   cout << "Response"<< setw( 11 ) << "Frequency"         << setw( 19 ) << "Histogram\n\n" << setw( 55 )         << "1    1    2    2\n" << setw( 56 )         << "5    0    5    0    5\n\n";   for ( rating = 1; rating <= 9; rating++ ) {       cout << setw( 8 ) << rating << setw( 11 )           << freq[ rating ] << "          ";       if ( freq[ rating ] > largest ) {         largest = freq[ rating ];         modeValue = rating;       }       for ( int h = 1; h <= freq[ rating ]; h++ )         cout << '*';       cout << '\n';   }   cout << "The mode is the most frequent value.\n"         << "For this run the mode is " << modeValue         << " which occurred " << largest << " times." << endl; }```

what it basically does is display the types of responses, the frequency at which those responces occurred (it puts that data in a nice tabular format) and alongside a histogram of the data. It then states the mode and its frequency. Now my problem, I'm having trouble with an array of a "tie" of mode values (example: 8 and 9 both occurred 20 times) and it only displays 8 occuring 20 times. How can i fix it so it will display both items or more than two for that matter if there is a more-than-two way tie?
• 07-12-2002
Unregistered
if(size % 2 == 0) //if size is an even number;
1)])/2);
else
• 07-12-2002
eazhar
thanks i modified the code to:

Code:

``` void median( int answer[], int size ) {   cout << "\n********\n Median\n********\n"         << "The unsorted array of responses is";   printArray( answer, size );   bubbleSort( answer, size );   cout << "\n\nThe sorted array is";   printArray( answer, size );     if ( size % 2 == 0) {           cout << "\n\nThe median elements are " << size / 2                     << " and " << ( size / 2 ) + 1 << " of\nthe sorted "                         << size                         << " element array.\nFor this run the median is "                         << (answer[ size / 2] + answer [ ( size / 2 ) + 1]) / 2                         << "\n\n";   }   else {           cout << "\n\nThe median is element " << size / 2             << " of\nthe sorted " << size             << " element array.\nFor this run the median is "             << answer[ size / 2 ] << "\n\n";   } }```
I never would have thought about using mod 2 which is the heart of what i needed :D .

Now i have a different problem. This time im doing mode and here is my code for the function:

Code:

``` void mode( int freq[], int answer[], int size ) {   int rating, largest = 0, modeValue = 0;   cout << "\n********\n  Mode\n********\n";   for ( rating = 1; rating <= 9; rating++ )       freq[ rating ] = 0;   for ( int j = 0; j < size; j++ )       ++freq[ answer[ j ] ];   cout << "Response"<< setw( 11 ) << "Frequency"         << setw( 19 ) << "Histogram\n\n" << setw( 55 )         << "1    1    2    2\n" << setw( 56 )         << "5    0    5    0    5\n\n";   for ( rating = 1; rating <= 9; rating++ ) {       cout << setw( 8 ) << rating << setw( 11 )           << freq[ rating ] << "          ";       if ( freq[ rating ] > largest ) {         largest = freq[ rating ];         modeValue = rating;       }       for ( int h = 1; h <= freq[ rating ]; h++ )         cout << '*';       cout << '\n';   }   cout << "The mode is the most frequent value.\n"         << "For this run the mode is " << modeValue         << " which occurred " << largest << " times." << endl; }```
what it basically does is display the types of responses, the frequency at which those responces occurred (it puts that data in a nice tabular format) and alongside a histogram of the data. It then states the mode and its frequency. Now my problem, I'm having trouble with an array of a "tie" of mode values (example: 8 and 9 both occurred 20 times) and it only displays 8 occuring 20 times. How can i fix it so it will display both items or more than two for that matter if there is a more-than-two way tie?
• 07-12-2002
eazhar
Im thinking that maybe i should include an if statement but im not sure how i could incorperate that to "detect" two modes... After it goes through the frequency loop it totally ignores the other mode and displays one. How can i display two modes????
• 07-13-2002
*ClownPimp*
once you have found the mode, just search through the array again and print out all values that match the mode
Code:

```for (int i = 0; i < size; ++i) {   if (freq[i] == largest)       cout << i << ": " << freq[i] << endl; }```