C++ Challenge Problems Chapter 4
Unlock all answers in this set
Unlock answersquestion
Write an expression that will print \"Even\" if the value of userNum is an even number
answer
#include using namespace std; int main() { int userNum = 0; userNum = 6; if (userNum%2 == 0) { cout << \"Even\" << endl; } else { cout << \"Odd\" << endl; } return 0; }
question
Re type the following code and fix any errors. The code should check if userNum is 2. if (userNum = 2) { cout << \"Num is two\" << endl; } else { cout << \"Num is not two\" << endl; }
answer
#include using namespace std; int main() { int userNum = 0; userNum = 2; if (userNum == 2) { cout << \"Num is two\" << endl; } else { cout << \"Num is not two\" << endl; } return 0; }
question
Re type the code and fix any errors. The code should convert negative numbers to 0. if (userNum >= 0) cout << \"Non-negative\" << endl; else cout << \"Negative; converting to 0\" << endl; userNum = 0; cout << \"Final: \" << userNum << endl;
answer
#include using namespace std; int main() { int userNum = 0; if (userNum >= 0) { cout << \"Non-negative\" << endl; } else { cout << \"Negative; converting to 0\" << endl; userNum = 0; } cout << \"Final: \" << userNum << endl; return 0; }
question
Write an if-else statement with multiple branches. If givenYear is 2100 or greater, print \"Distant future\" (without quotes). Else, if givenYear is 2000 or greater (2000-2099), print \"21st century\". Else, if givenYear is 1900 or greater (1900-1999), print \"20th century\". Else (1899 or earlier), print \"Long ago\". Do NOT end with newline.
answer
#include using namespace std; int main() { int givenYear = 0; givenYear = 1776; if (givenYear >= 2100) { cout <= 2000) { cout <= 1900) { cout << \"20th century\"; } else { cout << \"Long ago\"; } return 0; }
question
Write multiple if statements. If carYear is 1969 or earlier, print \"Probably has few safety features.\" If 1970 or higher, print \"Probably has seat belts.\" If 1990 or higher, print \"Probably has anti-lock brakes.\" If 2000 or higher, print \"Probably has air bags.\" End each phrase with period and newline. Ex: carYear = 1995 prints: Probably has seat belts. Probably has anti-lock brakes.
answer
#include using namespace std; int main() { int carYear = 0; carYear = 1940; if (carYear <= 1969) { cout << \"Probably has few safety features.\" <= 1970) { cout << \"Probably has seat belts.\" <= 1990) { cout << \"Probably has anti-lock brakes.\" <= 2000) { cout << \"Probably has air bags.\" << endl; } return 0; }
question
Write an expression that prints \"Special number\" if specialNum is -99, 0, or 44.
answer
#include using namespace std; int main() { int specialNum = 0; specialNum = 17; if ((specialNum == -99) || (specialNum == 0) || (specialNum == 44)) { cout << \"Special number\" << endl; } else { cout << \"Not special number\" << endl; } return 0; }
question
Write an expression that prints \"Eligible\" if userAge is between 18 and 25 inclusive. Ex: 17 prints \"Ineligible\", 18 prints \"Eligible\".
answer
#include using namespace std; int main() { int userAge = 0; userAge = 17; if ((userAge >= 18) && (userAge <= 25)) { cout << \"Eligible\" << endl; } else { cout << \"Ineligible\" << endl; } return 0; }
question
Write a switch statement that checks nextChoice. If 0, print \"Rock\". If 1, print \"Paper\". If 2, print \"Scissors\". For any other value, print \"Unknown\". End with newline. Do not get input from the user; nextChoice is assigned in main().
answer
#include using namespace std; int main() { int nextChoice = 0; nextChoice = 2; switch (nextChoice) { case 0: cout << \"Rock\" << endl; break; case 1: cout << \"Paper\" << endl; break; case 2: cout << \"Scissors\" << endl; break; default: cout << \"Unknown\" << endl; break; } return 0; }
question
Write a switch statement that checks origLetter. If 'a' or 'A', print \"Alpha\". If 'b' or 'B', print \"Beta\". For any other character, print \"Unknown\". Use fall-through as appropriate. End with newline.
answer
#include using namespace std; int main() { char origLetter = '?'; origLetter = 'a'; switch (origLetter){ case 'a': cout << \"Alpha\" << endl; break; case 'A': cout << \"Alpha\" << endl; break; case 'b': cout << \"Beta\" << endl; break; case 'B': cout << \"Beta\" << endl; break; default: cout << \"Unknown\" << endl; break; } return 0; }
question
Write code to assign true to isTeenager if kidAge is 13 to 19 inclusive.
answer
#include using namespace std; int main() { bool isTeenager = false; int kidAge = 0; kidAge = 13; if (kidAge == 13) { isTeenager = true; } else if (kidAge == 14) isTeenager = true; else if (kidAge == 15) isTeenager = true; else if (kidAge == 16) isTeenager = true; else if (kidAge == 17) isTeenager = true; else if (kidAge == 18) isTeenager = true; else if (kidAge == 19) isTeenager = true; else { isTeenager = false; } if (isTeenager) { cout << \"Teen\" << endl; } else { cout << \"Not teen\" << endl; } return 0; }
question
Write an if-else statement to describe an object. Print \"Balloon\" if isBalloon is true and isRed is false. Print \"Red balloon\" if isBalloon and isRed are both true. Print \"Not a balloon\" otherwise. End with newline.
answer
#include using namespace std; int main() { bool isRed = false; bool isBalloon = false; if (isBalloon && isRed) { cout << \"Red balloon\" << endl; } else if (isBalloon && !isRed) { cout << \"Balloon\" << endl; } else { cout << \"Not a balloon\" << endl; } return 0; }
question
Write an if-else statement that prints \"Goodbye\" if userString is \"Quit\", else prints \"Hello\". End with newline.
answer
#include #include using namespace std; int main() { string userString; userString = \"Quit\"; if (userString == \"Quit\") { cout << \"Goodbye\" << endl; } else { cout << \"Hello\" << endl; } return 0; }
question
Print the two strings in alphabetical order. Assume the strings are lowercase. End with newline. Sample output: capes rabbits
answer
#include #include using namespace std; int main() { string firstString; string secondString; firstString = \"rabbits\"; secondString = \"capes\"; cin >> firstString >> secondString; if (secondString < firstString) { cout << secondString << \" \" << firstString << endl; } else if (firstString < secondString) { cout << firstString << \" \" << secondString << endl; } else if (firstString == secondString) { cout << firstString << \" \" << secondString << endl; } return 0; }
question
Assign the size of userInput to stringSize. Ex: if userInput = \"Hello\", output is: Size of userInput: 5
answer
#include #include using namespace std; int main() { string userInput; int stringSize = 0; userInput = \"Hello\"; stringSize = userInput.length(); cout << \"Size of userInput: \" << stringSize << endl; return 0; }
question
Print \"Censored\" if userInput contains the word \"darn\", else print userInput. End with newline. Note: These activities may test code with different test values. This activity will perform three tests, with userInput of \"That darn cat.\", then with \"Dang, that was scary!\", then with \"I'm darning your socks.\". See How to Use zyBooks. Also note: If the submitted code has an out-of-range access., the system will stop running the code after a few seconds, and report \"Program end never reached.\" The system doesn't print the test case that caused the reported message.
answer
#include #include using namespace std; int main() { string userInput; userInput = \"That darn cat.\"; if (userInput.find(\"darn\") != -1){ cout << \"Censored\" << endl; } else { cout << userInput << endl; } return 0; }
question
Modify secondVerse to play \"The Name Game\" (a.k.a. \"The Banana Song\", see Wikipedia.org), by replacing \"(Name)\" with userName but without the first letter. Ex: if userName = \"Katie\", the program prints: Banana-fana fo-fatie! Note: The song verse may change, such as: Banana-fana fo-f(Name)!!! or Apple-fana fo-f(Name)
answer
#include #include using namespace std; int main() { string secondVerse = \"Banana-fana fo-f(Name)!\"; string userName = \"Katie\"; userName.erase(userName.begin()); // Removes first char from userName secondVerse.replace(16, 6, userName); cout << secondVerse << endl; return 0; }
question
Retype and correct the code provided to combine two strings separated by a space. Hint: What type of parameter does push_back expect? secretID.push_back(spaceChar); secretID.push_back(lastName);
answer
#include #include using namespace std; int main() { string secretID = \"Barry\"; string lastName = \"Allen\"; char spaceChar = ' '; secretID.push_back(spaceChar); secretID.length(); secretID.append(lastName); cout << secretID << endl; return 0; }
question
Write an expression to detect that the first character of userInput matches firstLetter. INCOMPLETE
answer
#include #include using namespace std; int main() { string userInput; char firstLetter = '-'; userInput = \"banana\"; firstLetter = 'b'; if (firstLetter = userInput.at(0)) { cout << \"Found match: \" << firstLetter << endl; } else { cout << \"No match: \" << firstLetter << endl; } return 0; }
question
Write an expression to detect that the first character of userInput matches firstLetter.
answer
#include #include using namespace std; int main() { string userInput; char firstLetter = '-'; userInput = \"banana\"; firstLetter = 'b'; if (firstLetter == userInput.at(0)) { cout << \"Found match: \" << firstLetter << endl; } else { cout << \"No match: \" << firstLetter << endl; } return 0; }
question
Set hasDigit to true if the 3-character passCode contains a digit.
answer
#include #include #include using namespace std; int main() { bool hasDigit = false; string passCode; int valid = 0; passCode = \"abc\"; if (isdigit(passCode.at(0))){ hasDigit = true; } if (isdigit(passCode.at(1))){ hasDigit = true; } if (isdigit(passCode.at(2))){ hasDigit = true; }
question
Replace any space ' ' by '_' in 2-character string passCode. Sample output for the given program: 1_
answer
#include #include #include using namespace std; int main() { string passCode; passCode = \"1 \"; if (isspace(passCode.at(0))){ passCode.replace(0,1,\"_\"); } if (isspace(passCode.at(1))){ passCode.replace(1,1, \"_\"); } cout << passCode << endl; return 0; }
question
Create a conditional expression that evaluates to string \"negative\" if userVal is less than 0, and \"positive\" otherwise. Example output when userVal = -9 for the below sample program: -9 is negative.
answer
#include #include using namespace std; int main() { string condStr; int userVal = 0; userVal = -9; condStr = (userVal < 0)? \"negative\" : \"positive\"; cout << userVal << \" is \" << condStr << \".\" << endl; return 0; }
question
Using a conditional expression, write a statement that increments numUsers if updateDirection is 1, otherwise decrements numUsers. Ex: if numUsers is 8 and updateDirection is 1, numUsers becomes 9; if updateDirection is 0, numUsers becomes 7. Hint: Start with \"numUsers = ...\".
answer
#include using namespace std; int main() { int numUsers = 0; int updateDirection = 0; numUsers = 8; updateDirection = 1; numUsers = (updateDirection == 1)? numUsers+1 : numUsers-1; cout << \"New value is: \" << numUsers << endl; return 0; }
question
Write an expression that will cause the following code to print \"Equal\" if the value of sensorReading is \"close enough\" to targetValue. Otherwise, print \"Not equal\".
answer
#include #include using namespace std; int main() { double targetValue = 0.3333; double sensorReading = 0.0; sensorReading = 1.0/3.0; if (fabs(targetValue - sensorReading) < 0.001) { cout << \"Equal\" << endl; } else { cout << \"Not equal\" << endl; } return 0; }