Structured Programming in C++
Prof. Gregory Safko

Camden County College, Cherry Hill Campus

Lab 4

Assigned: March 5, 2004

Due: March 26, 2004

 


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. 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.