Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Question 3 (10 marks)
- 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.
- 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:
- int addOneTo(int number)
- {
- return number+1;
- }
- int subOneFrom(int number)
- {
- return number-1;
- }
- 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.
- 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.
- (2 marks) Define the function
- int add(int a, int b)
- 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.
- (2 marks) Define the function
- int subtract(int a, int b)
- 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.
- (1 mark) Define the function
- int multiply(int a, int b)
- 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.
- (1 mark) Define the function
- int quotient(int a, int b)
- (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.
- (1 mark) Define the function
- int remainder(int a, int b)
- (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.
- Assume for simplicity that all inputs are not negative, and that b is never zero.
- 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).
- Purpose: Practice with designing functions that use other functions.
- 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).
- Evaluation:
- 0 mark: Nothing submitted, or files cannot be opened.
- 1 mark: Student information provided in the program file.
- 2 marks: The add() function working correctly.
- 2 marks: The subtract() function working correctly.
- 1 mark: The multiply() function working correctly.
- 1 mark: The quotient() function working correctly.
- 1 mark: The remainder() function working correctly.
- 2 marks: A file submitted showing some testing.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement