Tally Lab
Objective
C++ and C programming. Topics include
- Compiling
C and C++ programs
- keyboard I/O:
cout/cin
(C++),
printf() and scanf() (C)
- file creation and file i/o:
ifstream
infile() and ofstream outfile() (C++),
FILE
*infile, FILE *outfile, and fopen() (C)
fscanf() and fprintf() (C)
- Constants: const int MAXVALS=100; (C++)
#define MAXVALS 100 (C)
- static array: int myarray[MAXVALS]:
C++
and
C
- dynamic arrays:
C++:
int *myarray;
cin >> numVals; myarray=new int[numVals];
- dynamic arrays:
C:
int *myarray;
scanf("%d", &numVals); myarray=(int *)malloc(sizeof(int)*numVals);
"malloc" stands for "memory allocation"
- deletion of dynamic arrays
delete myarray (C++)
free(myarray) (C)
- "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
- 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)
- Compile each program
C: gcc -o randomNumFile randomNumFile.c
C++: g++ -o randomNumFileCPP randomNumFile.cpp
- Run each program to create files of 50 or more random numbers
./randomNumFile
./randomNumFileCPP
- 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++
- Write C and C++ programs to put 1000 random numbers in an array.
- Print the array forwards and backwards.
Assignment Part 1.3 - Dynamic Arrays in C and C++
- Same as Part 2, but use dynamically created arrays
Assignment Part 1.4 - Array Algorithms in C
Using either your dynamic or static arrays (you choose) in C:
- Find the highest and lowest values
- Find the average value
- Find the standard deviation
- Sort the numbers
Assignment Part 1.5 - "Tally" array
- Write a program to recreate the above researcher's frequency list.
- Here's a trick to use:
- If the random values range from 0..15, create a count array
(count[0], count[1], ..., count[15])
- Initialize the counts to 0
- count[value]++ (or count[value] = count[value] + 1) to increment the
counts
Assignment Part 1.6 - findMax() function
- Using the "tally" array from part 1.5, write a function to find the
maximum frequency.
In tally above, findMax(tally) returns 10
- Syntax for this C function:
int findMax(int tally[]) {
. . .
return maxFreq;
}
Assignment Part 1.7 - calculateModes() function
- Using the "tally" array from part 1.5, write a calculateModes() that
returns an int array containing the mode(s) found in tally.
- The length of this mode array is equal to the number of modes.
- A mode is a value that occurs with maximal frequency. If there is more
than one such value, each is considered a mode of the data.
- In tally above, the modes are 2, 4, and 11 because they occur 10
times
and all other values occur fewer than 10 times.
(mode[0]=2, mode[1]=4, mode[2]=11)
- In writing calculateModes() you should call the function findMax()
written in 1.6
- Syntax for this C function (I think arrays are returned only as
parameters):
void calculateModes(int tally[], int modes, ...) {
. . .
// modes array calculated here
}
Assignment Part 1.8 - kthDataValue() function
- Write the function kthDataValue() that returns the kth data value
when the data values are considered in sorted order. The indexes of the
tally array
represent possible data values and each array location contains the
frequency of the value corresponding to its index.
- In the tally array above, the first ten data values are 2, the next five
data values are 3, and the next ten data values are 4.
- For this example, kthDataValue(tally, 1) returns 2,
kthDataValue(tally, 1) returns 2, kthDataValue(tally, 14) returns 3,
kthDataValue(tally, 15) returns 3, and kthDataValue(tally, 16) returns 4