Computer Science and Programming
Prof. Gregory Safko

Lab 3


1. Testing an integer (inttest.cpp)

Write a function testInt that accepts one integer parameter, and returns a character, 'P' if the number is positive, 'N' if it is negative, or 'Z' if it is zero.

The function prototype (above main) should look like this:

char testInt(int n);

and the function definition (below main) should look something like this:

char testInt(int n)

{

    // The function may have its own "local" variables,

    // just like variables in main.

    // The code here does stuff... (DON'T use "testInt" as a variable)

 

    return (an expression giving the result) ;

}

return is allowed more than once within a function. The first return statement that is executed will exit the function and indicate the return value to be used.
(Examples: return 'X'; or if (n < 1) return 'a';)
Also write a suitable main program to demonstrate your function.

 


2. Even/odd test with function (evenodd.cpp)

Write a function that takes a single int parameter and returns a bool value, true if the integer passed in is even, false if it is odd.

The function prototype should look like this:

bool isEven(int n);

Also write a main program that prompts for a number, and tests that number, the number plus one, and the number plus two. Your program must make use of the function you defined.

Here are two example runs, showing one possible output format:

Please enter an integer: 4 
4 is even 
5 is odd 
6 is even
Press any key to continue

Please enter an integer: 33 
33 is odd 
34 is even 
35 is odd
Press any key to continue

 


 

 

3. Divisor testing (divisor.cpp)

Write a program that prompts for an integer n, and indicates whether it is divisible from to 2 to n/2. (This is an expansion of your divtest.cpp program)

Here is one possible output format, showing two example runs:

Enter an integer: 18
18 is divisible by 2.
18 is divisible by 3.
18 is not divisible by 4.
18 is not divisible by 5.
18 is divisible by 6.
18 is not divisible by 7.
18 is not divisible by 8.
18 is divisible by 9.
 

Press any key to continue
 

 



 

 

 

Enter an integer: 11
11 is not divisible by 2.
11 is not divisible by 3.
11 is not divisible by 4.
11 is not divisible by 5.
 

Press any key to continue
 

 


Optional this week (will be required later): Notice that the number 11 
is a prime number (a number whose only divisors are itself and 1). 
Write code to check that if a number n fails divisibility up to n/2, 
then it must be a prime number. 
 


 

4. Dropping the lowest of 3 grades, and 4 grades (droplow.cpp)

Write two overloaded functions, both named dropLowest ; one will take three ints as arguments, and the other will take 4 ints as arguments. In both cases, each function returns the sum of the largest values minus the lowest as an int. For example, dropLowest(100,70,90) should return 190; dropLowest(30,-40,-40) should return -10; dropLowest(50,20,100,100) should return 250; and dropLowest(10,10,10,10) should return 30. (Notice that the order does not matter.)

The function prototype (above main) should look something like this:

int dropLowest(int a, int b, int c);

int dropLowest(int i, int j, int k, int l); 

/*(Recall that the parameters a,b,c,i,j,k,l are optional, and are for 
human-readable purposes only) */
 

HINT: You can find the sum of all three numbers, then find the smallest of the three and subtract it from the sum.

Also write a suitable main program demonstrating your function. You may have it call your function with an appropriate set of fixed test cases (dropLowest should work reasonably when some or all of its arguments (values "plugged in") are equal), or you may have it prompt for numbers from the keyboard and print the result.