CS
210 – Fundamentals of Programming I
Spring 2007 – In-class
Exercise for 2/22/07 & 2/26/07
Name(s):
(15 points) Complete this exercise in pairs. Write the answers to the written part in this assignment sheet and turn it in with a print out the program when you are done. The purpose of this exercise is to continue to work with arrays, work with file streams, and to implement the Selection Sort algorithm introduced in lecture. The following is a problem statement, and an analysis and design for the complete program in file inclass13.cpp. Note that the function InteractiveOpen is overloaded to open both input file streams and output file streams.
Problem Statement
Write a program that reads real numbers from a file, prints them to the screen five on a line, sorts the array, prints them to the screen again, and writes the numbers back out to a file.
Analysis & Design of Main Program
Analysis - what data is constant, input, computed, or output?
|
Objects |
Type |
Kind |
Name |
|
Capacity of array (40) |
int |
constant |
CAPACITY |
|
Maximum characters in file name (80) |
int |
constant |
MAX_FILE_NAME |
|
Array of real numbers |
double [ ] |
variable |
numbers |
|
Number of elements stored in array |
int |
variable |
numElements |
Design - what are the steps to solve this problem?
Display what program does
Read numbers into the array using ReadArray (numbers, numElements, CAPACITY)
Display numbers to screen using PrintArray (numbers, numElements)
Sort numbers using SortArray(numbers, numElements)
Display numbers to screen using PrintArray(numbers, numElements)
Write numbers to file using WriteArray(numbers, numElements)
Analysis & Design
of Function InteractiveOpen (for input file streams)
Analysis &
Design of Function InteractiveOpen (for output file streams)
Analysis
& Design of Function ReadArray
Analysis & Design of
Function PrintArray
Analysis & Design of Function
SortArray
Analysis & Design of Function WriteArray
Analyses and designs for these (or similar) functions given previously or in lecture.
Assignment
0. Create a new project, the download file inclass13.cpp from the course webpage under today's date and add it to your project. Build and run this program. When it asks for a file name, enter any non-existent file name. You should get an error message saying that it can't open the file.
1. Create a sample input data file. Do this by choosing File, then New, then File. For Categories, choose General, then in Templates, choose Text File. Click on Open. This will give you a text file window.
This program expects a list of real numbers with the end of file marking the end of the list. For example, if you want to process 10 numbers, your data file might look like:
84.5 68.7 92 90.3 73.9 81.1 67 93.4 70.2 98
Type in the data, then use File, then Save As, and save the file in a folder whose name does not have any spaces (e.g., I:\cs210 on your network drive) with a name like inclass13.dat.
Run the program again using the name of your file.
When the program asks for an output file, type something like I:\cs210\inclass13.out (or wherever you put your input file). Since we aren't doing anything to the data, the resulting file should look just like the input file. You can view inclass13.out by opening it like any other text file.
2. (6 points) Study the program in inclass13.cpp and answer the following questions.
a. What is the type of the elements of the array numbers?
b. Write a C++ statement in the main program that will print out the exact number of scores stored in the array numbers after the function ReadArray has been called.
c. The argument theArray in the function header for ReadArray does not have an & even though we are using it as a passed back object. Why?
d. Why is the argument theArray declared to be const in the function header of PrintArray, but not in the function header of ReadArray?
e. In the function PrintArray, why is the loop condition in the for loop i < numElements rather than i <= numElements?
f. What would happen if the input file contained 100 numbers?
2. (9 points) Modify the program in the following way.
a. Add prototypes and definitions for the functions SortArray and FindIndexOfSmallest that implement the Selection Sort algorithm analysis and design given in lecture.
b. In the main program, after the comment about sorting the array, add code to call function SortArray.
Build and test your program until you are confident that is computes the correct result.
c. Suppose SortArray is called with the array pictured below. Show the contents of the array at the end of each loop iteration in the function SortArray. Circle the minimum element and draw arrows indicating which two elements are exchanged, if any, during each iteration of the loop.

Note you can check this by putting calls to PrintArray at the end of every loop iteration.
When you have completed this exercise, print out your program file and turn it in with one copy of this exercise sheet with your answers to the questions.
02/21/07