Skip to main content

Solution Manual For Introduction to C++ Programming and Data Structures, 5th edition Y Daniel Liang

Page 1

Solution Manual For Introduction to C++ Programming and Data Structures, 5th edition Y Daniel Liang ___________________________________________________________________________________ Chapters 1-27

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.


Turn static files into dynamic content formats.

Create a flipbook