CSCI 116 Spring 2008 Exam #2

Instructions:  Write your answers in the space provided.  If you need more room, use the back of one of the other sheets of the test, and indicate where I can find your work.  For partial credit, be sure to show your work!  This test is closed book, closed notes, closed neighbor, open mind.  Any calculations are simple enough that you don’t need to use a calculator.  Read through the entire test before starting to answer any of the questions so that you can answer the easiest questions first and maximize your score.  Not counting the bonus question, there are 100 points on this test.  You have 50 minutes to complete the test.  Good luck, and have fun!

1.     Short Answer (4 each)

a.     Specify the prototype for a function named red that takes an integer and a character as parameters (in that order, passed by value), and returns a double precision floating point number.

 

 

 

 

 

 

b.    What include file must be included when doing file input and output in C++?

 

 

 

c.     Write C++ code to declare fout to be an output stream.

 

 

 

d.    Write C++ code to associate the output stream fout (from above) with a file named test.data.

 

 

 

e.     Write C++ code to write the value stored in num to the file test.data (from above).

 

 

 

 

f.     Write C++ code to close the file test.data being written to above.

 

 

 

g.    Why do we close a file after writing to it?

 

 

 

 

 

h.     Write C++ code to declare an array of 20 integers named foo.

 

 

 

i.      Write C++ code to assign the value 7 to the third element of the array foo.

 

 

 

j.      Write C++ code to compute the average of the first 10 integers stored in foo, and to store that average in a variable named avg.

 

 

 

k.     Write C++ code to read a value from the keyboard into the 7th element of the array foo.

 

 

 

 

l.      Write C++ code to write a value to the screen from the 13th element of the array foo.

 

 

 

 

 

m.   Write C++ code to declare an array of 25 characters (not counting the null) named bar.

 

 

 

 

n.     Write C++ code to read a text from the keyboard into the cstring bar.

 

 

 

 

o.    Write C++ code to read an entire line of up to 25 characters (including spaces) into the cstring bar.

 

 

 

 

 

p.    Write C++ code to copy the cstring in bar into the cstring baz.

 

 

 

 

 

q.    Write C++ code to store the length of the cstring baz in the variable len.

 

 

 

 

r.      What include file must be included to be able to use the functions strdup, strtok, and strstr?

 

 

 

 

s.     What mechanism is used to pass an array to a function?  Why?

 

 

 

 

 

t.      How do we designate the end of a cstring?

 

 

 

 

 

 

 

 

 

 

 

2.     Longer Answer (5 each)

a.     Write a function called length that takes a cstring as a parameter and returns an integer that specifies the number of characters (not counting the null) in the cstring.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b.    Fill in the columns in the table below to show how an array being sorted by a bubblesort would look at the end of each pass.  The initial array is shown in the first column.

1

 

 

 

 

 

 

 

8

 

 

 

 

 

 

 

6

 

 

 

 

 

 

 

4

 

 

 

 

 

 

 

9

 

 

 

 

 

 

 

3

 

 

 

 

 

 

 

7

 

 

 

 

 

 

 

2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

c.     For an array containing 1, 3, 7, 13, 19, 22, 31, 46, 49, 65, 67, 72, 78, 83, 88, 89, 93, 96 in that order, indicate the values that are compared with when doing a binary search to find the value 89.

 

 

 

 

 

d.    Write a function called copy that takes two cstrings as parameters and copies the second into the first.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.     Bonus Questions

a.     Sudoku (5 points)

Here's a Sudoku challenge with a different spin. Instead of using numbers, the puzzle is made with letters! Can you solve the puzzle so that each of the nine letters are present in each 9x9 grid only once, and also each letter only appears once in each column and row?

http://www.puzzlersparadise.com/puzzles/rj_sudoku.jpg

(from PuzzlersParadise.com)

b.    Logic Puzzle

1.             (1 point) A man lives on the tenth floor of a building. Every day he takes the elevator to go down to the ground floor to go to work or to go shopping. When he returns he takes the elevator to the seventh floor and walks up the stairs to reach his apartment on the tenth floor. He hates walking so why does he do it?

 

 

 

 

 

2.             (1 point) A blind beggar had a brother who died. What relation was the blind beggar to the brother who died? (Brother is not the answer).

 

 

 

 

3.             (1 point) A man walks into a bar and asks the barman for a glass of water. The barman pulls out a gun and points it at the man. The man says 'Thank you' and walks out. Explain.

 

 

 

 

4.             (2 points) You are in a game of Russian roulette, but this time the gun (a 6 shooter revolver) has three bullets in_a_row in three of the chambers. The barrel is spun only once. Each player then points the gun at his (her) head and pulls the trigger. If he (she) is still alive, the gun is passed to the other player who then points it at his (her) own head and pulls the trigger. The game stops when one player dies.  Now to the point: would you rather be first or second to shoot?