Question 3: Error Detection and Correction [5 marks] Each of the following short C codes (or segments) contains one or more syntax and/or logic errors. Find ONE such error from each code (or segment) and correct it accordingly (e.g., by pointing out the part of the code that contains error/s and replacing it with your correct code segment). (1 mark each) 1) int main() { int distance, time, speed; speed distance / time; printf("Speed is: %d \n", speed); return 0; } 2) double height, centimetre=185; height = 1/100 * centimetre; printf("His height is: %d metre!\n", height); 3) #define GRAVITY 9.8 double speed = 0, terminalVelocity = 100.0; while (speed < terminalVelocity); { speed + GRAVITY; printf(" Speed = %d\n", speed); //height in metre
4) = {1,2,3,4,5,6,7,8,9,0}; int i=0, Array1[10], Array2 [10] Array1 = Array2; for (i=0; i<10; i++) { printf ("%d + %d = %d\n", Array¹, Array2, Array1+Array2); } 5) double calculateAverage (double first, double second) { double average = first + second / 2; return average; }
Question 3: Error Detection and Correction [5 marks] Each of the following short C codes (or segments) contains one or m
-
- Posts: 43759
- Joined: Sat Aug 07, 2021 7:38 am