ECS 40 Practice Final
ECS 40 Practice Final
1. (30 points) This question will ask for a question about your p7 or p8 code. If you wrote your own code, then the
answer should be easy and short.
2. (27 points) Write a recursive function, LargestFactor, which returns the largest factor of a number that is not the
number itself. The syntax is : int LargestFactor(int number, int start_number). The recursion is always initiated with
start_number set to 1. Here are some examples: LargestFactor(10, 1) is 5, LargestFactor(21, 1) is 7,
LargestFactor(50, 1) is 25, LargestFactor(77, 1) is 11, and LargestFactor(7, 1) is 1. Since a number cannot be its own
largest factor, the largest factor of a prime number is always 1. The heading is provided for you.
int LargestFactor(int number, int start_number) {
3. (10 points) Write a UNIX command line that will list the names of all the files in my current directory that use the
STL. Only those files contain the phrase “using namespace std”.
4. (30 points) Based on the following shell script, test.sh, answer the questions. Assume the following information
Line #
1 #! /bin/bash
2 i=0
3 for f in *.$1; do
4 mv $f ${f%.*}.$2
5 ((i = i + 1))
6 done
7 echo $i
a) (5 points) What does line #1 do?
b) (5 points) What does line #3 do?
c) (5 points) What does line #4 do?
d) (5 points) What does line #5 do?
e) (10 points) What does the whole script do?
5. (20 points)
a) (10 points) Assuming I have declared “queue
the pointers (or entering NULL) for each list node that exists after the operations. Assume that the nodes are
created in alphabetical order. If a node no longer exists, cross out its name, and make sure that it is not pointed to
by an existing node. (Note: there may be more list nodes represented than needed.) Also indicate the letter of the
list node that FrontPtr and BackPtr would be pointing to.
Q.push(37); Q.pop(), Q.push(19); Q.push(3); Q.push(13); Q.front(); Q.push(22); Q.push(8); Q.back();
Node A Node B Node C Node D Node E Node F
FrontPtr: _______________ BackPtr:__________________
b) (10 points) Assuming I have declared “stack
contents of the stack after the following operations. (Note: there may be more positions than needed.) Also
provide the final values of topIndex (the index).
st.push(79); st.top(); st.push(73); st.push(32); st.push(94); st.top(), st.push(-2); st.pop(); st.push(7)
0 1 2 3 4 5 6 7 8
topIndex: ________________
6. (190 points) For this section of the test you will be writing parts of a program that reads a passenger list for a train
from a file, “train.txt”, and then displays the information based on user requests. A sample file and its associated
output are provided on a separate sheet. main(), the class PassengerCar, and the header for the Train class are also
provided on a separate sheet.
a) (76 points) The SleeperCar class is derived from PassengerCar and contains a map, passengerNames, that has pairs
composed of keys passengerName
SleeperCar class also has three methods: 1) a constructor, SleeperCar(int carNumber, int capac, int reserv); 2) void
addName(string name, short berth = 0); and 3) void print(). Wirte a complete SleeperCar.h that provides all
appropriate preprocessor directives and implementation code for the SleeperCar class so that there is no need for a
SleeperCar.cpp file.
b) (10 points) Provide the implementation code for the Train class print() method.
c) (59 points) Provide the implementation code for the Train class >> operator. Note that you may not add any new
methods to either PassengerCar, or SleeperCar.
d) (20 points) Provide the implementation code for the Train class += operator. Note that the added passengers must
be added to a Normal car that has room available to seat all of them.
e) (25 points) If the array of PassengerCar pointers, cars, in the Train class was replaced with a TrainListNode *head,
provide the implementation code for the Train class addSleeper()method. Note that the added passenger must be
added to a Sleeper car that has a berth available for the passenger. You may assume that somewhere on the train
there is a Sleeper berth available for the passenger. TrainListNode has the following definition.
class TrainListNode
{
PassengerCar *info;
TrainListNode *next;
public:
TrainListNode(PassengerCar &in, TrainListNode *n);
friend class Train;
};
6. (190 points) There are two formats to the lines in train.txt. For every car there is a line describing it: type
description line is followed by a list of passengers and their assigned berths (beds): 1) berth
but the list may be in any order in the file.
% cat train.txt
1 8364 32 29
2 7920 12 5
4 Nixon, Richard
2 Carter, Jimmy
3 Carter, Rosalyn
1 Reagan, Ronald
5 Carter, Amy
1 4329 36 20
[davis@pc31 finalA]$ a.out
Your choice (0 = Done, 1 = Add Normal Passengers, 2 = Add Sleepers): 1
Number of passengers: 1
Your choice (0 = Done, 1 = Add Normal Passengers, 2 = Add Sleepers): 2
Number of passengers: 2
Name: Clinton, William
Name: Bush, George
Your choice (0 = Done, 1 = Add Normal Passengers, 2 = Add Sleepers): 1
Number of passengers: 4
Your choice (0 = Done, 1 = Add Normal Passengers, 2 = Add Sleepers): 0
8364 32 30
7920 12 7
Bush, George 7
Carter, Amy 5
Carter, Jimmy 2
Carter, Rosalyn 3
Clinton, William 6
Nixon, Richard 4
Reagan, Ronald 1
0123456789012345678901234567890 // Added by Sean for formatting purposes.
4329 36 24
[davis@pc31 finalA]$
int main()
{
Train train;
int choice, number;
string name, dummy;
ifstream inf(“train.txt”);
inf >> train;
do {
cout << "Your choice (0 = Done, 1 = Add Normal Passengers, 2 = Add Sleepers): ";
cin >> choice;
if(choice)
{
cout << "Number of passengers: ";
cin >> number;
getline(cin, dummy); // eats up ‘\n’
if(choice == 1)
train += number;
else // choice == 2
for(int i = 0; i < number; i++)
{
cout << "Name: ";
getline(cin, name);
train.addSleeper(name);
} // for each passenger
} // if choice
} while(choice);
train.print();
return 0;
}
In PassengerCar.h you would find:
class PassengerCar
{
protected:
short type; // 1 = Normal, 2 = Sleeper
int carNumber;
int capacity;
int reserved;
public:
PassengerCar(short ty, int number, int cap, int res):type(ty),
carNumber(number), capacity(cap), reserved(res){}
virtual ~PassengerCar(){}
virtual void print()
{cout << carNumber << " " << capacity << " " << reserved << endl;}
short getType()const {return type;}
int getAvailable()const {return capacity - reserved;}
void addPassengers(int n){reserved += n;}
};
In Train.h you would find:
class Train
{
PassengerCar *cars[10];
int count;
public:
Train():count(0){};
void operator+= (int number);
void addSleeper (string &name);
friend istream& operator>> (istream &is, Train &train);
void print();
};