Chapter 4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Given the boolean variable isFullTimeStudent and the integer variable age, write an expression that evaluates to true if age is less than 19 or isFullTimeStudent is true .

age < 19 || isFullTimeStudent == true

Given a double variable called average, write an expression that is true if and only if the variable 's value is less than 60.0.

average < 60.0

Given the char variable c, write an expression that is true if and only if the value of c is not the space character .

c!=' '

NOTE: in mathematics, the square root of a negative number is not real; in C++ therefore, passing such a value to the square root function is an error. Given a double variable named areaOfSquare write the necessary code to read in a value , the area of some square, into areaOfSquare and print out the length of the side of that square. HOWEVER: if any value read in is not valid input, just print the message "INVALID".

cin >> areaOfSquare; if(areaOfSquare >= 0){ double length = sqrt(areaOfSquare); cout << length; }else{ cout << "INVALID"; }

Assume that isIsosceles is a bool variable , and that the variables isoCount, triangleCount, and polygonCount have all been declared and initialized . Write a statement that adds 1 to each of these count variables (isoCount, triangleCount, and polygonCount) if isIsosceles is true .

if (isIsosceles == true) { isoCount ++; triangleCount ++; polygonCount ++; }

Write an expression that evaluates to true if and only if the value of the boolean variable isAMember is false .

isAMember == false

Write an expression that evaluates to true if and only if the integer x is greater than the integer y.

x>y

Given an int variable grossPay, write an expression that evaluates to true if and only if the value of grossPay is less than 10,000.

grossPay < 10000

Working overtime is defined as having worked more than 40 hours during the week. Given the variable hoursWorked, write an expression that evaluates to true if the employee worked overtime.

hoursWorked > 40

Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. Given a variable modelYear write a statement that prints the message "RECALL" to standard output if the value of modelYear falls within those two ranges.

if ((modelYear > 1994 && modelYear < 1999) || (modelYear > 2003 && modelYear < 2007)) { cout << "RECALL"; }else{ }

Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002. Given an int variable modelYear and a string modelName write a statement that prints the message "RECALL" to standard output if the values of modelYear and modelName match the recall details.

if ((modelYear >= 1999 && modelYear <= 2002) && (modelName == "Extravagant")) { cout << "RECALL"; }

Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18, adds 1 to the variable adults if age is 18 through 64, and adds 1 to the variable seniors if age is 65 or older.

if (age < 18) minors ++; else if (age >= 18 && age <= 64) adults ++; else seniors ++;

Write an if/else statement that compares the variable age with 65, adds 1 to the variable seniorCitizens if age is greater than or equal to 65, and adds 1 to the variable nonSeniors otherwise.

if (age >= 65) seniorCitizens ++; else nonSeniors ++;

Write a conditional that assigns 10,000 to the variable bonus if the value of the variable goodsSold is greater than 500,000.

if (goodsSold > 500000) bonus = 10000;

Assume that the variables gpa, deansList and studentName, have been declared and initialized . Write a statement that both adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5.

if (gpa > 3.5) { deansList ++; cout << studentName; }

Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. Given an int variable modelYear write a statement that prints the message "NO RECALL" to standard output if the value of modelYear DOES NOT fall within that range.

