A3 Functions - sheerazwalid/COMP-I GitHub Wiki

Assignment folder

Place source code files for this assignment in a folder named functions within your Cloud9 workspace.

Exercise 1: Pass by value (10 points)

Implement a function that returns an integer that is one greater than the integer passed into it. The function has the following signature.

int addOne(int k);

Write test code with assertions to verify that your function is correctly implemented.

For example, the following tests that the function works correctly for several different values.

assert(addOne(-3) == -2);
assert(addOne( 0) == 1);
assert(addOne( 1) == 2);

You need to include the cassert header in your code in order to use the assert function.

#include <cassert>

Exercise 2: Pass by reference (10 points)

Implement a function that increases the value of an integer passed into it by 1. The function has the following signature.

void addOne(int & k);

Write test code with assertions to verify that you implemented the function correctly. Unlike the previous exercise, you can not call the addOne function with a constant because the function's argument is a reference.

Exercise 3: IsEven function (10 points)

Write a function named isEven that takes an integer argument n and returns a boolean value. Implement the function so that it returns true when n is even and false when n is odd.

Write test code with assertions to verify that you implemented the function correctly.

Exercise 4: Strings (10 points)

A person's initials is the first letter of their first name, followed by a period, followed by the first letter of their last name, followed by a period. For example, the initials for David Turner are D.T.

Write a program that prompts the user for a first name and then prompt the user for a last name. Have the program display the user's initials.

As part of your solution, define a function named initials, which takes 2 arguments: the first name and the last name. The function returns the person's initials as a string.

Because the function arguments don't need to be changed and are complex data types, you should pass them into the function as const references.

Hint: use the substr function or the index operator of the string data type.

Exercise 5: Random integer function (10 points)

Implement a function named randomInteger that takes as arguments a lower bound a and an upper bound b of an interval [a, b]. The function computes and returns a random integer that falls inside this interval.

Write a program that prompts the user for a lower bound and an upper bound. After the user inputs these values, the program should call your randomInteger function to get a random integer between the lower and upper bounds. The program displays this value to the user and then terminates.

Submit your work

Submit your work by sending the url of your Cloud9 workspace to the teaching assistant and CC the instructor. The subject line of your email should be 201 Assignment: Functions.

Example quiz questions

  1. Any of the above or similar problems.

  2. Implement a function that determines if an integer is lucky. The function returns true if the number passed into it is divisible by 7 and not by 13; otherwise it returns false. A declaration of the function is shown below.

bool isLucky(int n)
⚠️ **GitHub.com Fallback** ⚠️