CPSC170A
Fundamentals of Computer Science II

Lab 2

Functions, Conditional statements, Iteration

Some Important Commands

grep ubuntu.com
pushd, popd Howtoforge.com

Handshakes

Details

Part 1: Write a function that prints the number of handshakes it would take for every student in a classroom to shake hands with every other student. The function should have one parameter for the number of students in the classroom.

Function header/signature:


//PRE: The number of students is n, and n > 0.
//POST: The return value is the number of handshakes among n students.
int handshakes(int n);

Example

The function call:

handshakes(4);

Should return 6.

Test your function with the following main function:


int main() {
   int num_students = 5;
   std::cout << "handshakes(" << num_students << ") = "
             << handshakes(num_students) << std::endl;
   
   return 0;
}

Your output should look like this:


 handshakes(5) = 10

By hand, compute the number of handshakes for 1, 2, 3, 4, and 5 students. Test your program on these input sizes to see if it works.

Part 2: Once you're convinced that your function works, modify the main function so that it asks the user to input the number of students, and then prints a message of the following form.

It would take 378 handshakes for all 28 students to meet each other.


Part 3: Alyx clamis that the number of handshakes required for n students to meet is
(n-1)n/2. Do you believe her?

See if the formula works for all numbers between 1 and 1 000.

Do you think the formula works for all numbers greater than 1?

If Alyx's formula is correct, which method do you think is more efficient for solving the handshake problem? State your reason.

Sum

Details

Part 1: Write a function that computes the sum of the first n positive integers i.e.
1 + 2 + ... + n

Function header/signature:


//PRE:  n is a non-negative integer.
//POST: The return value is the sum of the first n positive integers.
int sum(int n);

Example

The function call:

sum(8);

Should return 36

Test your function with the following main function:


int main() {
   int n = 8;
   std::cout << "sum(" << n << ") = "
             << sum(n) << std::endl;
   
   return 0;
}

Your output should look like this:


 sum(8) = 36

Now test your function, just like you did in the last exercise.

Part 2: Modify the main function to ask the user for a nonnegative integer. Based on the user's input your program should print a message of the following form.

The sum of the first 3 nonnegative numbers is 6.

is_prime

Part 1: Write a function that indicates whether or not a given positive integer is prime or not.

Function header/signature:


//PRE: n > 1.
//POST: Returns true if n is prime, otherwise, returns false.
bool is_prime(int n);

Example

The function call:

is_prime(50);

Would return:

0

The function call:

is_prime(47);

Would return:

1

Part 2: Let's make testing your function a little more interactive. In the main function write a program that prompts the user for an integer greater than 1. Then your program should print whether or not the input is prime. Your program should terminate only when the user enters a number less than 2. In that case, your program should print goodbye! and then terminate.

Here's a possible interaction a user can have with your program.

Enter an integer greater than one: 4
4 is not a prime number.
Enter an integer greater than one: 47
47 is a prime number.
Enter an integer greater than one: -1
Goodbye! 

Swap

Part 1: Write the code for the following function.


//PRE: a and b are initialized integer variables.
//POST: the values in a and b have been swapped.
void swap(int &a, int &b)

Part 2: Test your code to see if it works. First, print out the values of a and b, next call swap(a,b), then print out the values of a and b. If your code is correct the values of a and b should be swapped.

Part 3: In your function header change int &a to int a. What happens?
Do the same for int &b. What happens?
Finally, remove the ampersand (&) from both. What happens?