CS31 Worksheet 3
CS31 Worksheet 3
This worksheet is entirely optional, and meant for extra practice. Some problems will be more
challenging than others and are designed to have you apply your knowledge beyond the examples
presented in lecture, discussion or projects. All exams will be done on paper, so it is in your best
interest to practice these problems by hand and not rely on a compiler.
Concepts
Switch, functions
int main() {
int a = 5, b = 10;
cout << “a: “ << a << “ b: “ << b << endl;
mystery(a, b);
cout << “a: “ << a << “ b: “ << b << endl;
return( 0 );
}
int main() {
mystery( ‘2’ );
mystery( ‘a’ );
mystery( ‘Z’ );
mystery( ‘d’ );
mystery( ‘1’ );
mystery( ‘c’ );
return( 0 );
}
3. Consider the two programs shown below. If there are no errors in the program, show what will
be printed by each of the following programs. If there are any errors in the program explain what is
wrong. If not, what will be the output be?
1) Write a function that returns whether or not two integers are palindrome number. A
palindrome number is a value that reads the same forwards and backwards. HINT: Use % and /
to break the value into its different digits. For example:
2) Write a function checkeven which accepts 3 integer parameters and prints YES if all three
numbers are even. Otherwise the function prints NO. Then write a main program with the
statements to read in 3 integers. Then call your function to determine whether the data entered
was all even.
3) Write a function that returns the cost of mailing a package, given the weight of the package in
pounds and ounces, and a cost per ounce are supplied as arguments to the function. Recall that
there are 16 ounces in a pound. Then write a main program with the statements to read in the
weight of a package (in pounds and ounces), and the cost per ounce for mailing. Then call your
function to calculate the mailing cost, and print the mailing cost.
4) Write a function that does integer division without using the division operator (/). Return -1
if second number is 0. Your main driver code should recognize that -1 and print an error
statement as shown below. Then write a main program with the statements to read in 2
integers. Then call your function.
5) Write a function that does integer multiplication without using the multiplication operator
(*). Return true if the multiplied value equals the third argument, false otherwise. Then write a
main program with the statements to read in 3 integers. Then call your function passing these
three arguments.
HINT: Perform the multiplication by repetitively using addition.