Homework 2: Stats

Educational Objectives: After successfully completing this assignment, the student should be able to accomplish the following:

Operational Objectives: Create a single file named stats.cpp that computes the mean and median of a sequence of integers received via standard input.

Deliverables: One file stats.cpp.

Background

Given a finite collection of n numbers:

  1. The mean is the sum of the numbers divided by n, and
  2. The median is the middle value (in case n is odd) or the average of the two middle values (in case n is even).

Note that to find the median of a collection of data, it is convenient to first sort the data, that is, put the data in increasing (or non-decreasing) order. Then the median is just the middle datum in the sorted sequence (or the average of the two middle data, if there are an even number).

One of the simplest sort algorithms is called Selection Sort, which operates on an array of elements and has a computation which can be described in one sentence: For each element of the array, find the smallest element with equal or higher index in the array and swap these two elements. Here is a "pseudocode" description of the algorithm:

for i in [0...n)       // for each element of array A
  k = i                // find the smallest element following it
  for j in [i+1...n)
    if A[j] < A[k]
      k = j
    endif
  endfor               // now A[k] is the smallest element following A[i]
  swap the values in A[i] and A[k]
endfor

(You could test whether A[k] < A[i] before the swap, but it is not clear this would speed up the process - swapping may be faster than testing.)

Procedural Requirements:

  1. Create and work within a separate subdirectory cop3330/hw2. Review the COP 3330 rules found in Introduction/Work Rules.

  2. Turn in one file stats.cpp using the hw2submit.sh submit script.

    Warning: Submit scripts do not work on the program and linprog servers. Use shell.cs.fsu.edu to submit projects. If you do not receive the second confirmation with the contents of your project, there has been a malfunction.

Technical Requirements and Specifications

  1. The project should compile error- and warning-free on linprog with the command g++ -ostats.x -Wall -Wextra stats.cpp.

  2. The number of integers input by the user is not known in advance, except that it will not exceed 100. Numbers are input through standard input, either from keyboard or file re-direct. The program should read numbers until a non-digit or end-of-file is encountered or 100 numbers have been read.

  3. Once the input numbers have been read, the program should calculate the mean and median and then report these values to standard output.

  4. The source code should be structured and organized as follows:

    1. Implement separate functions with the following prototypes:
      float Mean   (const int* a, size_t size); // calculates mean of data in a
      float Median (int* a, size_t size);       // calculates median of data in a
      void  Swap   (int& x, int& y);            // interchanges values of x and y
      void  Sort   (int* a, size_t size);       // sorts the data in a
      
    2. I/O is handled by function main(); no other functions should do any I/O
    3. Function main() calls Mean() and Median()
    4. Function Median() calls Sort()
    5. Function Sort() calls Swap()

  5. The Sort() function should implement the Selection Sort algorithm.

  6. When in doubt, your program should behave like the distributed executable examples in stats_i.x and stats_s.x in area51. Identical behavior is not required, but the general I/O behavior should be the same. In particular, the data input loop should not be interupted by prompts for a next datum - this will make file redirect cumbersome. Just ask for the data one time, then read until a non-digit or end of file is encountered.

Hints