# Classwork 14: Mode

## Objectives

Practice working with arrays.

## The Assignment

Recall that the *mode* of a sequence of numbers,
{ *x*_{1}, *x*_{2}, ..., *x*_{n} },
is the value that appears the most number of times (ties are broken
arbitrarily). For example, the mode of {1, 7, 2, 2, 4, 2, 1, 3} is 2,
since the value 2 appears 3 times.

Your assignment for this classwork is to compute the mode of a sequence
of *scores* stored in a file. We will assume that the scores are
between 0 and 20 inclusive. You can start with this skeleton program:
`mode.c`. It was adapted from `input4.c` shown in class.

To determine the mode, you need to create a new array `count[]`
so that `count[i]` keeps track of the number of times that
the score *i* has appeared. Since we assume that the scores are
between 0 and 20 (inclusive), it is good to define a constant for this
value:

#define MAX_SCORE 20

To complete the program, you need to

- Declare a new
`count[]`array. - Initialize each element of
`count[]`to 0. - Iterate through the elements of the
`A[]`array and update`count[]`in each iteration. (Recall that`A[]`holds the scores.) For example, when your program sees that`A[34]`is 11, you should add 1 to`count[11]`because now you know that 11 has appeared 1 more time. - Iterate through the
`count[]`array and print out the number of times that each score appeared. - Iterate through the elements of the
`count[]`array to find the maximum countthe score with the maximum count.*and* - Print out the score that has the highest count and the number of times that score appeared.

## Notes

Things to think about:- Where do you put the
`#define`for`MAX_SCORE`? - If the largest possible score is 20, how
large do you have to make the
`count[]`array? - Once you have the
`count[]`array computed, how do you find the largest element in it? For example, it is not enough to know that the largest count so far is 5, you also need to remember which score has appeared 5 times. - Two input files have been prepared for you: cw14test1.txt and cw14test2.txt. The first one is a small file with only 10 numbers that is useful for testing while you develop your program. The second file has 100 numbers.
- Remember to use input redirection to get the input
from a file:
PT[194]% ./a.out <cw14test2.txt The average score is: 9.710000 count[0] is 8. count[1] is 4. count[2] is 5. count[3] is 4. count[4] is 5. count[5] is 5. count[6] is 4. count[7] is 4. count[8] is 6. count[9] is 2. count[10] is 7. count[11] is 1. count[12] is 3. count[13] is 5. count[14] is 12. count[15] is 4. count[16] is 4. count[17] is 5. count[18] is 7. count[19] is 3. count[20] is 2. The mode of the scores is 14. It occurred 12 times. PT[195]%

## Submit

When you are done testing your program, use the`script`command to record yourself compiling the program and running it on the two test cases. Then, submit as usual:

submit cs104_chang cw14 mode.c typescript