TDT4102, Spring 2008

Exercise 3

Deadline: 08.02.2008


The objective of this exercise:

General requirements:

Recommended reading:


Note: The entire exercise can be done in a single Visual Studio project, but keeping a seperate file for each part will give you a better overview of the exercise.

Part 1: Arrays (15 pt.)

Arrays are useful for storing all kinds of data, and can be used to group similar data in a single memory area. In this first part, we will focus on implementing a list of phone numbers.

a) (7.5 %) Create a new file containing a main function. Create an array of type int called phoneNumbers. Populate the list with 5 different numbers.

b) (7.5 %) Write a function called printArray with a loop which displays all the numbers in the list you just created. Use this function in your main() function. The function should be able to print lists of arbitrary length.

Part 2: Multi-dimentional arrays (25 pt)

a) (5 %) Suppose that you have the array with phone numbers from part 1, and you want to keep a record of how many times each number is called every given day of the week. Create a 2-dimensional array where every collum represents a day, and each row a phone number. (record[1][2] would show how many times phoneNumber[1] was called on Wednesday).

b) (5 %) Write a funtion printArray which takes the two-dimensional array as a parameter, and prints it formated to the screen with each line representing each phonenumber in a list. Hint: The dimension of the array should always be [# of phone numbers]x7 (days of the week).

c) (7.5 %) Estimate how many phone calls you would get in a week (i.e. 300). Write a function randomizeList which populates your two-dimensional array with phone call records (increment a random entry for each call). Print the list again to verify that your code works.

d) (7.5 %) Write a function countCalls which takes the two-dimensional array as a parameter, and a parameter (integer) to specifiy a day. The function should return the number of total phone calls for every number the given day (a single integer).

Part 3: Mastermind (60 pt)

In this part, you will implement the simple game Mastermind. The program will generate a random code of letters (A-F). The player will have to guess what these letters are and in which order they're placed in. For each guess, the player is told how many letters are correct, and how many are placed correctly.

a) (5%) Create a new file which contains a function to initialize the game. You can choose between creating a new main function, or simply name the function playMastermind and call it from the original main function from part 1.

Hint: Define the following global integer constants: SIZE (number of code characters, value 4), LETTERS (number of letters, value 6). This will make it easier to modify the properties of the program later.

b) (10%) Write a function called randomChar which returns a random character in the range of the LETTERS constant (i.e. if LETTERS is equal to 3 you should return A, B or C).

Hint: Characters are the same as integers in C/C++, and the types can be cast directly. The code cout << (char)65; will print the letter A. 65+1 equals B etc. You should in other words find a number from 65 to 65+NUMBER-1 and return it as a char.

c) (10%) Use the function from (b) to create and fill array of random characters called code of length SIZE. This will be the original code which the player should attempt to guess.

d) (10%) Create an array called guess. This should contain the input from the player. Write a function readInput which takes an array as a parameter. The function should ask the player for SIZE characters, and put them in the list which was passed as a parameter.

Optional: Write an additional function to check if the input from the player is valid, so that it is impossible to guess characters which are not in the code set. Use this to verify the input in the readInput function each time it's called.

e) (10%) Write two functions checkCharacters and checkCharactersAndPosition. The first should count how many correct characters the player guessed, and the second should count how many correct characters are placed in the right order. Both should return the result as an integer. Extend the code from (d) so that the game continues to ask for a new code as long as checkCharactersAndPosition returns a number less than SIZE.

f) (5%) Write a function named printCode which displays an array representing a code (the original or the guessed one).

g) (10%) Finally, put all these functions together in the function from (a). When done, the program should create and store a random code, and the player should be able to guess until he finds the correct code. For each guess, the player is presented with how many correct letters and places he guessed.

Optional: Extend the game so that the player only has a limited amount of guesses.