0% found this document useful (0 votes)
68 views

Solutions for Exercises from Introduction to C++ Programming and Data Structures, 5th Edition by Liang

The document contains a series of programming quizzes and projects related to C++ programming, including tasks such as printing messages, computing expressions, and calculating population projections. Each section provides code examples and hints for solving the problems. It also includes quizzes that cover variable declarations, arithmetic operations, and user input handling.

Uploaded by

nazrinajeeb3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Solutions for Exercises from Introduction to C++ Programming and Data Structures, 5th Edition by Liang

The document contains a series of programming quizzes and projects related to C++ programming, including tasks such as printing messages, computing expressions, and calculating population projections. Each section provides code examples and hints for solving the problems. It also includes quizzes that cover variable declarations, arithmetic operations, and user input handling.

Uploaded by

nazrinajeeb3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

You can access complete document on following URL.

Contact me if site not loaded


https://solumanu.com/product/c-plus-plus-programming-liang/
Liang C++ 5E Revel Assigned Programming Quiz and Programming Project Solution

Note: For every question you entered, test it using the solution.
sm
Chapter 1

Programming Quiz 1.6: Question 1: (00000-10501)

Write a statement that prints Hello World to the screen.

cout << "Hello World";


tb9
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";
8@
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).


gm
cout << "Turing,Alan";

Chapter 1: Programming Project 1: (345021-00007)

(Display three messages)

Write a program that displays

Welcome to C++
ail

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.
.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;

WhatsApp: https://wa.me/message/2H3BV2L5TTSUF1 - Telegram: https://t.me/solutionmanual


Contact me in order to access the whole complete document - Email: [email protected]
}

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)

You might also like