Thread: Hard for me... What about you?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    2

    Hard for me... What about you?

    I'm trying to find out how to program or modifiy a program so that
    it can handle a tie.... for example..... if you had the numbers
    1,1,1,2,2,1,1,2,2,2,3,3,3,6,5,4,8,5,4,5,6 in an array and you counted how many times each number apperied. 1 occured 5 and so did 2 how can you get the program to understand this???
    Any help????
    thanks

    Here's what I got so far

    // Include files
    #include <iostream> //used for cin, cout
    #include <iomanip>

    using namespace std;


    //Global Type Declarations

    //Function Prototypes
    void instruct (void);
    void mean ( const int [], int );
    void median ( int [], int );
    void mode ( int [], int [], int );
    void bubbleSort ( int[], int );
    void printArray ( const int [], int );

    //Global Variables - should not be used without good reason.

    int main ()
    {


    //Declaration section
    const int responseSize = 99;

    int frequency [ 10 ] = { 0 },
    response [ responseSize ] =
    { 6,7,8,9,8,7,8,9,8,9,
    7,8,9,5,9,8,7,8,7,8,
    6,7,8,9,4,9,8,7,8,7,
    7,8,9,8,9,8,9,8,8,9,
    6,7,8,7,8,7,9,8,9,2,
    7,8,9,8,9,8,9,7,5,3,
    5,6,7,2,5,1,9,4,6,4,
    7,8,9,6,8,7,8,9,7,8,
    7,4,4,2,5,3,8,7,5,6,
    9,9,9,9,9,9,9,9,7 };

    mean ( response, responseSize );
    median ( response, responseSize );
    mode ( frequency, response, responseSize );

    //Executable section


    return 0;
    }
    void mean ( const int answer [], int arraySize )
    {
    int total = 0;

    cout<< "********\n Mean\n********\n";

    for ( int j = 0; j < arraySize; ++j )
    total += answer [ j ];

    cout<< "The mean is the average value of the data\n"
    << "items. The mean is equal to the total of \n"
    << "all the data items divided by the number\n"
    << "of data items (" << arraySize
    << total << " / " << arraySize << " = "
    << setiosflags ( ios::fixed | ios:: showpoint )
    << setprecision ( 4 )
    << static_cast < double >( total ) / arraySize << "\n\n";
    }
    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 median is element " << size / 2
    << "of \nthe sorted " << size
    << "element array. \nFor this run the median is "
    << answer [ size / 2 ] << "\n\n";

    }
    void mode (int freq [], int answer [], int size )
    {
    int rating, largest = 0, modeValue = 0, largest2 = 0, modeValue2 = 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 << " and " << modeValue2
    << " which occured " << largest << " times"<< " and " << largest2 <<" times." << endl;
    }

    void bubbleSort ( int a[], int size )
    {
    int hold;

    for ( int pass = 1; pass < size; ++pass)

    for ( int j = 0; j < size - 1; ++j)

    if ( a[ j ] > a[ j + 1 ] )
    {
    hold = a[ j ];
    a[ j ] = a[ j + 1 ];
    a[ j + 1 ] = hold;
    }
    }

    void printArray ( const int a[], int size )
    {
    for ( int j = 0; j < size; ++j )
    {
    if ( j % 20 == 0 )
    cout<< endl;

    cout<< setw ( 2 ) << a[ j ];
    }
    }



    void instruct (void)
    {
    //Declaration section

    //Executable section

    }
    /*
    Program Output

    */

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    just taking a guess, i could be wrong but to find out how many of each number are in the array, could you go something like:

    Code:
    int array[5];
    int 1count = 0;
    int 2count = 0;
    for(int x=0;x<5;x++)
    {
         switch(array[x])
         {
         case 1:
              1count++;
              break;
    
         case 2:
              2count++;
              break;
         
         // etc
         }
    }
    
    cout << "There are " << 1count << " 1s in the array!\n";
    cout << "There are " << 2count << " 2s in the array!\n";
    ?
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    2
    thanks... let me give it a try and see what I can come up with... thanks again

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    21
    Actually there is a more efficient and short way to do this: If the numbers in response array range from 1 to 9, then :
    Code:
    for(int i=0;i<responsesize;i++)
    frequency[response[i]]++;
    Then frequency[1] represents the frequency of the number 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting data from a laptop hard drive
    By DavidP in forum Tech Board
    Replies: 6
    Last Post: 06-13-2009, 07:02 AM
  2. Detect SCSI Hard Drive Serial Number
    By mercury529 in forum Windows Programming
    Replies: 3
    Last Post: 10-17-2006, 06:23 PM
  3. Replies: 2
    Last Post: 07-06-2005, 07:11 PM
  4. Trinary Hard Drive
    By nickname_changed in forum Tech Board
    Replies: 14
    Last Post: 05-13-2005, 10:01 AM
  5. hard drive problems continually increasing
    By DavidP in forum Tech Board
    Replies: 5
    Last Post: 11-21-2002, 10:48 PM