COS10029 - Fundamentals of Programming SWIN BUR · NE . SWINBURNE UNIVERSITY OF TECHNOLOGY Fundamentals of Programming Week 7 - Lab exercises (Submit all tasks) Task 7.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. array 2 4 6 8 10 0 1 2 3 4 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 = (float) temp1/array_size; } array [2, 4, 6, 8, 10] [-2, 3, -3, 10,10, 5] array_size 5 6 temp2 b) int ???? (int array[], int array_size) { int i=0,temp1; temp1=array[0]; for (i=1; i<array_size; i++) { if (array[i] < temp1) temp1=array[i]; } return temp1; } @Copyright: Swinburne University of Technology Week 7 Lab CRICOS: 0011D TOID: 3059 Page 1 of 10
COS10029 - Fundamentals of Programming SWIN BUR . NE . SWINBURNE UNIVERSITY OF TECHNOLOGY Hand execute the following arrays for the code given above. array [2, 4, 6, 8, 10] [-2, 3, -3, 10,10, 5] array_size 5 6 Suggest a meaningful name. Task 7.2 - Make a complete C++ program after implementing the following functions a. Write a total_array function that receives a data array and size as parameters and returns the sum of the array elements. Function: total_array Parameter: data (array) size (int) Returns: int Local variables sum (int) to find the sum i (int) for the loop counter b. Write a negative_count function that receives a data array and size as parameters and returns the number of the negative elements. Function: negative_count Parameter: data (array) size (int) Returns: int Local variables negative (int) to find the negative count i (int) for the loop counter c) Write a check_total function which receives total as the parameter and return true if the total is greater than or equal to 100, otherwise it returns false. d) Write a main function using the instructions given below Function: main @Copyright: Swinburne University of Technology Local variables Week 7 Lab CRICOS: 0011D TOID: 3059 Page 2 of 10
COS10029 - Fundamentals of Programming SWIN BUR NE SWINBURNE UNIVERSITY OF TECHNOLOGY - data_array (an array of size 10) - i (int) loop counter - key (int) - total (int) - neg (int) Steps : 1: Print the message "Enter values in the array" 2: Loop i for each index of data_array 4: read in values at the index i 5: Store in total, the total_array passing in data_array and SIZE 6: Print the value of 'total' 7: set i, the value 0 8: if (check_total) - call check_total function passing total 9: print sum is greater than 100: 11:else 12: print sum is less than 100 13: Set neg, the value returned by negative_count passing data_array and SIZE 14: Print the value of 'neg' 15: return 0 Task 7.3 Write a program to draw rectangles on a