Solutions for Exercises from Introduction to C++ Programming and Data Structures, 5th Edition by Liang
Solutions for Exercises from Introduction to C++ Programming and Data Structures, 5th Edition by Liang
Note: For every question you entered, test it using the solution.
sm
Chapter 1
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World";
8@
return 0;
}
Welcome to C++
ail
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.
.co
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to C++" << endl;
cout << "Welcome to Computer Science" << endl;
cout << "Programming is fun" << endl;
m
return 0;
(Compute expressions)
#include <iostream>
using namespace std;
int main()
{
cout << (9.5 * 4.5 - 2.5 * 3) / (45.5 - 3.5) << endl;
return 0;
}
(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.
#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;
}
(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.
int main()
{
cout << 4 * 4 - 4 * 3 * 5 << endl;
return 0;
}
(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.
int main()
{
cout << (10.5 - 5.6) / 0.5 << endl;
return 0;
}
Chapter 2
double dosage;
#include <iostream>
using namespace std;
int main()
{
int k;
cin >> k;
cout << k * k;
return 0;
}
• 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;
}
newRecord = oldRecord;
drivingAge = 17;
num = 4;
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.
x + 1
endingTime - startingTime
pricePerCase * 12
principal % divisor
(12 + 40) / 2
bridgePlayers += 4;
profits *= 10;
minutes_left -= 10;
pay /= 2;
val %= 16;
num_items++;
timer--;
count--;
static_cast<double>(distance) / speed
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
int main()
{
cout << "Enter the subtotal and a gratuity rate: ";
double subtotal, gratuityRate;
cin >> subtotal >> gratuityRate;
cout << "The gratuity is " << gratuity << " and total is " << total <<
endl;
return 0;
}
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
int main()
{
// Prompt the user to enter the number of minutes
cout << "Enter the number of minutes: ";
long minutes;
cin >> minutes;
// Display results
cout << minutes << " minutes is approximately " <<
numberOfYears << " years and " << remainingNumberOfDays
<< " days." << endl;
return 0;
}
Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their
distance between them.
Note that you can use pow(a, 0.5) to compute square root of a.
Sample Run
int main()
{
// Enter the first point with two double values
cout << "Enter x1 and y1: ";
double x1, y1;
cin >> x1 >> y1;
cout << "The distance of the two points is " << distance;
return 0;
}
Write a program that prompts the user to enter three points (x1, y1), (x2, y2), (x3, y3) of a triangle and
displays its area.
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
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;
cout << "The area of the triangle is " << area << endl;
return 0;
}
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
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double investmentAmount, annualInterestRate, numberOfYears;
cout << "Enter investment amount: ";
cin >> investmentAmount;
double accumulatedValue =
investmentAmount * pow(1 + monthlyInterestRate, numberOfYears * 12);
return 0;
}
Chapter 3
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