As you work through these problems, remember what you've learned about problem solving in writing programs - break problems up into manageable, logically distinct “chunks”, and then use functions to implement these “chunks” as appropriate.
1. Keeping track of the last
k numbers entered with an array.
Sometimes computers are allowed to forget things. For example, when I access
my checking account online, only the last 10 cleared checks get displayed.
We'll do something along the same lines here. Write a program that gets a
number k from the user,
and then simply reads strings from the user. When the user types the
string end, the program
prints out the last k
strings entered by the user (not counting
end) and then exits. A run of this program might
look like:
Memory size? 3
Enter strings: the world is a very big place end
Last 3 words were: very big place
Hint: Work out on paper what you'd like your array to look like at each step using the above input. Then try to write code to make it happen. Also, keep in mind that you need to know how big to make the array when you allocate space for it. You'll know how much "memory" you'll need, but no idea how many words the user will enter. Don't try to simply make a huge array - a sufficiently patient user would still be able to type in enough words to overflow it!
2. Smoothing. Sometimes
experimental data is noisy, so that you have a hard time seeing
any trends. For example, plot the data in
data.txt,
and you'll see what I mean. In a situation like this, we might try to
"average out" this noise. Instead of plotting each consecutive
data value, we plot the average of each
k
consecutive data values. So, if our original data was:
-2 2 1 5 2 5
... and
k
was 2,
we'd have the "averaged" data points
0 1.5 3 3.5 3.5


Plotting these two shows what a difference the
averaging makes! Write a program that reads in a number
k from the user and then writes out the
file that results from doing averaging on the data in
data.txt
using averages of k
consecutive values (Think about your part 1 solution!). Import the original
file and the "averaged" file using
k = 20, into Excel to produce two
plots, and then see how the original plot compares with the averaged (sometimes
called “smoothed”) plot that your program produces. (Hint: use the Line chart type.)
Going Further
3. Write a program that will read a text file like "table.txt" that contains information in the form of a table and writes an output file, except that the rows and columns have been swapped! The file, as you should see, starts by telling you how many rows and columns of data there are, though note that this does not count the row and column headings. Hint: it's probably easiest if you treat each table entry, whether row/column heading or data, as a string.
Sample Run Input File Rendered Output File What is the input file: table.txt What is the output file: output.txt5 by 4 Singles Doubles Triples HRs Ames 6 2 0 1 Jones 4 1 1 0 Morris 3 0 0 4 Smith 6 4 0 0 Zoolander 0 0 0 0Ames Jones Morris Smith Zoolander
Singles 6 4 3 6 0
Doubles 2 1 0 4 0
Triples 0 1 0 0 0
HRs 1 0 4 0 0
Last modified by LT M. Johnson 10/24/2007 01:24:27 PM