COMP1921 16s2

Final Exam Part I Instructions

The Final Examination consists of two parts: Part I Written and Part II Practical. The maximum time allowed for both parts combined is 3 hours. The following instructions and questions form the Written part.

You are NOT permitted to log into the lab computers until you have completed and handed in Written Part I to the exam invigilator.

Final Exam Part II Instructions

Prac Question 1 (14 marks)

Write a program called swapTree.c that: Examples of program execution are:

prompt$ ./swapTree 2 1 3
	1
2
	3
Swapped tree:
        3
2
        1

prompt$ ./swapTree 8 4 6 2 7 5 3 1 12 10 9 14 13 11 15
			1
		2
			3
	4
			5
		6
			7
8
			9
		10
			11
	12
			13
		14
			15
Swapped tree:
			15
		14
			13
	12
			11
		10
			9
8
			7
		6
			5
	4
			3
		2
			1

prompt$ ./swapTree 6 5 4 3 2 1
					1
				2
			3
		4
	5
6
Swapped tree:
6
	5
		4
			3
				2
					1

prompt$ ./swapTree 7
7
Swapped tree:
7

If there are no arguments on the command line then the program generates a usage error message, as shown below:

prompt$ ./swapTree
Usage: ./swapTree integers ...

If there is a non-numeric argument on the command line, a usage error message is also printed.

prompt$ ./swapTree 3 10 a
Usage: ./swapTree integers ...

NOTE: You must ensure that your program prints the output in the format shown in the above test cases.

Prac Question 2 (15 marks)

Write a program simulate.c that simulates the following roulette betting strategy, starting with a given initial balance ≥ $1:

  1. Let n = $1
  2. Place n on "Odd"
  3. If the round is won, then n will be added to the balance; and the next bet is n = $1
  4. If the round is lost, then n will be subtracted from the balance; and the next bet is n = min(2 * n, balance)
  5. Repeat steps 2-4 until either you have doubled the initial amount (i.e. balance is twice the initial balance), or balance is $0

Your program should use a random number generator to simulate the result of spinning the wheel in each round, which is a number between 0 and 36 (including both 0 and 36). A round is won if this number is odd. A round is lost if the number is even (including zero). Your program should output the bet, the winning number and the new balance after each round (NOTE: Your program must adhere exactly to the format shown below.) Your program should accept two integer command line arguments:

Examples of program executions are:

prompt$ ./simulate 3 4567
Bet: $1. Winning number: 29. New balance: $4
Bet: $1. Winning number: 0. New balance: $3
Bet: $2. Winning number: 25. New balance: $5
Bet: $1. Winning number: 31. New balance: $6

prompt$ ./simulate 3 2234
Bet: $1. Winning number: 16. New balance: $2
Bet: $2. Winning number: 1. New balance: $4
Bet: $1. Winning number: 9. New balance: $5
Bet: $1. Winning number: 0. New balance: $4
Bet: $2. Winning number: 10. New balance: $2
Bet: $2. Winning number: 26. New balance: $0

prompt$ ./simulate 100 16
Bet: $1. Winning number: 0. New balance: $99
Bet: $2. Winning number: 2. New balance: $97
Bet: $4. Winning number: 18. New balance: $93
Bet: $8. Winning number: 2. New balance: $85
Bet: $16. Winning number: 14. New balance: $69
Bet: $32. Winning number: 6. New balance: $37
Bet: $37. Winning number: 8. New balance: $0

If there are not exactly two arguments or one of them is not a positive integer, then a usage error message should be generated:

prompt$ ./simulate 100
Usage: ./simulate initialbalance randomseed

prompt$ ./simulate 100 16 1
Usage: ./simulate initialbalance randomseed

prompt$ ./simulate 100 x
Usage: ./simulate initialbalance randomseed

Note: