Exercise 5

Description

Create a program that will ask the user for n numbers then get the Median, the Mean and the Mode. Problem definition and example output can be found attached below. My solution's source code can also be found from the attachments below. Note that this version doesn't use the qsort function any more. Instead a very simple implementation of bubble sort is being used to sort out the array for the Mode operation.

Source Code

The following are the important function definitions for Median, Mean, Mode, and Sorting.

The Mean

Using the mathMean function is pretty straightforward. Its parameters are the input array and the length of the array. It returns the average value in double of all the elements in the array.

Mean

double mathMean(const double arrInput[], int inputLen) {
    double sum = 0;
    int i = 0;
    for (i=0; i<inputLen; i++) {
        sum += arrInput[i];
    }
    return sum / inputLen;
}

The Median

Median

// arrInput is not constant because we still have to 
// sort it in to a particular order.
double mathMedian(double arrInput[], int inputLen){
  // sort the array first
  helperSortBubble(arrInput, inputLen);
  // perform the operations :D
  if (inputLen % 2 == 1) {
    return (arrInput[inputLen / 2]);
  } else if (inputLen % 2 == 0) {
    return ((double)(arrInput[inputLen / 2] + arrInput[(inputLen / 2) - 1]) / 2);
  }
}
void helperSortBubble(double arrInput[], int inputLen) {
  int x, y;
  x = y = 0;
  for(x=0; x<inputLen; x++) {

for(y=0; y<inputLen-1; y++) {

if(arrInput[y]>arrInput[y+1]) {

int temp = arrInput[y+1];

arrInput[y+1] = arrInput[y];

arrInput[y] = temp;

}

}

}

}

Although the code is a little bit complicated, the usage of mathMedian is still pretty straightforward. Its parameters are the input array of doubles and the length of the input array as integer. It returns the median in double as described by the problem statement.

The trick here is to arrange the array first in a particular order, whether from highest to lowest or the opposite. Then since we know how many elements, n, there are in the array we can immediately find the median. First, determine whether there are an even or odd number of elements by using modulus. If n % 2 == 1 then it's odd, else it's even. If it's odd, then the integer part of (n/2) is the middle element.

Example: Consider the already sorted array of doubles below. The array has a length of 11 which is odd. But notice that the index are one less than the element number.

Now, consider we take n=11. Additionally n % 2 = 1, hence it's odd n / 2 = 11 / 2 = 5.5. Let i be the integer part of 5.5 which is 5. The median in this case would directly be myDoubles[i] where i = 5. Hence the final value of myMedian from the following code will be 25.6!

double myMedian = 0;
double myDoubles[10] = {20.1, 21.2, 22.3, 23.4, 24.5, 25.6, 26.7, 27.8, 28.9, 29.0, 30.1};
int myLength = 11;
myMedian = mathMedian(myDoubles, myLength); // Should be 25.6 :D

Example 2: Consider the already sorted array of doubles below. The array has a length of 12 which is even.

Now, consider we take len = 12. Additionally len % 2 = 0, hence it's even (n / 2) = (12 / 2) = 6, and keep the value in i. The median in this case would be ((myDoubles[i] + myDoubles[i-1]) / 2) where i = 6. Hence the final value of myMedian from the following code will be (26.7 + 25.6) / 2 which is 26.15!

double myMedian = 0;
double myDoubles[11] = {20.1, 21.2, 22.3, 23.4, 24.5, 25.6, 26.7, 27.8, 28.9, 29.0, 30.1};
int myLength = 12;
myMedian = mathMedian(myDoubles, myLength); // Should be 26.15 :D

This time, let's look at the helperSortBubble function and see how it works. Assuming we let len = the lenght of the array, it looping through all the elements, len of the array. And for each element, arr[i], it again, loops itnern asdfjalskdjf unya na nako tiwason!

Mode

int mathMode(const double arrInput[], double arrOutput[], int inputLen, int* outputLen) {
    double arrItem[MAXSIZE], dblMode;
    int arrSum[MAXSIZE], intModeIndex, intModeMax, integerSum, i, y;
    dblMode = intModeIndex = integerSum = i = y = intModeMax = 0;
    for (i=0; i<inputLen; i++) {
        arrItem[i] = 0;
        arrSum[i] = 0;
    }
    for (i=0; i<inputLen; i++) {
        int itemFound = 0;
        for (y=0; y<intModeIndex; y++) {
            if (arrInput[i] == arrItem[y]) {
                // printf ("debug info: input[%d]{%g} == item[%d]{%g}\n", i, arrInput[i], i, arrItem[i]);
                arrSum[y] += 1;
                arrItem[y] = arrInput[i];
                itemFound = 1;
                break;
            }
        }
        if (itemFound==0) {
            arrItem[intModeIndex] = arrInput[i];
            arrSum[intModeIndex] += 1;
            intModeIndex++;
        }
    }
    
    // determine how many is the most prevailent number
    // in the array.
    intModeMax = 0;
    for (i=0; i<inputLen; i++) {
      if (intModeMax < arrSum[i]){
          intModeMax = arrSum[i];
      }
    }
    
    // find all the numbers that have the most prevailant
    // number in the array and put them in the arrOutput.
    // and determine how many they are and put it in the
    // intNewSize output. :)
    *outputLen = 0;
    if (intModeMax > 0) {
      for (i=0; i<inputLen; i++) {
        if (intModeMax == arrSum[i]){
          // printf ("debug info: address arrOutputLen: %d inside function mode loop.\n", outputLen);
          arrOutput[*outputLen] = arrItem[i];
          // printf ("debug info: arrOutput[%d]{%g} = arrItem[%d]{%g};\n", *outputLen, arrOutput[*outputLen], i, arrItem[i]);
          (*outputLen)++;
        }
      }
    }
    
    // printf ("debug info: address arrOutputLen: %d inside function mode.\n", outputLen);
    if (*outputLen > 1 && intModeMax > 1) {
      printf ("The modes are ");
      for (i=0; i < (*outputLen) - 1; i++) {
        printf ("%g, ", arrOutput[i]);
      }
      printf ("and %g with a count of %d.\n", arrOutput[(*outputLen) - 1], intModeMax);
    } else if (*outputLen == 1 && intModeMax > 1) {
      printf ("The mode is %g.\n", arrOutput[0]);
    } else {
      printf ("There is no Mode.\n");
    }
    return intModeMax;
}

A formatted version of the complete source can also be found from PasteBin.