COS10008 – Foundations of Technical Programming

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.
©Copyright: 2016 Swinburne University of Technology Week 6 Lab
Version 2
CRICOS: 0011D TOID: 3059 30/03/2017 Page 1 of 10
COS10008 – Foundations of Technical Programming
Task 6.1 – Hand execution of arrays.
a) Demonstrate the computer execution of the following code. What does this function do? Suggest a meaningful name and replace ??? with that name.
01234
array
2
4
6
8
10
float {
???(int array[], int array_size)
int i;
int temp1=0;
float temp2 = 0;
for (i=0; i< array_size; i++) { temp1 = temp1 + array[ i ]; } return temp2 = temp1/array_size; } b) array array_size temp2 [2, 4, 6, 8, 10] 5 [-2, 3, -3, 10,10, 5] 6 int ????(int array[], int array_size) { int i=0,temp1; temp1=array[0]; for (i=1; i= xValue) && (mouse_x() <= rightValue) && (mouse_y() >= yValue) &&
(mouse_y() <= bottomValue)) 5: return true 6: else 7: return false ©Copyright: 2016 Swinburne University of Technology Week 6 Lab Version 2 CRICOS: 0011D TOID: 3059 30/03/2017 Page 7 of 10 20: 21: 22: 23: 24: start for loop with initial value of i=1; condition of i<=6; increment as i++ Set y, the value of y+35 call draw_button with ColorGrey at position x,y with width btnWidth and height btnHeight sprintf(str, "%d", i); Call draw_text with the values of str, ColorWhite, "arial.ttf", 15, x and y (refer to line 17) COS10008 – Foundations of Technical Programming Function : play -------------------------- Parameters: None Local Variables - x,y (float) set x, the value of 45; y, the value of 70 - btnWidth, btnHeight (int) set the btnWidth the value of 50; btnHeight the value of 30 - freq[7][7] (int) set all the elements to zero - str[20] (char) - i,j (int) - roll1, roll2 (int) used for rolling die 1 and 2 Steps: 1: Call srand with system time as seed – srand(time(NULL)); 2: start for loop with initial value of i=0; condition of i<36000; increment as i++ 3: set roll1, the value of 1+rand()%6 – to get a random number between 1 and 6 4: set roll2, the value of 1+rand()%6 – to get second random number between 1 and 6 5: switch (roll1) 6: case 1 update the array ++freq[1][roll2]; break; 7: case 2 update the array ++freq[2][roll2]; break; 8: case 3 update the array ++freq[3][roll2]; break; 9: case 4 update the array ++freq[4][roll2]; break; 10: case 5 update the array ++freq[5][roll2]; break; 11: case 6 update the array ++freq[6][roll2]; break; 12: end of for 13: 14: 15: 16: 17: start for loop with initial value of i=1; condition of i<=6; increment as i++ set x, the value of x+55 call draw_button with ColorGrey at position x,y with width btnWidth and height btnHeight sprintf(str, "%d", i); – this is to convert the integer to string draw_text(str, ColorWhite, "arial.ttf", 15, x,y); - Writing text in the button 18: 19: Set x, the value of 45 end of for loop 25: end of for loop ©Copyright: 2016 Swinburne University of Technology Week 6 Lab Version 2 CRICOS: 0011D TOID: 3059 30/03/2017 Page 8 of 10 COS10008 – Foundations of Technical Programming 26: Set x, the value of 45 27: Set y, the value of 105 // Starting draw of 2D array 28: start for loop with initial value of i=1; condition of i<=6; increment as i++ 29: start for loop with initial value of j=1; condition of j<=6; increment as j++ 30: Set x, the value of x+55 31: Call draw_button with ColorYellow, x, y, btnWidth and btnHeight 32: sprintf(str, "%d", freq[i][j]); 33: Call draw_text with str, ColorRed, "arial.ttf", 15, x and y 34: end of for loop 35: Set x, the value of 45 36: Set y, the value of y+35 37: end of for loop 38: if (freq[6][6]>=1000)
39: draw_button with ColorDarkGreen at location (200, 320) with width 100 and
height 30
40: Call draw_text with “Player Wins”, ColorYellow, “arial.ttf”, 15, 205 and 320
41: else
42: draw_button with ColorDarkGreen at location (200, 320) with width 100 and
height 30
Call draw_text with ” Player Loses”, ColorYellow, “arial.ttf”, 15, 205 and 320
©Copyright: 2016 Swinburne University of Technology Week 6 Lab
Version 2
CRICOS: 0011D TOID: 3059 30/03/2017 Page 9 of 10
COS10008 – Foundations of Technical Programming
After implementing the previous part extend your program so that the player wins the game only when the sum of diagonal elements are greater than 6000. You have to show a text box showing the sum as well as shown in the figure below.

Assume a[7][7] is your array
0123456 0
1 2 3 4 5 6
Submit the code and screen shot of the extended part.
Not using these red shaded arrays

发表评论

电子邮件地址不会被公开。 必填项已用*标注