BEng (Hons) Electronic Engineering Cohort: BEE/10B/FT Resit Examinations for 2016-2017 / Semester 1 MODULE: Programming for Engineers MODULE CODE: PROG1114 Duration: 3 Hours Instructions to Candidates: 1. Answer all questions. 2. Questions may be answered in any order but your answers must show the question number clearly. 3. Always start a new question on a fresh page. 4. All questions do not carry equal marks. 5. Total marks 100. This question paper contains 4 questions and 6 pages. Page 1 of 6
QUESTION 1: (25 MARKS) ANSWER ALL QUESTIONS (a) The following code segments are erroneous. Correct the error(s) in each segment of code below. i) double mainint = 3000.0; double int = 1.016 * mainint; cout<< setprecision(2) << fixed; cout<< "The calculated amount is" << int; ii) int t; cout << "Enter a number: "; cin << T; cout << "Your number is " << t; iii) iv) x = 1; while( x <= 10 ); x++; string* sub1; string* sub2; *sub1 = Programming ; sub1 = sub2; v) int* x= &10; (10 marks) Page 2 of 6
Write a block of C++ code that prints out the odd numbers 1 through 999. You do not need to write a full program. Explain the difference between the two expressions below. (3 marks) x = 2 and x == 2 (d) (e) (2 marks) What does the function fixed in the <iomanip> library do? Give an example to support your answer. (2 marks) Highlight the difference between compile errors and runtime errors. Give a specific example of each type of error. (f) Rewrite the following for loop as a do-while loop. (4.5 marks) for (int x=0; x<100; x++) cout << x << " "; (3.5 marks) QUESTION 2: (25 MARKS) (a) Write a function, using switch statement, that converts a char letter grade into its numerical equivalent as per grading system below: A=25.0 B=20.0 C=15.0 D=10.0 F=0.0 The program should use the following header line double grade2num (char grade) The program should return the value 0.0 if it receives a character besides A, B, C, D and F. Page 3 of 6
The binary search uses the divide and conquer strategy. It repeatedly divides the array into two pieces and then searches the piece that could contain the target value. Write a recursive binarysearch function to perform the binary search of an array. The function should receive an integer array, the starting subscript, ending subscript and search key as arguments. If the search key is found, the function should return the array subscript; otherwise, it should return -1. Write the output of the following program. (10 marks) void something (int* p, int* &q) { *p = 5; p += 3; *p += *q; cout << *p << << *q << \n ; q--; return; int main() { int a[4] = {10,20,30,40; int* b = a; int* c = &a[2]; something (b, c); cout << *b << << *c; return 0; (d) Write a program that reads all data from the file experiment.txt, containing integers, and display them on the screen. (7 marks) QUESTION 3: (25 MARKS) (a) Give two advantages of using functions to modularise a program? (2 marks) Page 4 of 6
Convert the following if/else sequence to a switch statement. int x; cin >> x; if (x == 2) cout << "x=2"; else if (x==3 x==5) { cout << "x=3 or 5"; x++; else if (x==4) cout << "x=4"; else cout << "Else."; Briefly explain the difference between passing parameters by value and passing parameters by reference. (d) Write the output of the following program. Assume that p and x are allocated addresses 06FD4 and 06FD8, respectively. #include <iostream> using namespace std; int main () { int *p; int x=37; cout<< Line 1: x = <<x<<endl; p=&x; cout<< Line 3: *p= <<*p<<, x = <<x<<endl; *p=58; cout<< Line 5: *p= <<*p<<, x = <<x<<endl; cout<< Line 6: Address of p = <<&p<<endl; cout<< Line 7: Value of p = <<p<<endl; cout<< Line 8: Value of the location pointed to by p = <<*p<<endl; cout<< Line 9: Address of x = <<&x<<endl; cout<< Line 10: Value of x = <<x<<endl; return 0; (10 marks) Page 5 of 6
(e) With appropriate example distinguish between static binding and dynamic binding? (5 marks) QUESTION 4: (25 MARKS) (a) Write detailed notes on Classes and Objects. Your answer should include the following: (i) Class and object definitions, (ii) Class declaration, (iii) Levels of restriction, (iv) Member variables and functions, (9 marks) i) What is a queue? (2 marks) ii) Write the class interface for a node and for a queue using the predefined node in C++. A linked list may be represented as follows: (8 marks) The list starts with a pointer Head, which points to the first node and the next field of each node points to the next node. Diagrammatically show the steps i) to add a node at the beginning of the list. ii) to add a node at the end of the list. iii) to add a node in between two nodes. (6 marks) ***END OF QUESTION PAPER*** Page 6 of 6