Lab 3. Functions

How to submit your code

Each program (the source code .cpp file) should be submitted through Blackboard (Course Materials > Lab).

You can submit all your programs at the end of the lab session in one submission. This way, we can hopefully avoid the situation when you are quickly writing your program, immediately uploading it to Blackboard, but then, say 10 minutes later, realizing that there is a bug in it.

Basically, submit when you are sure that it will be your final version.

Each program should start with a comment that contains your name and a short program description, for example:

/*
  Author: Your Name 
  Description: Lab 1. Task 1. Hello world
*/

You can submit incomplete programs, but their “incomplete” status must be clearly mentioned in the comment to the program. In this case, also briefly describe what is implemented, and what is not.

Lab 3. Functions

Task 1. Is even?

Write a function int is_even(int n) that returns true when the argument is an even integer.

Please use the following program as a starting point. It already implements a testing code for the function:

task1.cpp

Task 2. The number of digits

Write a function int num_of_digits(int n) that computes the number of decimal digits in the argument. It should work for any integer (positive, zero, and negative).

Please use the following program as a starting point. It already implements a testing code for the function:

task2.cpp

Task 3. Is the first digit equal to the last?

The goal of this task is to write a function that checks whether or not the first and the last digits of its argument are equal.

For this task, we assume that the input is a non-negative integer number.

Please write three functions:

  1. int last(int n) returns the last digit of the argument.
  2. int first(int n) returns the first digit of the argument.
  3. the third function bool first_equals_last(int n) returns true if the first and the last digits of the argument are equal to each other, otherwise returns false.

Please use the following program as a starting point. It already implements a testing code for the function:

task3.cpp

Task 4. Bonus (Extra credit).

You’ve got the source code of a vending machine.

machine.cpp

The owner of the machine is very angry, and is not really able to describe the problem in detail. It seems that something is wrong with the change dispenser code.

Vending machine

Your task is to test the code, and find why the change dispenser does not work properly. Please briefly report your findings (in the comment at the beginning of the source code file) and try to fix the problem.

(Hint: the function void dispense_change(int) seems to be the most likely source of the problem)

About the machine

Machine’s interface is emulated using the standard input and the standard output.

Input character n means that a customer inserted a nickel, d means a dime, and q means a quarter. Similarly, input 1, 2, and 3 denote the buttons for choosing one of the three products that are sold by the machine.

To stop the program, press Ctrl+D in the terminal (it means the end of file). Ctrl+C is another way to stop it.