if (modelYear > 2000 && modelYear < 2007) { }else{ cout << "NO RECALL";

Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. Given a variable modelYear write a statement that prints the message "RECALL" to standard output if the value of modelYear falls within that range.

if (modelYear >= 2001 && modelYear <= 2006) cout << "RECALL";

Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books. It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books. Write a statement that assigns freeBooks the appropriate value based on the values of the bool variable isPremiumCustomer and the int variable nbooksPurchased.

if (nbooksPurchased > 4){ if (isPremiumCustomer){ freeBooks = 1; if (nbooksPurchased > 7){ freeBooks = 2; } }else{ freeBooks = 0; if (nbooksPurchased > 6){ freeBooks = 1; } if (nbooksPurchased > 11){ freeBooks = 2; } } }else{ freeBooks = 0; }

Write a statement that toggles the value of the bool variable onOffSwitch. That is, if onOffSwitch is false , its value is changed to true ; if onOffSwitch is true , its value is changed to false .

if (onOffSwitch == false) { onOffSwitch = true; } else { onOffSwitch = false; }

Write a conditional that decreases the variable shelfLife by 4 if the variable outsideTemperature is greater than 90.

if (outsideTemperature > 90)shelfLife -= 4;

Write an expression that evaluates to true if the integer variable x contains an even value , and false if it contains an odd value .

(x % 2 == 0)

NOTE: in mathematics, division by zero is undefined. So, in C++, division by zero is always an error. Given a int variable named callsReceived and another int variable named operatorsOnCall write the necessary code to read values into callsReceived and operatorsOnCall and print out the number of calls received per operator (integer division with truncation will do). HOWEVER: if any value read in is not valid input, just print the message "INVALID".

cin >> callsReceived; cin >> operatorsOnCall; if (operatorsOnCall > 0){ cout << (callsReceived / operatorsOnCall); }else{ cout << "INVALID";

Assume that an int variable age has been declared and already given a value and assume that a char variable choice has been declared as well. Assume further that the user has just been presented with the following menu: •S: hangar steak, red potatoes, asparagus •T: whole trout, long rice, brussel sprouts •B: cheddar cheeseburger, steak fries, cole slaw (Yes, this menu really IS a menu!) Write some code that reads a single character (S or T or B) into choice. Then the code prints out a recommended accompanying drink as follows: If the value of age is 21 or lower, the recommendation is "vegetable juice" for steak, "cranberry juice" for trout, and "soda" for the burger. Otherwise, the recommendations are "cabernet", "chardonnay", and "IPA" for steak, trout, and burger respectively. Regardless of the value of age, your code should print "invalid menu selection" if the character read into choice was not S or T or B.

cin >> choice; if(age < 22){ switch(choice){ case 'S': cout << "vegetable juice"; break; case 'T': cout << "cranberry juice"; break; case 'B': cout << "soda"; break; default: cout << "invalid menu selection"; } }else{ switch(choice){ case 'S': cout << "cabernet"; break; case 'T': cout << "chardonnay"; break; case 'B': cout << "IPA"; break; default: cout << "invalid menu selection"; } }

Given a int variable named yesCount and another int variable named noCount and a char variable named response, write the necessary code to read a value into response and then carry out the following: •if the character typed in is a y or a Y then increment yesCount and print out "YES WAS RECORDED" •if the character typed in is an n or an N then increment noCount and print out "NO WAS RECORDED" If the input is invalid just print the message "INVALID" and do nothing else.

cin >> response; switch (response) { case 'Y': case 'y': cout << "YES WAS RECORDED"; yesCount++; break; case 'N': case 'n': cout << "NO WAS RECORDED"; noCount++; break; default: cout << "INVALID"; break; }

Write a statement that compares the values of score1 and score2 and takes the following actions. When score1 exceeds score2, the message "player1 wins" is printed to standard out. When score2 exceeds score1, the message "player2 wins" is printed to standard out. In each case, the variables player1Wins,, player1Losses, player2Wins, and player2Losses,, are incremented when appropriate. Finally, in the event of a tie, the message "tie" is printed and the variable tieCount is incremented .

if (score1 > score2) { cout << "player1 wins"; player1Wins ++; player2Losses ++; } if (score2 > score1) { cout << "player2 wins"; player1Losses ++; player2Wins ++; } if (score1 == score2) { cout << "tie"; tieCount ++; }

Write an if/else statement that compares the value of the variables soldYesterday and soldToday, and based upon that comparison assigns salesTrend the value -1 or 1. -1 represents the case where soldYesterday is greater than soldToday; 1 represents the case where soldYesterday is not greater than soldToday.

if (soldYesterday > soldToday) salesTrend =- 1; else salesTrend = 1;

Write a statement that increments (adds 1 to) one and only one of these five variables : reverseDrivers parkedDrivers slowDrivers safeDrivers speeders. The variable speed determines which of the five is incremented as follows: The statement increments reverseDrivers if speed is less than 0, increments parkedDrivers if speed is less than 1, increments slowDrivers if speed is less than 40, increments safeDrivers if speed is less than or equal to 65, and otherwise increments speeders.

if (speed <0) { reverseDrivers ++; } if (speed >= 0 && speed < 1) { parkedDrivers ++; } if (speed >= 1 && speed < 40) { slowDrivers ++; } if (speed >= 40 && speed <= 65) { safeDrivers ++; } if (speed >= 65 && speed > 65) { speeders ++; }

Write an if/else statement that assigns true to the variable fever if the variable temperature is greater than 98.6; otherwise it assigns false to fever.

if (temperature > 98.6) fever = true; else fever = false;

Write a conditional that assigns true to the variable fever if the variable temperature is greater than 98.6.

if (temperature > 98.6) fever = true;

Write a conditional that multiplies the values of the variable pay by one-and-a-half if the value of the boolean variable workedOvertime is true .

if (workedOvertime){ pay = pay *1.5; }

Assume that a bool variable isQuadrilateral has been declared , and that an int variable , numberOfSides has been declared and initialized . Write a statement that assigns the value of isQuadrilateral to true if numberOfSides is exactly 4 and false otherwise.

isQuadrilateral = numberOfSides == 4;

Write an expression that evaluates to true if and only if the variables profits and losses are exactly equal .

profits == losses

Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. A bool variable named recalled has been declared . Given an int variable modelYear write a statement that assigns true to recalled if the value of modelYear falls within the recall range and assigns false otherwise. Do not use an if statement in this exercise!

recalled = (modelYear >= 2001 && modelYear <= 2006);

Assume that grade is a variable whose value is a letter grade-- any one of the following letters: 'A', 'B', 'C', 'D', 'E', 'F', 'W', 'I'. Assume further that there are the following int variables , declared and already initialized : acount, bcount, ccount, dcount, ecount, fcount, wcount, icount. Write a switch statement that increments the appropriate variable (acount, bcount, ccount, etc.) depending on the value of grade. So if grade is 'A' then acount is incremented ; if grade is'B' then bcount is incremented , and so on.

switch (grade) { case 'A': acount++; break; case 'B': bcount++; break; case 'C': ccount++; break; case 'D': dcount++; break; case 'E': ecount++; break; case 'F': fcount++; break; case 'W': wcount++; break; case 'I': icount++; break; }

HTTP is the protocol that governs communications between web servers and web clients (i.e. browsers). Part of the protocol includes a status code returned by the server to tell the browser the status of its most recent page request. Some of the codes and their meanings are listed below: •200, OK (fulfilled) •403, forbidden •404, not found •500, server error Given an int variable status, write a switch statement that prints out the appropriate label from the above list based on status.

switch( status ){ case 200: cout << "OK (fulfilled)"; break; case 403: cout << "forbidden"; break; case 404: cout << "not found"; break; case 500: cout << "server error"; break; }

Write an expression that evaluates to true if and only if the value of the boolean variable workedOvertime is true .

workedOvertime == true

Write an expression that evaluates to true if the value of the integer variable x is divisible (with no remainder) by the integer variable y. (Assume that y is not zero.)

x % y == 0

Write an expression that evaluates to true if the value x is greater than or equal to y.

x>=y

Given the integer variables yearsWithCompany and department, write an expression that evaluates to true if yearsWithCompany is less than 5 and department is not equal to 99.

yearsWithCompany < 5 && department != 99


संबंधित स्टडी सेट्स

NS 415 Test 1 Objectives (Ch 1,2,3,4,17,18,23,25)

View Set

Control of the Internal Environment

View Set

Chapter 15: Pain Management During Childbirth

View Set

CT-Exam Secrets Practice Questions

View Set