Tally Lab

Objective
C++ and C programming. Topics include
  1. Compiling C and C++ programs
  2. keyboard I/O: cout/cin (C++), printf() and scanf() (C)
  3. file creation and file i/o: ifstream infile() and ofstream outfile() (C++),
    FILE *infile, FILE *outfile, and fopen() (C)
    fscanf() and fprintf() (C)
  4. Constants: const int MAXVALS=100; (C++)
    #define MAXVALS 100 (C)
  5. static array: int myarray[MAXVALS]: C++ and C
  6. dynamic arrays: C++:
    int *myarray;
    cin >> numVals; myarray=new int[numVals];
  7. dynamic arrays: C:
    int *myarray;
    scanf("%d", &numVals); myarray=(int *)malloc(sizeof(int)*numVals);
    "malloc" stands for "memory allocation"
  8. deletion of dynamic arrays
    delete myarray (C++)
    free(myarray) (C)
  9. "Random" numbers in C and C++

Background
A researcher wishes to calculate some statistical properties for a collection of
data values. The data values are represented by the array tally.
The indexes of the array represent the possible values of the actual data values
from zero to the maximal value (15 in the example below). Each array location
contains the frequency (number of occurrences) of the value corresponding to its
index. In the example below, tally[4] is 10, which means that the value 4 occurs
ten times in the collection of data; whereas tally[8] is 0, which means that
the value 8 does not occur in the data collection.

tally

Value      0   1    2   3   4   5   6   7   8   9   10   11   12   13   14    15
Frequency  0   0   10   5  10   0   7   1   0   6    0   10    3    0    0     1

Assignment Part 1.1

  1. Download these programs to write random numbers to a file: randomNumFile.c, and randomNumFile.cpp
    (right click on each, choose "Download Link", save as .c or .cpp)
  2. Compile each program
    C: gcc -o randomNumFile randomNumFile.c
    C++: g++ -o randomNumFileCPP randomNumFile.cpp
  3. Run each program to create files of 50 or more random numbers
    ./randomNumFile
    ./randomNumFileCPP
  4. Look at the contents of each of the number files to verify the programs worked.

Assignment Part 1.2 - Static and Dynamic Arrays in C and C++

Assignment Part 1.3 - Dynamic Arrays in C and C++

Assignment Part 1.4 - Array Algorithms in C

Using either your dynamic or static arrays (you choose) in C:

Assignment Part 1.5 - "Tally" array
Assignment Part 1.6 - findMax() function
Assignment Part 1.7 - calculateModes() function
Assignment Part 1.8 - kthDataValue() function