Liang C++ 5E Revel Assigned Programming Quiz and Programming Project Solution Note: For every question you entered, test it using the solution. Chapter 1 Programming Quiz 1.6: Question 1: (00000-10501) Write a statement that prints Hello World to the screen. cout << "Hello World";
Programming Quiz 1.6: Question 2: (00000-10502) Write a complete program that prints Hello World to the screen. #include <iostream> using namespace std; int main() { cout << "Hello World"; return 0; } Programming Quiz 1.6: Question 3: (00000-10503) Suppose your name was Alan Turing. Write a statement that would print your last name, followed by a comma, followed by your first name. Do not print anything else (that includes blanks). cout << "Turing,Alan"; Chapter 1: Programming Project 1: (345021-00007) (Display three messages) Write a program that displays Welcome to C++ Welcome to Computer Science Programming is fun For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> using namespace std; int main() { cout << "Welcome to C++" << endl; cout << "Welcome to Computer Science" << endl; cout << "Programming is fun" << endl; return 0;
} Chapter 1: Programming Project 2: (345021-00008) (Compute expressions) Write a program that displays the result of (9.5 x 4.5 - 2.5 x 3) / (45.5 - 3.5) For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> using namespace std; int main() { cout << (9.5 * 4.5 - 2.5 * 3) / (45.5 - 3.5) << endl; }
return 0;
Chapter 1: Programming Project 3: (345021-00009) (Population projection) The U.S. Census Bureau projects population based on the following assumptions: One birth every 7 seconds. One death every 13 seconds. One new immigrant every 45 seconds Write a program that displays the population for each of the next five years. Assume the current population is 312,032,486 and one year has 365 days. Hint: In C++, if two integers perform division, the result is the quotient. The fractional part is truncated. For example, 5 / 4 is 1 (not 1.25) and 10 / 4 is 2 (not 2.5). To get an accuate result with the fractional part, one of the values involved in the division must be a number with a decimal point. For example, 5.0 / 4 is 1.25 and 10 / 4.0 is 2.5. For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> using namespace std; int main() { cout << 312032486 + 365 * 24 * 60 * 60 / 7.0 - 365 * 24 * 60 * 60 / 13.0 + 365 * 24 * 60 * 60 / 45.0 << endl; cout << 312032486 + 2 * 365 * 24 * 60 * 60 / 7.0 - 2 * 365 * 24 * 60 * 60 / 13.0 + 2 * 365 * 24 * 60 * 60 / 45.0 << endl; cout << 312032486 + 3 * 365 * 24 * 60 * 60 / 7.0 - 3 * 365 * 24 * 60 * 60 / 13.0 + 3 * 365 * 24 * 60 * 60 / 45.0 << endl; cout << 312032486 + 4 * 365 * 24 * 60 * 60 / 7.0 - 4 * 365 * 24 * 60 * 60 / 13.0 + 4 * 365 * 24 * 60 * 60 / 45.0 << endl;
cout << 312032486 + 5 * 365 * 24 * 60 * 60 / 7.0 - 5 * 365 * 24 * 60 * 60 / 13.0 + 5 * 365 * 24 * 60 * 60 / 45.0 << endl; }
return 0;
Chapter 1: Programming Project 4: (345021-00153) (Simple computation) The formula for computing the discriminant of a quadratic equation ax^2 + bx + c = 0 is b^2 – 4ac. Write a program that computes the discriminant for the equation 3x^2 + 4x + 5 = 0. For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> // Exercise01_01Extra using namespace std; int main() { cout << 4 * 4 - 4 * 3 * 5 << endl; }
return 0;
Chapter 1: Programming Project 5: (345021-00154) (Physics: acceleration) Average acceleration is defined as the change of velocity divided by the time taken to make the change, as shown in the following formula: a = (v1 - v0) / t Here, v0 is the starting velocity in meters/second, v1 is the ending velocity in meters/second, and t is the time span in seconds. Assume v0 is 5.6, v1 is 10.5, and t is 0.5, and write the code to display the average acceleration. For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> // Exercise01_02Extra using namespace std; int main() { cout << (10.5 - 5.6) / 0.5 << endl; }
return 0;
Chapter 2 Programming Quiz 2.2: Question 1: (00000-10982) Declare a double variable named distance.
double distance; Programming Quiz 2.2: Question 2: (00000-10983) Write a statement that declares a double variable named dosage. double dosage; Programming Quiz 2.3: Question 1: (00000-10968) Write a statement that reads a floating point (real) value from standard input into temperature. Assume that temperature has already been declared as a double variable. cin >> temperature; Programming Quiz 2.5: Question 1: (00000-10514) Write a complete program that •
declares an int variable,
•
reads a value from the keyboard into that variable, and
•
writes to standard output the square of the variable's value.
Besides the number, nothing else should be written to standard output. #include <iostream> using namespace std; int main() { int k; cin >> k; cout << k * k; }
return 0;
Programming Quiz 2.5: Question 2: (00000-10515) Write a complete program that •
declares an int variable,
•
reads a value from the keyboard into that variable, and
•
writes to standard output the variable's value, twice the value, and the square of the value, separated by spaces.
Besides the numbers, nothing else should be written to standard output except for spaces separating the values. #include <iostream> using namespace std; int main() { int k; cin >> k; cout << k << " " << k * 2 << " " << k * k;
}
return 0;
Programming Quiz 2.6: Question 1: (00000-10539) Given two int variables oldRecord and newRecord, write a statement that gives newRecord the same value that oldRecord has. newRecord = oldRecord; Programming Quiz 2.6: Question 2: (00000-10538) Given an int variable drivingAge that has already been declared, write a statement that assigns the value 17 to drivingAge. drivingAge = 17; Programming Quiz 2.6: Question 3: (00000-10947) Write a statement to set the value of num to 4 (num is a variable that has already been declared). num = 4;
Programming Quiz 2.7: Question 1: (00000-10519) Declare an int constant, MonthsInYear, whose value is 12. Note: The quizzes are a third-party product. The name for the constant does not follow the convention used in the text. It would be named MONTHS_IN_YEAR if the convention in the text is used. This is the only place we alert our users. We will not explicitly point it out in other places. const int MonthsInYear = 12;
Programming Quiz 2.7: Question 2: (00000-10520) Declare an int constant MonthsInDecade whose value is the value of the constant MonthsInYear (already declared) multiplied by 10. const int MonthsInDecade = MonthsInYear * 10;
Programming Quiz 2.8: Question 1: (00000-10505) Assume that an int variable x that has already been declared and initialized. Write an expression whose value is 1 more than x. x + 1 Programming Quiz 2.8: Question 2: (00000-10526) Assume that variables verbalScore and mathScore are already declared and assigned values. Write an expression that computes the sum of the two variables verbalScore and mathScore.
verbalScore + mathScore Programming Quiz 2.8: Question 3: (00000-10528) Assume two variables endingTime and startingTime are already declared and assigned values. Write an expression that computes the difference of the variables endingTime and startingTime. endingTime - startingTime Programming Quiz 2.8: Question 4: (00000-10530) Given the variable pricePerCase, write an expression corresponding to the price of a dozen cases. pricePerCase * 12 Programming Quiz 2.8: Question 5: (00000-10932) Write an expression that computes the remainder of the variable principal when divided by the variable divisor. (Assume both are type int.) principal % divisor
Programming Quiz 2.9: Question 1: (00000-10533) Write an expression that computes the average of the values 12 and 40. (12 + 40) / 2 Programming Quiz 2.9: Question 2: (00000-11013) The dimensions (width and length) of room1 have been read into two variables: width1 and length1. The dimensions of room2 have been read into two other variables: width2 and length2. Write a single expression whose value is the total area of the two rooms. length1 * width1 + length2 * width2 Programming Quiz 2.11: Question 1: (00000-10541) Given an int variable bridgePlayers, write a statement that increases the value of that variable by 4. bridgePlayers += 4; Programming Quiz 2.11: Question 2: (00000-10542) Given an int variable profits, write a statement that increases the value of that variable by a factor of 10. profits *= 10; Programming Quiz 2.11: Question 3: (00000-10961)
Write a statement using a compound assignment operator to subtract 10 from minutes_left (an int variable that has already been declared and initialized). minutes_left -= 10; Programming Quiz 2.11: Question 4: (00000-10962) Write a statement using a compound assignment operator to cut the value of pay in half (pay is an integer variable that has already been declared and initialized). pay /= 2;
Programming Quiz 2.11: Question 5: (00000-10964) Write a statement using a compound assignment operator to change val to the remainder when val is divided by 16 (val has already been declared and initialized) val %= 16; Programming Quiz 2.12: Question 1: (00000-10958) Write a statement using the increment operator or combined assignment operator to increase the value of num_items (an already declared integer variable) by 1. num_items++; Programming Quiz 2.12: Question 2: (00000-10544) Given an int variable timer, write a statement that uses the decrement operator to decrease the value of that variable by 1. timer--; Programming Quiz 2.12: Question 3: (00000-10959) Write a statement using the decrement operator to decrease the value of count (an already declared integer variable) by 1. count--; Programming Quiz 2.13: Question 1: (00000-10608) Given two int variables distance and speed, write an expression that divides distance by speed using floating point arithmetic, i.e., a fractional result should be produced. static_cast<double>(distance) / speed Programming Quiz 2.13: Question 2: (00000-11017) In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Write an expression whose value is the 8th harmonic number.
1 + 1.0 / 2 + 1.0 / 3 + 1.0 / 4 + 1.0 / 5 + 1.0 / 6 + 1.0 /7 + 1.0 / 8 Chapter 2: Programming Project 1: (345021-00010) (Financial application: calculate tips) Write a program that reads the subtotal and the gratuity rate, then computes the gratuity and total. For example, if the user enters 10 for subtotal and 15% for gratuity rate, the program displays $1.5 as gratuity and $11.5 as total. Sample Run Enter the subtotal and a gratuity rate: 10 15 The gratuity is $1.5 and total is $11.5 For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> using namespace std; int main() { cout << "Enter the subtotal and a gratuity rate: "; double subtotal, gratuityRate; cin >> subtotal >> gratuityRate; double gratuity = subtotal * gratuityRate / 100; double total = subtotal + gratuity; cout << "The gratuity is " << gratuity << " and total is " << total << endl; }
return 0;
Chapter 2: Programming Project 2: (345021-00011) (Find the number of years) Write a program that prompts the user to enter the minutes (e.g., 1 billion) as an integer and displays the number of years and days for the minutes. For simplicity, assume a year has 365 days. Sample Run Enter the number of minutes: 1000000000 1000000000 minutes is approximately 1902 years and 214 days For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> using namespace std; int main() { // Prompt the user to enter the number of minutes
cout << "Enter the number of minutes: "; long minutes; cin >> minutes; long totalNumberOfDays = minutes / (24 * 60); long numberOfYears = totalNumberOfDays / 365; long remainingNumberOfDays = totalNumberOfDays % 365; // Display results cout << minutes << " minutes is approximately " << numberOfYears << " years and " << remainingNumberOfDays << " days." << endl; }
return 0;
Chapter 2: Programming Project 3: (345021-00012) (Geometry: distance of two points) Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their distance between them. The formula for computing the distance is: Square root of ((x2 - x1) squared + (y2 - y1) squared) Note that you can use pow(a, 0.5) to compute square root of a.
Sample Run
Enter x1 and y1: 1.5 -3.4 Enter x2 and y2: 4 5 The distance between the two points is 8.764131445842194 For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> #include <cmath> using namespace std; int main() { // Enter the first point with two double values cout << "Enter x1 and y1: "; double x1, y1; cin >> x1 >> y1; // Enter the second point with two double values cout << "Enter x2 and y2: "; double x2, y2; cin >> x2 >> y2; // Compute the distance double distance = pow((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2), 0.5); cout << "The distance of the two points is " << distance;
}
return 0;
Chapter 2: Programming Project 4: (345021-00013) (Geometry: area of a triangle) Write a program that prompts the user to enter three points (x1, y1), (x2, y2), (x3, y3) of a triangle and displays its area. The formula for computing the area of a triangle is: s = (side1 + side2 + side3) / 2 area = square root of s(s - side1)(s - side2)(s - side3) Sample Run Enter three points for a triangle: 1.5 -3.4 4.6 5 9.5 -3.4 The area of the triangle is 33.6 For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> // Exercise02_19 #include <cmath> using namespace std; int main() { // Enter three points for a triangle double x1, y1, x2, y2, x3, y3; cout << "Enter three points for a triangle: "; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; // Compute the length of the three sides double side1 = pow((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2), 0.5); double side2 = pow((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3), 0.5); double side3 = pow((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2), 0.5); double s = (side1 + side2 + side3) / 2; double area = pow(s * (s - side1) * (s - side2) * (s - side3), 0.5); cout << "The area of the triangle is " << area << endl; }
return 0;
Chapter 2: Programming Project 5: (345021-00014) (Financial application: future investment value) Write a program that reads in investment amount, annual interest rate, and number of years, and displays the future investment value using the following formula: futureInvestmentValue =
investmentAmount x (1 + monthlyInterestRate)^(numberOfYears x 12) For example, if you enter amount 1000.56, annual interest rate 4.25%, and number of years 1, the future investment value is $1043.92. Sample Run Enter investment amount: 1000.56 Enter annual interest rate in percentage: 4.25 Enter number of years: 1 Accumulated value is $1043.92 For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> #include <cmath> using namespace std; int main() { double investmentAmount, annualInterestRate, numberOfYears; cout << "Enter investment amount: "; cin >> investmentAmount; cout << "Enter annual interest rate: "; cin >> annualInterestRate; cout << "Enter number of years: "; cin >> numberOfYears; double monthlyInterestRate = annualInterestRate / 1200; double accumulatedValue = investmentAmount * pow(1 + monthlyInterestRate, numberOfYears * 12); cout << "Future value is $" << accumulatedValue << endl; }
return 0;
Chapter 3 Programming Quiz 3.2: Question 1: (00000-10597) 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
Programming Quiz 3.2: Question 2: (00000-10599)
Given the variables numberOfMen and numberOfWomen, write an expression that evaluates to true if the number of men is greater than or equal to the number of women. numberOfMen >= numberOfWomen
Programming Quiz 3.2: Question 3: (00000-10907) Write an expression that evaluates to true if the value of the int variable x is divisible (with no remainder) by the int variable y. (Assume that y is not zero.) All the variables used in the programming quizzes are assumed to be declared already unless explicitly specified otherwise. This is the only place we alert our users. We will not explicitly point it out in other places. x % y == 0 Programming Quiz 3.2: Question 4: (00000-10547) Write an expression that evaluates to true if the int variable x contains an even value, and false if it contains an odd value. x % 2 == 0
Programming Quiz 3.3: Question 1: (00000-10561) Write an if statement that assigns 10,000 to the variable bonus if the value of the variable goodsSold is greater than 500,000. Assume bonus and goodsSold have been declared and initialized. if (goodsSold > 500000) bonus = 10000; Programming Quiz 3.3: Question 2: (00000-10562) Write an if statement that decreases the variable shelfLife by 4 if the variable outsideTemperature is greater than 90. Assume shelfLife and outsideTemperature have been declared and initialized. if (outsideTemperature > 90) shelfLife -= 4; Programming Quiz 3.3: Question 3: (00000-11060) 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 output if gpa exceeds 3.5. if (gpa > 3.5) { deansList++; cout << studentName << endl; }
Programming Quiz 3.4 : Question 1: (00000-10564) 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. Assume all variables have been declared and initialized. if (age >= 65) seniorCitizens++; else nonSeniors++; Programming Quiz 3.4: Question 2: (00000-11119) 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 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.0) cout << pow(areaOfSquare, 0.5); else cout << "INVALID"; Programming Quiz 3.5: Question 1: (00000-10567) Write a multi-way if 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 < 65) adults++; else seniors++; Programming Quiz 3.5: Question 2: (00000-10568) Write a multi-way if statement that compares the double variable pH with 7.0 and makes the following assignments to the bool variables neutral, base, and acid: •
false, false, true if pH is less than 7
•
false, true, false if pH is greater than 7
•
true, false, false if pH is equal to 7
if (pH < 7) { neutral = false; base = false; acid = true; } else if (pH > 7)
{
neutral = false; base = true; acid = false;
} else { neutral = true; base = false; acid = false; }
Programming Quiz 3.5: Question 3: (00000-11064) 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. Assign 0 to freeBooks if no free books are offered. Assume the variables freeBooks, isPremiumCustomer, and nbooksPurchased are already declared. if (isPremiumCustomer) { if (nbooksPurchased >= 8) freeBooks = 2; else if (nbooksPurchased >= 5) freeBooks = 1; else freeBooks = 0; } else { if (nbooksPurchased >= 12) freeBooks = 2; else if (nbooksPurchased >= 7) freeBooks = 1; else freeBooks = 0; } Programming Quiz 3.10: Question 1: (00000-10556) Given the variables temperature and humidity, write an expression that evaluates to true if and only if the temperature is greater than 90 and the humidity is less than 10. temperature > 90 && humidity < 10
Programming Quiz 3.10: Question 2: (00000-10557) Given the int 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 Programming Quiz 3.10: Question 3: (00000-10558)
Given the bool variable isFullTimeStudent and the int variable age, write an expression that evaluates to true if age is less than 19 or isFullTimeStudent is true. age < 19 || isFullTimeStudent Programming Quiz 3.10: Question 4: (00000-11067) 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 = 2001 <= modelYear && modelYear <= 2006; Programming Quiz 3.11: Question 1: (00000-11118) Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. A bool variable named norecall has been declared. Given an int variable modelYear, write a statement that assigns true to norecall if the value of modelYear does not fall within the two recall ranges and assigns false otherwise. Do not use an if statement in this exercise! norecall = !(1995 <= modelYear && modelYear <= 1998 || 2004 <= modelYear && modelYear <= 2006); Programming Quiz 3.11: Question 2: (00000-11116) 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 < 2001 || modelYear > 2006) cout << "NO RECALL"; Programming Quiz 3.11: Question 3: (00000-11066) 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 (2001 <= modelYear && modelYear <= 2006) cout << "RECALL"; Programming Quiz 3.13: Question 1: (00000-11074) 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; } Programming Quiz 3.13: Question 2: (00000-11073) In the Happy Valley School System, children are classified by age as follows: •
less than 2, ineligible
•
2, toddler
•
3-5, early childhood
•
6-7, young reader
•
8-10, elementary
•
11 and 12, middle
•
13, impossible
•
14-16, high school
•
17-18, scholar
•
greater than 18, ineligible
Given an int variable age, write a switch statement that prints out the appropriate label from the above list based on age. switch (age) { case 2: cout << "toddler"; break; case 3: case 4: case 5: cout << "early childhood"; break; case 6: case 7: cout << "young reader"; break; case 8: case 9: case 10: cout << "elementary"; break; case 11: case 12: cout << "middle"; break; case 13: cout << "impossible"; break; case 14: case 15: case 16: cout << "high school"; break; case 17: case 18: cout << "scholar"; break; default: cout << "ineligible"; break; } Programming Quiz 3.14: Question 1: (00000-10569)
Write an expression using the conditional operator (? :) that compares the value of the variable x to 5 and results in: •
x if x is greater than or equal to 5
•
-x if x is less than 5
x >= 5 ? x : -x
Programming Quiz 3.14: Question 2: (00000-10777) Assume that day is an int variable whose value is 1 or 2 or ... or 7. Write an expression whose value is "sun" or "mon" or "tue" or "wed" or "thu" or "fri" or "sat" based on the value of day. day == 1 ? "sun" : day == 2 ? "mon" : day == 3 ? "tue" : day == 4 ? "wed":day == 5 ? "thu" : day == 6 ? "fri" : "sat" Programming Quiz 3.14: Question 3: (00000-10806) Assume that credits is an int variable whose value is 0 or positive. Write an expression whose value is "freshman" or "sophomore" or "junior" or "senior" based on the value of credits. In particular: if the value of credits is less than 30 the expression's value is "freshman"; 30-59 would be a "sophomore"; 60-89 would be "junior" and 90 or more would be a "senior". credits < 30 ? "freshman" : credits <= 59 ? "sophomore" : credits <= 89 ? "junior" : "senior" Chapter 3: Programming Project 1: (345021-00015) The description of this project is given in Programming Exercise 3.1 in the Chapter 3 Programming Exercise from the Book section. For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> #include <cmath> using namespace std; int main() { cout << "Enter a, b, c: "; double a, b, c, discriminant, r1, r2; cin >> a >> b >> c; discriminant = b * b - 4 * a * c; if (discriminant < 0) {
cout << "The equation has no real roots" << endl; } else if (discriminant == 0) { r1 = (-b + pow(discriminant, 0.5)) / (2 * a); cout << "The root is " << r1 << endl; } else // (discriminant >= 0) { r1 = (-b + pow(discriminant, 0.5)) / (2 * a); r2 = (-b - pow(discriminant, 0.5)) / (2 * a); cout << "The roots are " << r1 << " and " << r2 << endl; } }
return 0;
Chapter 3: Programming Project 2: (345021-00016) (Algebra: solve 2 × 2 linear equations) You can use Cramer’s rule to solve the following 2 × 2 system of linear equation: ax + by = e cx + dy = f x = (ed - bf) / (ad - bc) y = (af - ec) / (ad - bc) Write a program that prompts the user to enter a, b, c, d, e and f and displays the result. If ad - bc is 0, report that “The equation has no solution”. Sample Run 1 Enter a, b, c, d, e, f: 9.0 4.0 3.0 -5.0 -6.0 -21.0 x is -2 and y is 3 Sample Run 2 Enter a, b, c, d, e, f: 1.0 2.0 2.0 4.0 4.0 5.0 The equation has no solution For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> using namespace std; int main() { cout << "Enter a, b, c, d, e, f: "; double a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f; double detA = a * d - b * c;
if (detA == 0) cout << "The equation has no solution" << endl; else { double x = (e * d - b * f) / detA; double y = (a * f - e * c) / detA; cout << "x is " << x << " and y is " << y << endl; } }
return 0;
Chapter 3: Programming Project 3: (345021-00017) (Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2012, the program should display February 2012 has 29 days If the user entered month 3 and year 2015, the program should display March 2015 has 31 days For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html. #include <iostream> using namespace std; int main() { cout << "Enter a month in the year (e.g., 1 for Jan): "; int month; cin >> month; cout << "Enter a year: "; int year; cin >> year; int numberOfDaysInMonth = 0; switch (month) { case 1: cout << "January " << year; numberOfDaysInMonth = 31; break; case 2: cout << "February " << year; if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) numberOfDaysInMonth = 29; else numberOfDaysInMonth = 28; break; case 3:
}
cout << "March " << year; numberOfDaysInMonth = 31; break; case 4: cout << "April " << year; numberOfDaysInMonth = 30; break; case 5: cout << "May " << year; numberOfDaysInMonth = 31; break; case 6: cout << "June " << year; numberOfDaysInMonth = 30; break; case 7: cout << "July " << year; numberOfDaysInMonth = 31; break; case 8: cout << "August " << year; numberOfDaysInMonth = 31; break; case 9: cout << "September " << year; numberOfDaysInMonth = 30; break; case 10: cout << "October " << year; numberOfDaysInMonth = 31; break; case 11: cout << "November " << year; numberOfDaysInMonth = 30; break; case 12: cout << "December " << year; numberOfDaysInMonth = 31; break;
cout << " has " << numberOfDaysInMonth << " days"; }
return 0;
Chapter 3: Programming Project 4: (345021-00018) (Science: day of the week) Zeller’s congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is: h = (q + 26(m + 1)/10 + k + k/4 + j/4 + 5j) % 7 where - h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, 6: Friday).