# Foundations of Technical Programming Week 5

You may be asked to demonstrate/explain your work to the tutor, if you are absent/unavailable or fail to demonstrate properly, zero marks will be awarded.
Version 2
CRICOS: 0011D
TOID: 3059 19/03/2017 Page 1 of 5
COS10008 – Foundations of Technical Programming
Task 5.1. Describe the step-by-step execution of these set of instructions by the computer.
int k, j, count=0;
for (k=-1; k<1; k++) for (j=3; j>=1; j–)
count++;
Sequence
Action
Note
1
Declaring int variables k,j, count and set the initial value of count to zero
2
move to for loop and set the initial value of k as -1
k=-1
3
moves to the condition check k<1 Condition is true. 4 moves to the statement part of the outer for loop, which is a for loop again. Initialises the j value to 3 j=3 5 moves to the condition check j>=1
Condition is true.
6
moves to the statement part of the inner for loop, increments the count value
count=1
7
Moves to the modification of j which is j–
j=2
8
moves to the condition check j>=1
Condition is true.
9
increments the count value
count =2
10
Moves to the modification of j which is j–
j=1
11
moves to the condition check j>=1
Condition is true.
12
increments the count value
count =3
13
Moves to the modification of j which is j–
j=0
14
moves to the condition check j>=1
Condition is false.
15
come out of inner for loop and moves to the increment of k
k=0
16
.
.
.
Version 2
CRICOS: 0011D
TOID: 3059 19/03/2017 Page 2 of 5
COS10008 – Foundations of Technical Programming
Task 5.2. Write a program to print the following table of values: A A+2 A+4 A+6 3579
68 10 12
9 11 12 14 15 17
13 15 16 18 19 21
Task 5.3. Write a program that utilizes looping to print the following table of values:
N
1
2
3
4
5
6
7
8
9
10
The process of
computer applications. For example, a program that determines the winner of a sales contest would input the number of units sold by each sales person. The salesperson who sold the most units wins the contest. Write a a program that inputs a series of 10 numbers and determines and prints the largest of the numbers. [Hint: Your program should use three variables as follows]:
counter: A counter to count to10 (i.e., to keep track of how many numbers have been input and to determine when all 10 numbers have been processed)
number: The current number input to the program
largest: The largest number found so far
Modify:
Using an approach similar to the problem 1 above, find the two largest values of the 10 numbers. [Note: You may input each number only once.]
A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All of their data is transmitted as four-digit integers. They have asked you to write a program that will encrypt their data so that it may be transmitted more securely. Your program should read a four-digit integer and then call an encrypt() function passing the integer that you entered.
encrypt() function should do the following
Replace each digit by the remainder after the sum of that digit plus 7 is divided by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then return the encrypted integer.
Then write a decrypt() function to decrypts it to form the original number.
10*N 100*N 1000*N 10 100 1000 20 200 2000 30 300 3000 40 400 4000 50 500 5000 60 600 6000 70 700 7000 80 800 8000 90 900 9000 100 1000 10000
finding the largest number (i.e., the maximum of a group of numbers) is used frequently in
Version 2
CRICOS: 0011D
TOID: 3059 19/03/2017 Page 3 of 5
COS10008 – Foundations of Technical Programming
Make sure you enter only one integer, not separate four digits, eg: 2547)
Tips
1. Declare 1 bitmap variable for 1 player and 2 bitmap variable for 2 enemies.
2. Declare float variables for x position and y position of player.
3. Declare float variables for x, y position of enemy1 and enemy2 (total four variables)
start main function
1. open a graphics window titled “Simple Game” with width 800 and height 600
2. Use srand(time(NULL));
3. set the position of the player at the middle of the window (set x and y values of player accordingly) 4. Set random values for x and y position of enemy1 and enemy2
(eg: Set x value of rand()%screen_width() and y the value of rand()%screen_height())
5. Start a do …while loop …inside do … while loop
1. use process_events 2. clear screen to white
Version 2
CRICOS: 0011D
TOID: 3059 19/03/2017 Page 4 of 5
COS10008 – Foundations of Technical Programming 3. Set your bitmap player variable the value of
6. call draw_bitmap function by passing bitmap variable, x position and y position of player
7. call draw_bitmap function by passing bitmap variable, x position and y position of enemy1
8. call draw_bitmap function by passing bitmap variable, x position and y position of enemy2
9. Add integer 3 to the x position of enemy1 and 2 to the y position of enemy2
10. Write two if conditions (one for x position and one for y position) to reappear the enemies at the other end if it crosses the window boarder …refer to week 4 lecture demo-code
11. repeat the same for enemy2 (tasks mentioned in line 9 and 10)
12. Write the statements to move the player left, right, up and down (update the x, y variables of player)
13. if the player crosses the boarder play the sound effect
(play_sound_effect(“Arrow+Hit+1.wav”);)
13b. delay the game for 10 sec
eg: while(sound_effect_playing(sound_effect_named(“Arrow+Hit+1.wav”)))
{
delay(10);
}
13.c reset the position of the player to the middle
(13.b and 13.c are within if condition mentioned in line 13)
14. if a collision occurs between player and enemies do the same tasks mentioned in 13b and 13c.
eg for collision check:
bitmap_collision(bmp_player, floor(player_x), floor(player_y),
bmp_enemy1, floor(x_enemy1), floor(y_enemy1)); 15. refresh screen with FPS 60
16. close do..while loop 17. end of main function