0% found this document useful (0 votes)
21 views5 pages

Worksheet 5

This document is a CS 31 worksheet providing optional practice problems related to coding functions and control structures in C++. It includes tasks such as implementing functions to validate ring strings, converting if-else statements to switch statements, and determining the order of characters in a string. The worksheet is designed to help students apply their knowledge beyond classroom examples.
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)
21 views5 pages

Worksheet 5

This document is a CS 31 worksheet providing optional practice problems related to coding functions and control structures in C++. It includes tasks such as implementing functions to validate ring strings, converting if-else statements to switch statements, and determining the order of characters in a string. The worksheet is designed to help students apply their knowledge beyond classroom examples.
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/ 5

CS 31 Worksheet 5

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
Midterm Coding Problems

Question 8 (1)
Please recall the functions shown below that you implemented as part of Project 3
// Project 3 Code
bool isValidRingString( string ringstring );
int totalMoveMinutes( string ringstring );
int totalExerciseMinutes( string ringstring );
int totalStandMinutes( string ringstring );
bool metMoveGoal( string ringstring, int goal );
bool metExerciseGoal( string ringstring, int goal );
bool metStandGoal( string ringstring, int goal );
Based on these functions, implement a function that accepts two string parameters and an int
parameter and returns a boolean value. Return false when either string parameter is not a valid
ring string. Return false when the int parameter is less than zero. Return true only when both
string parameters are valid ring strings and their combined number of exercise minutes is
greater than the int parameter. Return false in all other situations.
The declaration for this function will be:
bool exerciseMinutesExceedX( string s1, string s2, int x );
Here are some examples of how a main routine could test this function:
string a = "S5M5E5"; string b = "E10M10S10";
assert( exerciseMinutesExceedX( "", "", 10 ) == false );
assert( exerciseMinutesExceedX( a, a, -10 ) == false );
assert( exerciseMinutesExceedX( a, b, 12 ) == true );
assert( exerciseMinutesExceedX( b, a, 100 ) == false );
Write your exerciseMinutesExceedX function in the text box below. You do not have to write a
main routine or #include directives.
Question 8 (2)
Please recall the functions shown below that you implemented as part of Project 3
// Project 3 Code
bool isValidRingString( string ringstring );
int totalMoveMinutes( string ringstring );
int totalExerciseMinutes( string ringstring );
int totalStandMinutes( string ringstring );
bool metMoveGoal( string ringstring, int goal );
bool metExerciseGoal( string ringstring, int goal );
bool metStandGoal( string ringstring, int goal );
Based on these functions, implement a function that accepts two string parameters and an int
parameter and returns a boolean value. Return false when either string parameter is not a valid
ring string. Return false when the int parameter is less than zero. Return true only when both
string parameters are valid ring strings and their combined number of move minutes is greater
than the int parameter. Return false in all other situations.
The declaration for this function will be:
bool moveMinutesExceedX( string s1, string s2, int x );
Here are some examples of how a main routine could test this function:
string a = "S5M5E5"; string b = "E10M10S10";
assert( moveMinutesExceedX( "", "", 10 ) == false );
assert( moveMinutesExceedX( a, a, -10 ) == false );
assert( moveMinutesExceedX( a, b, 12 ) == true );
assert( moveMinutesExceedX( b, a, 100 ) == false );
Write your moveMinutesExceedX function in the text box below. You do not have to write a
main routine or #include directives.

Question 4 (1)
Convert the if-else if-else statement below so that it produces exactly the same output but uses a
switch statement without any if statements at all. (Please read the code carefully!) Write your
answer in the text box below. You do not have to write a main routine or #include directives.

int value;
cin >> value;
if ( value == 11 || value == 12 || value == 13 )
{
cout << "11, 12 or 13!" << endl;
if ( value == 11 ) cout << "11!" << endl;
}
else if (value == 10)
{ cout << "Ten!" << endl; }
else
{ cout << "Dunno!" << endl; }
Question 4 (2)
Convert the if-else if-else statement below so that it produces exactly the same output but uses a
switch statement without any if statements at all. (Please read the code carefully!) Write your
answer in the text box below. You do not have to write a main routine or #include directives.

int value;
cin >> value;
if ( value == 1 || value == 2 || value == 3 )
{
cout << "1, 2 or 3!" << endl;
if ( value == 3 ) cout << "3!" << endl;
}
else if (value == 0)
{ cout << "Zero!" << endl; }
else
{ cout << "Dunno!" << endl; }

Question 5. (1)
Convert the switch statement below so that it produces exactly the same output but does not use
a switch statement. (Please read the code carefully!) Write your answer in the text box below.
You do not have to write a main routine or #include directives.
char letter; cin >> letter;
switch (letter)
{
case '2':
case '4':
cout << "Two or Four" << endl; break;
case 'a':
case 'A':
cout << "aaas " << endl;
case '0':
cout << "Zero " << endl; break;
case '5':
cout << "Five " << endl; break;
default:
cout << "!" << endl;
}

Question 5. (2)
Convert the switch statement below so that it produces exactly the same output but does not use
a switch statement. (Please read the code carefully!) Write your answer in the text box below.
You do not have to write a main routine or #include directives.
char letter; cin >> letter;
switch (letter)
{
case 'A':
case 'B':
cout << "A or B " << endl; break;
case 'a':
case 'b':
cout << "a or b " << endl;
case '0':
cout << "Zero " << endl; break;
case '1':
cout << "One " << endl; break;
default:
cout << "!" << endl;
}

Question 9. (1)
Implement a function that accepts a string parameter and int parameter and returns a boolean.
Your function should determine if every ‘A’ comes before every ‘Z’. If this is the case, return
true; otherwise, return false. Additionally, your function should update the int parameter to
hold the total number of ‘A’ characters found in the string parameter. Write your function in the
text box on the next page. You do not have to write a main routine or #include directives.

The declaration for this function will be:


bool allAsBeforeZs( string s, int & count );
Here are some examples of how a main routine could test this function:

int i = 12;
assert( allAsBeforeZs( "fghijk", i ) == true );
// there are no As after any Zs so return true
assert( i == 0 ); // Zero A’s found
i = 12;
assert( allAsBeforeZs( "bkghjAAAAZbnhjk", i ) == true );
// there are no As after any Zs so return true
assert( i == 4 ); // Four A’s found
i = 12;
assert( allAsBeforeZs( "ZbkghjAAAAZbnhjk", i ) == false );
// there are As after Zs so return false
assert( i == 4 ); // Four A’s found
Question 9. (2)
Implement a function that accepts a string parameter and int parameter and returns a boolean.
Your function should determine if every ‘A’ comes before every ‘Z’. If this is the case, return
true; otherwise, return false. Additionally, your function should update the int parameter to
hold the total number of ‘A’ characters found in the string parameter. Write your function in the
text box on the next page. You do not have to write a main routine or #include directives.

The declaration for this function will be:


bool allAsBeforeZs( string s, int & count );
Here are some examples of how a main routine could test this function:

int i = 12;
assert( allAsBeforeZs( "fghijk", i ) == true );
// there are no As after any Zs so return true
assert( i == 0 ); // Zero A’s found
i = 12;
assert( allAsBeforeZs( "bkghjAAAAZbnhjk", i ) == true );
// there are no As after any Zs so return true
assert( i == 4 ); // Four A’s found
i = 12;
assert( allAsBeforeZs( "ZbkghjAAAAZbnhjk", i ) == false );
// there are As after Zs so return false
assert( i == 4 ); // Four A’s found

You might also like