Thread: Code to calculate "mode"

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    10

    Code to calculate "mode"

    I'm trying to create a function to calculate the mode of 11 items entered into an array. Can anyone help me with this? Thanks!

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    I'll correct your code for you...

  3. #3
    Unregistered
    Guest
    You need to give us more information about what you want to do

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    10
    Sorry, I'm new here...

    I have to write a program that inputs 11 floating point values from the keyboard, then print our the numbers in ascending order along with the mean, median and mode of the array. I figured out everything but the mode. I attached my code.

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > Sorry, I'm new here...

    No prob - everyone was new at one point.

    but....
    > I attached my code.

    No, you didn't....

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    10
    Well, I tried to attach it...here, I'll include it instead:


    # include <stdio.h>
    # define INPUT 11

    // --- Prototypes ---
    void getData(float A[]);
    void bubblesort(float A[]);
    void printOutput(float A[]);
    void mean(float A[]);
    void median(float A[]);
    void main(void)
    {
    int stop;
    float A[INPUT];

    getData(A);
    bubblesort(A);
    printOutput(A);
    mean(A);
    median(A);

    scanf("%i", &stop);
    }

    // Function: getData
    // Precondition: None
    // Postcondition: Fills array with numbers input by user
    void getData(float A[])
    {
    int count;

    for (count = 0; count < INPUT; count++)
    {
    printf("Enter number %2i: ", count + 1);
    scanf("%f", &A[count]);
    }
    }

    // Function: bubblesort
    // Precondition: Floating point array
    // Postcondition: Array is sorted in ascending order
    void bubblesort(float A[])
    {
    int i, j, didswap;
    float temp;

    for (i = 0; i < INPUT; i++)
    {
    didswap = 0;
    for (j = 0; j < INPUT - i; j++)
    if (A[j] > A[j + 1])
    {
    temp = A[j];
    A[j] = A[j + 1];
    A[j + 1] = temp;
    didswap = 1;
    }
    if (didswap == 0)
    break;
    }
    }

    // Function: printOutput
    // Precondition: Sorted floating point array
    // Postcondition: Prints elements of sorted array
    void printOutput(float A[])
    {
    int i;

    printf("\nSorted array follows:\n");
    for (i = 0; i < INPUT; i++)
    {
    printf("%.2f ", A[i]);
    }
    printf("\n\n");
    }

    // Function: mean
    // Precondition: Sorted floating point array
    // Postcondition: Prints mean of sorted array
    void mean(float A[])
    {
    int count;
    float sum = 0;

    for (count = 1; count <= INPUT; ++count)
    { sum += A[count - 1];
    }

    printf("Array Mean : %.2f", sum /= INPUT);
    }

    // Function: median
    // Precondition: Sorted floating point array
    // Postcondition: Prints median of sorted array
    void median(float A[])
    {
    printf("\nArray Median : %.2f\n", A[5]);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM