Practice Exercises - Chapter: 08
* Exercise 8.1: Movie Data
Write a program that uses a structure named MovieData to store the following information
about a movie:
Title
Director
Year Released
Running Time (in minutes)
The program should create two MovieData variables, store values in their members, and
pass each one, in turn, to a function that displays the information about the movie in a
clearly formatted manner.
Solution 8.1:
#include <iostream>
#include <string>
using namespace std;
struct MovieData {
string title;
string director;
int yearReleased;
int runningTime; // don v?: phút
};
void displayMovie(const MovieData &movie) {
cout << "============================\n";
cout << "Title : " << [Link] << "\n";
cout << "Director : " << [Link] << "\n";
cout << "Year : " << [Link] << "\n";
cout << "Running Time: " << [Link] << " minutes\n";
cout << "============================\n\n";
}
int main() {
MovieData movie1 = {"Inception", "Christopher Nolan", 2010, 148};
MovieData movie2 = {"The Matrix", "The Wachowskis", 1999, 136};
displayMovie(movie1);
displayMovie(movie2);
return 0;
}
* Exercise 8.2 : Movie Profit
Modify the Movie Data program written for Problem 1 to include two additional members
that hold the movie’s production costs and first-year revenues. Modify the function that
displays the movie data to display the title, director, release year, running time, and first
year’s profit or loss.
Solution 8.2:
#include <iostream>
#include <string>
using namespace std;
struct MovieData {
string title;
string director;
int yearReleased;
int runningTime;
double productionCosts;
double firstYearRevenue;
};
void displayMovie(const MovieData &movie) {
double profit = [Link] - [Link];
cout << "============================\n";
cout << "Title : " << [Link] << "\n";
cout << "Director : " << [Link] << "\n";
cout << "Year : " << [Link] << "\n";
cout << "Running Time: " << [Link] << " minutes\n";
cout << "Profit/Loss : ";
if (profit >= 0)
cout << "$" << profit << " (Profit)\n";
else
cout << "$" << profit << " (Loss)\n";
cout << "============================\n\n";
}
int main() {
MovieData movie1 = {"Inception", "Christopher Nolan", 2010, 148,
160000000, 829895144};
MovieData movie2 = {"The Matrix", "The Wachowskis", 1999, 136,
63000000, 466364845};
displayMovie(movie1);
displayMovie(movie2);
return 0;
}
* Exercise 8.3 : Corporate Sales Data
Write a program that uses a structure to store the following data on a company division:
Division Name (such as East, West, North, or South)
First-Quarter Sales
Second-Quarter Sales
Third-Quarter Sales
Fourth-Quarter Sales
Total Annual Sales
Average Quarterly Sales
The program should use four variables of this structure. Each variable should represent one
of the following corporate divisions: East, West, North, and South. The user should be
asked for the four quarters’ sales figures for each division. Each division’s total and average
sales should be calculated and stored in the appropriate member of each structure variable.
These figures should then be displayed on the screen.
Input Validation: Do not accept negative numbers for any sales figures.
Solution 8.3:
#include <iostream>
#include <string>
using namespace std;
struct Division {
string name;
double q1;
double q2;
double q3;
double q4;
double totalAnnual;
double averageQuarterly;
};
double getSalesInput(const string& prompt) {
double value;
do {
cout << prompt;
cin >> value;
if (value < 0)
cout << "Sales cannot be negative. Try again.\n";
} while (value < 0);
return value;
}
void inputDivisionData(Division &div) {
cout << "Enter sales data for " << [Link] << " division:\n";
div.q1 = getSalesInput(" First-Quarter Sales: ");
div.q2 = getSalesInput(" Second-Quarter Sales: ");
div.q3 = getSalesInput(" Third-Quarter Sales: ");
div.q4 = getSalesInput(" Fourth-Quarter Sales: ");
[Link] = div.q1 + div.q2 + div.q3 + div.q4;
[Link] = [Link] / 4.0;
}
void displayDivisionData(const Division &div) {
cout << "\nDivision: " << [Link] << "\n";
cout << " Q1 Sales: $" << div.q1 << "\n";
cout << " Q2 Sales: $" << div.q2 << "\n";
cout << " Q3 Sales: $" << div.q3 << "\n";
cout << " Q4 Sales: $" << div.q4 << "\n";
cout << " Total Annual Sales: $" << [Link] << "\n";
cout << " Average Quarterly Sales: $" << [Link] <<
"\n";
}
int main() {
Division east, west, north, south;
[Link] = "East";
[Link] = "West";
[Link] = "North";
[Link] = "South";
inputDivisionData(east);
inputDivisionData(west);
inputDivisionData(north);
inputDivisionData(south);
displayDivisionData(east);
displayDivisionData(west);
displayDivisionData(north);
displayDivisionData(south);
return 0;
}
* Exercise 8.4 : Weather Statistics
Write a program that uses a structure to store the following weather data for a particular
month:
Total Rainfall
High Temperature
Low Temperature
Average Temperature
The program should have an array of 12 structures to hold weather data for an entire year.
When the program runs, it should ask the user to enter data for each month. (The average
temperature should be calculated.) Once the data are entered for all the months, the program
should calculate and display the average monthly rainfall, the total rainfall for the year, the
highest and lowest temperatures for the year (and the months they occurred in), and the
average of all the monthly average temperatures.
Input Validation: Only accept temperatures within the range between –100 and +140
degrees Fahrenheit.
* Exercise 8.5 : Weather Statistics Modification
Modify the program that you wrote for Problem 4 so it defines an enumerated data type
with enumerators for the months (JANUARY, FEBRUARY, etc.). The program should
use the enumerated type to step through the elements of the array.
* * Exercise 8.6 : Speakers’ Bureau
Write a program that keeps track of a speakers’ bureau. The program should use a structure
to store the following data about a speaker:
Name
Telephone Number
Speaking Topic
Fee Required
The program should use an array of at least 10 structures. It should let the user enter data
into the array, change the contents of any element, and display all the data stored in the
array. The program should have a menu-driven user interface.
Input Validation: When the data for a new speaker is entered, be sure the user enters data
for all the fields. No negative amounts should be entered for a speaker’s fee.
* Exercise 8.7 : Search Function for the Speakers’ Bureau Program
Add a function to Problem 6 that allows the user to search for a speaker on a particular
topic. It should accept a key word as an argument and then search the array for a structure
with that key word in the Speaking Topic field. All structures that match should be
displayed. If no structure matches, a message saying so should be displayed.
* Exercise 8.8 : Monthly Budget
A student has established the following monthly budget:
Housing 500.00
Utilities 150.00
Household Expenses 65.00
Transportation 50.00
Food 250.00
Medical 30.00
Insurance 100.00
Entertainment 150.00
Clothing 75.00
Miscellaneous 50.00
Write a program that has a MonthlyBudget structure designed to hold each of these expense
categories. The program should pass the structure to a function that asks the user to enter
the amounts spent in each budget category during a month. The program should then pass
the structure to a function that displays a report indicating the amount over or under in each
category, as well as the amount over or under for the entire monthly budget.
* Exercise 8.9 : Course Grade
Write a program that uses a structure to store the following data:
Member Name Description
Name Student name
Idnum Student ID number
Tests Pointer to an array of test scores
Average Average test score
Grade Course grade
The program should keep a list of test scores for a group of students. It should ask the user
how many test scores there are to be and how many students there are. It should then
dynamically allocate an array of structures. Each structure’s Tests member should point to
a dynamically allocated array that will hold the test scores.
After the arrays have been dynamically allocated, the program should ask for the ID
number and all the test scores for each student. The average test score should be calculated
and stored in the average member of each structure. The course grade should be computed
on the basis of the following grading scale:
Average Test Grade Course Grade
91-100 A
81-90 B
71-80 C
61-70 D
60 or below F
The course grade should then be stored in the Grade member of each structure. Once all
this data is calculated, a table should be displayed on the screen listing each student’s name,
ID number, average test score, and course grade.
Input Validation: Be sure all the data for each student is entered. Do not accept negative
numbers for any test score.
* Exercise 8.10: Drink Machine Simulator
Write a program that simulates a soft drink machine. The program should use a structure
that stores the following data:
Drink Name
Drink Cost
Number of Drinks in Machine
The program should create an array of five structures. The elements should be initialized
with the following data:
Drink Name Cost Number in Machine
Cola .75 20
Root Beer .75 20
Lemon-Lime .75 20
Grape Soda .80 20
Cream Soda .80 20
Each time the program runs, it should enter a loop that performs the following steps: A list
of drinks is displayed on the screen. The user should be allowed to either quit the program
or pick a drink. If the user selects a drink, he or she will next enter the amount of money
that is to be inserted into the drink machine. The program should display the amount of
change that would be returned and subtract one from the number of that drink left in the
machine. If the user selects a drink that has sold out, a message should be displayed. The
loop then repeats. When the user chooses to quit the program it should display the total
amount of money the machine earned.
Input Validation: When the user enters an amount of money, do not accept negative values
or values greater than $1.00.