SI204 Lab 9

As you work through these problems, remember what you've learned about problem solving in writing programs - break problems up into manageable chunks!

  1. Keeping track of the last k numbers entered. 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 type in enough words to overflow it!

  2. 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 in html that renders in the browser as the input table, except that the rows and columns have been swapped! The file, as you should see, starts by tellim=ng 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 (or view table.html)
    What is the input file: table.txt
    What is the output file: table.html
    5 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       0
    
    Ames 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
  3. Smoothing. Sometimes experimental data is noisey, 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 into Excel to see how the plots compare. (Hint: use the Line chart type.)


Christopher W Brown
Last modified: Mon Oct 25 14:14:40 EDT 2004