CS 210 - Fundamentals of Programming I

Spring 2006 - In-class exercise for 4/06/06

15 points

Names:

This exercise should be completed in pairs. The purpose of this exercise is to work with vectors and strings to implement the Search algorithm introduced in lecture. The following is a problem statement, and an analysis and design for the complete program in file inclass23.cpp.

Problem Statement

Write a program that reads strings from a file, prints them to the screen one on a line, and searches for a target.

Main Program

Function: InteractiveOpen (for input file streams)

Function: ReadVector

Function: PrintVector

Analyses and designs for these (or similar) functions given previously or in lecture.


0. Create a new project, then download file inclass23.cpp from the course webpage under today's date to your project folder. Also download file inclass23.dat into the C:\WINDOWS folder (this is for KC-267 only, otherwise save it in the project folder).


Build and run this program using inclass23.dat for the input file. If it can't find your file, you might need to type the full name of your file including the drive and folder path, or move the file to the expected folder location.


Since the program doesn't actually search the vector, yet, it will always say the target is not in the vector.


1. (9 points) Study the program in inclass23.cpp and answer the following questions.

a.
Which variable in the main program is a vector? Write its variable declaration.







b.
What is the type of the elements of the vector?







c.
Write a C++ statement in the main program that will print out the exact number of names stored in the vector after the function ReadVector has been called.







d.
In the function PrintVector, why is the loop control variable in the for loop declared as unsigned int i rather than int i?







e.
What would happen if the input file contained 100 numbers?







f.
Give one way a string is like an array of characters. Give one way a string is different from an array of characters.







g.
Give one way a vector is like an array. Give one way a vector is different from an array.








2. (6 points) Modify this program in the following way:

a.
Add the prototype and definition for the function Search that implements the search algorithm analysis and design given in lecture.

b.
Change the main program to call Search where indicated in the design and in the comments.
Rebuild and test your program until your are satisfied it works.


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.


Converted using latex2html on Wed Apr 5 22:09:36 CDT 2006