Topical Information

This lab will help you practice with dynamic memory (NOT mixed with classes).

Program Information

One statistic of interest to many researchers is the median of a set of data. The median is the value in the middle. (This is NOT the average, mind you.) Being in the middle, of course implies that the data are in order (sorted). If there are an odd number of data items, the middle is simple to find. If there are an even number of data items, you are supposed to take the average of the two middle values.

You'll need to find the median of a set of data from a file. You don't know how many data are in the file, of course. You'll have to count the data, then read them in to sort, then find the median, and finally print it (the median) out.

(BTW, there are three sorts described in the online notes from 121 as well as the pseudocode for a decent bubble sort with several suggestions for efficiency tuning. You cannot use the sort function talked of in chapter 7! You also cannot use the qsort function you might find on the web...)

Do NOT use any more memory than is necessary!!

Thought Provoking Questions

  1. What (data) types of values can your program handle?

  2. How do you count the number of data items which are in the file? Once you've counted them, how do you start over and read them in?

  3. How do you allocate memory on the heap? Is it guaranteed to work?

  4. When do you allocate memory for your values array? Before or after counting the values in the file?

  5. When you are done with dynamic memory, what should you do?

  6. What's this nullptr value for, anyway?

  7. Which sorting algorithm did you use? How efficient is it? (i.e. What is it's complexity?)

  8. Does it matter if the data are sorted in ascending or descending order? Why/Why not?

  9. Do your median, sort, etc. functions need to know that your data array is dynamic?

This assignment is (Level 1.5).

Options