IC210 Lab 9 Arrays

Pre-lab homework. Turn in a pseudocode/flowchart for the program described in paragraph 1 below.  Your pseudocode/flowchart MUST describe the conceptually difficult parts of the program’s control flow logic.  Reminder: All pre-lab homework is due at the beginning of the lab period, and late pre-lab homework will earn a grade of 0.


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 #1: Your solution will clearly need an array. You will NOT need a file.

Hint #2: You need to know how big to make the array when you allocate space for it. However, at that point you will have no idea how many words the user will enter. You will, however, know how much "memory" you'll need – how can you use that information?

Hint #3: Do NOT try to solve this by simply make a huge array - a sufficiently patient user would still be able to type in enough words to overflow it!

2. Once you have part 1 working:

·       Read the problem below (#3). 

·       Think again about your solution to #1 above.  Identify a key operation that (a) you used in problem #1 above and (b) that would be a useful operation for solving #2 (though you will have to change the ‘type’ of the data that is involved). Ask your instructor if you are stuck.

·       Rewrite your solution to #1 so that it uses a new function that does this key operation (if you already used a function for this, then you already met this requirement).

·       When finished, proceed to solving #3 below, and re-use the new function that you just wrote (changing the type of the array involved as needed).

3.  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 a spreadsheet program 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

4.  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.txt
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
 

 


Christopher W Brown

Last modified by LT M. Johnson 10/24/2007 01:24:27 PM