Thread: Working out the mode of a set of values

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    6

    Working out the mode of a set of values

    Can anyone please give me any assistance in how to work out the mode of a set of values entered by the user.

    Many Thanks


  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    6
    Mode as in the value that occurs most often in a set of data.

    i.e. 1,2,3,4,5,6,2,1,1, Mode = 1

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    6
    many Thanks

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    6
    Thanks loads for your help !

    Here is my program, can someone please put the relevant coding (to work out the mode) into the correct place on my program for me.

    Sorry if this is being cheeky !

    #include <iostream.h>
    #include <conio.h>
    #include <dos.h>
    #include <math.h>
    #include <iomanip.h>
    #include <stdio.h>

    //Function declarations
    void BubbleSort (float[], int); // Sorts the array.
    void goswap (float&, float&); // Swaps two adjacent numbers in the array.

    //Main Program
    void main ()
    {
    const int ArrayS = 50; // Determines the array size, i.e. to 50.
    int i; // A loop control variable.
    int count;
    float max, min; // Variables which hold results obtained from the
    float range, mean, median; // Entered values
    int MedianArray; // Variable to assist in the calculation of the median.
    float sum; // Sum of the data.
    float Stat[ArrayS]; // The array that the numbers are entered into.
    clrscr();
    do
    {
    cout <<"Enter how many values you would like to use and press Return (Max of " << ArrayS << "): ";
    cin >> count;
    if (count<=ArrayS&&count>0)
    break;
    cout << "The amount of values must be less than " << ArrayS << " numbers." << endl << endl;
    } while (count>=ArrayS||count<=0);
    for (i=0; i<count; i++)
    {
    cout <<" Enter value number " << (i+1) << " :- ";
    cin >> Stat[i];
    }
    BubbleSort (Stat, count);
    max = Stat[count-1];
    min = Stat[0];
    range = max - min;
    sum = 0; // initialise sum
    for (i=0; i<count; i++)
    {
    sum+= Stat[i];
    }
    mean = sum/count; // calculates the mean value
    if (fmod(count, 2) == 1)
    {
    MedianArray = (count-1)/2;
    median = (Stat[MedianArray]);
    }
    else
    {
    MedianArray = count/2;
    median = ((Stat[MedianArray] + Stat[MedianArray-1])/2); // calculates the median value
    }
    cout << endl;
    cout << "********************************************* * " << endl;
    cout << " YOU ENTERED " << count << " VALUES." << endl;
    cout << " " << endl;
    cout << " The minimum value was: " << min << endl;
    cout << " The maximum value was: " << max << endl;
    cout << " The mean value was: " << mean << endl;
    cout << " The median value was: " << median << endl;
    cout << " The range of the values was: " << range << endl;
    getchar();
    return;
    }

    // Sorting Functions
    void BubbleSort (float array_to_sort[], int size_of_array)
    {
    enum boolean {FALSE, TRUE} sorted;
    do
    {
    sorted = TRUE;
    for (int i=0; i<size_of_array-1; i++)
    {
    if (array_to_sort[i] > array_to_sort[i+1])
    {
    goswap(array_to_sort[i], array_to_sort[i+1]);
    sorted = FALSE;
    }
    }
    } while (sorted==FALSE);
    return;
    }// End Function BubbleSort.

    void goswap (float& a, float& b)
    {
    float temp;
    temp = a;
    a = b;
    b = temp;
    return;
    }

    If you copy and paste this coding into a work document or a C++ blank template you will see the correct layout !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM