Thread: arrays and functions (beginner q)

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    11

    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?
    Last edited by eazhar; 07-12-2002 at 09:00 PM.

  2. #2
    Unregistered
    Guest
    if(size % 2 == 0) //if size is an even number;
    double median = (double)((answer[size/2] + answer[((size/2) -
    1)])/2);
    else
    median = answer[size/2];

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    11
    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 .

    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?

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    11

    Unhappy

    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????

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  2. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  3. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  4. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM
  5. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM