Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. Question 3 (10 marks)
  2.  
  3. As we saw in lecture, all computers have an arithmetic and logic unit (ALU) to do calculations. In this question, we will pretend that our computer cannot do complicated operations like + and *, so that we can practice writing functions.
  4.  
  5. We will pretend that the computer can only do very simple arithmetic, namely, adding or subtracting 1 from an integer. We implement these two operations using functions, as follows:
  6.  
  7. int addOneTo(int number)
  8. {
  9. return number+1;
  10. }
  11. int subOneFrom(int number)
  12. {
  13. return number-1;
  14. }
  15.  
  16. These functions are defined for you in a file here. This technique is obviously not practical, but it will force us to think about "normal" arithmetic in a different way.
  17.  
  18. In the exercises below, you will be asked to implement 5 functions that do not use +,-,*,/, directly. Instead, you will use the 2 functions above, as well as any that you define yourself.
  19.  
  20. (2 marks) Define the function
  21. int add(int a, int b)
  22. using a loop, and any of the functions defined earlier in the question. It is not allowed to use +,*,-,/ directly. Assume for simplicity that all inputs are not negative.
  23. (2 marks) Define the function
  24. int subtract(int a, int b)
  25. using a loop, and any of the functions defined earlier in the question. It is not allowed to use +,*,-,/ directly.
  26. Assume for simplicity that all inputs are not negative.
  27. (1 mark) Define the function
  28. int multiply(int a, int b)
  29. using a loop, the function add(), or any of the functions defined earlier in the question. It is not allowed to use +,*,-,/ directly. Assume for simplicity that all inputs are not negative.
  30. (1 mark) Define the function
  31. int quotient(int a, int b)
  32. (the result of integer division) using a loop, the function subtract(), or any of the functions defined earlier in the question. It is not allowed to use +,*,-,/ directly. Assume for simplicity that all inputs are not negative, and that b is never zero.
  33. (1 mark) Define the function
  34. int remainder(int a, int b)
  35. (the remainder of integer division) using a loop, and any of the functions defined earlier in the question. It is not allowed to use +,*,-,/,% directly.
  36. Assume for simplicity that all inputs are not negative, and that b is never zero.
  37. Write a program that tests all your function definitions. To test your functions, you should compare the answers they give to the answers given by the normal C++ operators +,-,*,/,%. Hand in a file that shows some of the testing you did (copy-paste from your terminal window to a text editor).
  38.  
  39. Purpose: Practice with designing functions that use other functions.
  40.  
  41. What to hand in: Your program, in a file called a6q3.cpp. Your program must include 5 functions above. Be sure to include your name, NSID, student number, lecture section and tutorial section at the top of your program file. Also submit a file called a6q3_testing.txt that shows that you tested your program (copy-paste from your terminal window to a text editor).
  42.  
  43. Evaluation:
  44.  
  45. 0 mark: Nothing submitted, or files cannot be opened.
  46. 1 mark: Student information provided in the program file.
  47. 2 marks: The add() function working correctly.
  48. 2 marks: The subtract() function working correctly.
  49. 1 mark: The multiply() function working correctly.
  50. 1 mark: The quotient() function working correctly.
  51. 1 mark: The remainder() function working correctly.
  52. 2 marks: A file submitted showing some testing.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement