ECS 40
ECS 40 Practice Final Key
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.
Pts
0 int LargestFactor(int number, int start_number) {
4 if (start_number == number) // or start_number > number / 2
4 return 1;
6 int num = LargestFactor(number, start_number + 1);
9 if( num == 1 && number % start_number == 0)
2 return start_number;
2 return num;
}
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”.
Pts: 3 1 1 2 1 2
grep -l “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?
Informs the shell that bash should be used to process the script.
b) (5 points) What does line #3 do?
Selects every file in the current directory that ends with the first command line parameter.
c) (5 points) What does line #4 do?
Changes the name of a file ending with the first parameter so that its extension is now the second command line parameter.
d) (5 points) What does line #5 do?
It increments the shell variable i.
e) (10 points) What does the whole script do?
Searches for all files in the current directory that end with the first parameter, and replaces their extension with the second
parameter. Lastly, it prints the number of files changed.
5. (20 points)
a) (10 points) Assuming I have declared “queue > Q”. Do this by filling in the values and drawing 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 Front and Back 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
37 19 3 13 22 8 NULL
FrontPtr: ____B___________ BackPtr:_____F_____ -2 points for each incorrect Node, pointer, Front, or Back.
b) (10 points) Assuming I have declared “stack > st” and that the topIndex is initially -1. Show the contents of
the stack after the following operations. (Note: there may be more positions than needed.) Also provide the final value 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
79 73 32 94 7
topIndex: ____4____________ -2 points for each incorrect value or Top.
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 and elements berth . The typedef of the map is SleeperMap. The 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.
Pts
2 #ifndef SleeperCarH
2 #define SleeperCarH
2 #include