Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 3


Homework 3

Be sure and use the C++ Programming Style Guide and follow the conventions described there. Up to 35% of the grade for this homework will be based on following these conventions and practicing good documentation.

Problem 1 : Linear Search

Write a program that

  • retrieves a C string filename containing data from the user
  • reads the file as a set of random numbers with one per line
  • terminates the file read when the file input object becomes invalid

The program should then repeatedly retrieve an input integer from the user and then use linear search to attempt to find the integer from the array of random numbers. If the number is found, the index of the location in the array should be returned. If not found, this should be reported.

Satifactory solution as stated above will earn up to 85% for this program. For full credit, the linear search should be structured as a separate function.

Problem 2 : Binary Search

As above, but this time, the algorithm should employ binary search. For this, the input data file must be sorted. Use the random data, but use the Unix sort command to sort the data into a new file.

Satifactory solution as stated above will earn up to 85% for this program. For full credit, the binary search should be structured as a separate function.

Problem 3 : Rules of Elimination

Elimination is a one-player game. The board consists of a set of 12 tiles, numbered 1 through 12. The player rolls a pair of dice and then chooses a move that removes tiles based on the numbers shown on the dice. For each roll, the player can remove either the two tiles corresponding to the numbers shown on the dice or can alternatively remove the tile corresponding to the sum of the numbers on the dice. If the player rolls doubles, the player can remove only the tile corresponding to the sum of the dice. Play continues until the player cannot make alegal move or all the tiles have been removed. The sum of the remaining tiles is the player's score. The goal is to have a low score. Below is an example game:

1
2
3
4
5
6
7
8
9
10
11
12
.
.
.
.
.
.
.
.
.
.
.
.

Dice Roll: 1 6 ; User selects individual dice

1
2
3
4
5
6
7
8
9
10
11
12
X
.
.
.
.
X
.
.
.
.
.
.

Dice Roll: 6 2 ; User selects sum

1
2
3
4
5
6
7
8
9
10
11
12
X
.
.
.
.
X
.
X
.
.
.
.

Dice Roll: 5 5; User must select sum

1
2
3
4
5
6
7
8
9
10
11
12
X
.
.
.
.
X
.
.
.
X
.
.

Dice Roll: 2 4; User selects individual dice

1
2
3
4
5
6
7
8
9
10
11
12
X
X
.
X
.
X
.
.
.
.
.
.

Dice Roll: 3 1; Player quits as there is no legal move, score is 47.

1
2
3
4
5
6
7
8
9
10
11
12
X
X
.
X
.
X
.
.
.
.
.
.

Create a program to play the game of Elimination. Repeat the following steps until the user has removed all the tiles or quit.

  • Display the board
  • Roll the dice
  • Let the user pick a move (D = individual dice, S = Sum, Q = quit)
Ensure that the user makes a legal choice, and display the final board and the sum of the remaining tiles after the user selects quit.