UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2013/14 INTRODUCTORY PROGRAMMING CMP-0005B Time allowed: 2 hours Answer BOTH questions from Section A and ONE question from Section B. All questions carry equal weight. Notes are not permitted in this examination. Do not turn over until you are told to do so by the Invigilator. CMP-0005B Module Contact: Dr Graeme Richards, CMP. Copyright of the University of East Anglia Version 1
Page 2 SECTION A 1. There is no requirement to test for input ranges or carry out other validation in the following parts of this question. Comments should only be included if they are required to add clarity to the code. (a) Write a C++ code fragment to ask the user how many numbers they wish to input. Then, allow them to input the numbers and output the sum of the numbers input. (b) Write a C++ code fragment to ask a user to enter their height in metres (m) and their weight in kilograms (kg) then output their body mass index (BMI). BMI = weight/height 2. The user should then be given the opportunity to enter another body weight and height (c) Write a C++ code fragment to allow a user to enter any number of positive integers. When a negative integer is entered the code should output all of the numbers that have been entered, but in reverse order. CMP-0005B Version 1
Page 3 2. (a) The C++ program below contains a number of syntax errors. Identify and describe each syntax error. Use line numbers to identify the errors. 1. int partition(int a[], left, right, pivotindex) 2. { 3. int pivot = a[pivotindex], temp=""; 4. repeat 5. { 6. whilst (a[left] < pivot) 7. left++; 8. whilst (a[right] > pivot) 9. right--; 10. if (left and right and a[left]!= a[right]) then 11. { 12. tmp = a[left]; a[left]= a[right]; a[right]=temp; 13. } 14. else 15. { 16. return right; 17. } 18. }while(left or right); 19. return right; 20. } 21. 22. void quicksort(int a[], left, right) 23. { 24. if (left <> right) 25. { 26. int pivot = (left or right) / 2; // middle 27. int pivotnew=partition(a,left,right,pivot); 28. quickstor(a, left, pivotnew - 1); 29. quickstor(a, pivotnew + 1, right); 30. } 31. (b) Write a C++ code fragment that generates a random number between 1 and 6 inclusive. It should then output a textual representation as output. E.g. if a 1 is generated the output should be one, if a 2 is generated the output should be two. CMP-0005B Version 1 TURN OVER
Page 4 (c) What is the output of the following main function after calling the function? int func(int a, int &b, int * c) { a = b; b = *c; *c = a; return a+b+*c; } void main() { int a=1, b=2, c=3; int* cpoint=&c; int result = func(a, b, cpoint); cout<<a<<" "<<b<<" "<<c<<" "<<result<<endl; } CMP-0005B Version 1
Page 5 SECTION B 3. (a) Write a function that asks a user for the number of hours they have worked and their hourly rate of pay and then returns these values. (b) Write another function that calculates total pay as hours hourly rate and returns this value. (c) Write another function that accepts hourly rate and hours worked as arguments and outputs these values along with total pay as calculated by the function in Question 3.b. (d) Write a main function that utilises the other functions in Question 3 to ask the user for their hours worked and hourly rate and outputs theses values together with the total pay. CMP-0005B Version 1 TURN OVER
Page 6 4. (a) Write a C++ class to represent a job carried out by a removal company. The class should include only four members, driver s name, customer s name, hours spent on job and hourly rate. The class should include a single constructor for all members and get and set methods for the driver s name. There should also be a method for calculating the total cost of a job (hours spent hourly rate) and a method to output all of a job s members and the total cost. [20 marks] (b) In a main function, create an instance of a new job by asking the user for the job information. (c) In the same main function create a vector capable of storing a set of jobs and add the job from Question 4b to it. [5 marks] (d) Assume that the vector in Question 4c includes many jobs. In the same main function add code to output the job details for each of the jobs in the vector. [5 marks] END OF PAPER CMP-0005B Version 1