0% found this document useful (0 votes)
10 views4 pages

CS31 Worksheet 3

This document is an optional worksheet for CS 31, designed for extra practice on concepts such as switch statements and functions. It includes code snippets for students to analyze and determine outputs, as well as programming problems that require writing functions for various tasks like checking for palindrome numbers and performing integer division without using the division operator. The worksheet encourages hands-on practice without relying on a compiler.

Uploaded by

kellyzzou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

CS31 Worksheet 3

This document is an optional worksheet for CS 31, designed for extra practice on concepts such as switch statements and functions. It includes code snippets for students to analyze and determine outputs, as well as programming problems that require writing functions for various tasks like checking for palindrome numbers and performing integer division without using the division operator. The worksheet encourages hands-on practice without relying on a compiler.

Uploaded by

kellyzzou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS 31 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

1) What does the following code snippet output?

void mystery(int a, int b) {


int count = 0;
while (count < 2) {
a = a + b/2;
b = a + 5;
cout << “a: “ << a << “ b: “ << b << endl;
count++;
}
}

int main() {
int a = 5, b = 10;
cout << “a: “ << a << “ b: “ << b << endl;
mystery(a, b);
cout << “a: “ << a << “ b: “ << b << endl;
return( 0 );
}

2. What does the following code snippet output?

int mystery(char code) {


switch(code) {
case ‘a’:
case ‘b’:
case ‘c’:
cout << “spooky”;
break;
case ‘d’:
cout << “feeling”;
break;
case ‘1’:
cout << “ ”;
break;
case ‘2’:
cout << “?”;
default:
cout << endl;
break;
}
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?

a. #include <iostream> b. #include <iostream>


using namespace std; using namespace std;
int three(int,int); int three(int,int);
int main() int main()
{ {
int a,b; int f;
a = 3; f = 1;
b = 4; int i = 1;
cout << three(a,b); while( i < 5 ) {
return( 0 ); f = three(i,f);
} cout << f << endl;
int three(int x, int y) i = i + 1;
{ }
int a; return( 0 );
a = x + y; }
return a; int three(int a, int b)
} {
int z;
z = a + a * b;
return z;
}
Programming Problems

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:

intPalindrome(62, 26) should return true


intPalindrome(154, 451) should return true
intPalindrome(25, 56) should return false

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.

integerDivide(6, 2) should return 3


integerDivide(2, 0) should return -1 and then your main program should print:
Error: Cannot divide by 0

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.

integerMultiply(6, 2, 12) should return true


integerMultiply(2, 0, 7 ) should false

You might also like