Content text answer for exercise of test2.pdf
SEBB2033 Test 2 2023/2024-1 Answer Scheme Part A: 50 marks 1. a) float ECG_record[1000]; //float = 1 mark, [1000] = 1 mark b) int matrix [3][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}; // int = 1 mark, [3][5] = 1 marks, initialization = 1 mark c) int i=0, j = 0, sum = 0; // 1 mark for (i=0; i < 3; i++) // 1 mark { for (j=0; j < 5; j++) // 1 mark { sum = sum + matrix[i][j]; // 2 marks } } 2. struct patient_info // 1mark { char name[30]; // 1 mark int age; // 1 mark float medical_bill; // 1 mark }; // semicolon + { } = 1 mark e) struct patient_info ICU_patients[30]; // struct patient_info = 1 mark // ICU_patients[30] = 1 mark f) ICU_patients[1].medical_bill = 15000; // ICU_patients[1] = 1 mark // .medical_bill = 1 mark // = 15000, 1 mark 3. enum, typedef, union: a) Line #10 : }; Line #11 : typedef union stock item; Line #14 : union stock *ptr = &device; Line #16 : enum KPI {PASS, FAIL}; Line #22 : if (ptr->qty > 1000) status = PASS; b) 0 (not PASS, if student key in PASS, deduct half mark) c) 20 (cause the largest datatype in union stock is char model[20] which consume 20 bytes.
4. Pointer: Each correct question provides 2 marks Variable Value a) &commission 6422028 b) ptr1 6422044 c) *ptr2 800.00 d) *(price+1) 40.00 e) price+2 6422012 5. Function and variable scope: Each correct provides 2 marks Value a) 100 b) 50 c) 200 d) 200 e) 600
Part B: ANSWER ALL SUBJECTIVE QUESTIONS (50 MARKS) (a) Start Declare function, array and variables Call avgWeight () Display avg end avgWeight return Declare variables; sum, avg i < 5 ? Sum =+ arr [i] Avg = sum / 4 Return sum 2m 2m 2m 2m 2m 2m 2m 2m 2m 2m 2m
(b) C program #include /* function declaration */ float avgWeight (float arr[]); // float [2m], name[2m], argument[2m] int main () { /* an int array with 5 elements */ int i; float avg, myWeight[5]= {55.5, 65.5, 58.6, 64.3}; // correct variable declaration [3m] avg = avgWeight (myWeight) ; // correct function call pass array [2m], argument [2m] printf( "Average value is: %.2f ", avg ); // display the return value [2m] return 0; // return 0 to end [1m] } //----------------------------------------------------------------- float avgWeight (float arr[]) { // correct function definition [2m] int i; double avg; double sum = 0; for (i = 0; i < 4; ++i) { // correct looping [3m] sum += arr[i]; // correct sum[1m], array [2m] } avg = sum / 4; // correct arithmetic [2m] return avg; // correct return [2m] }