Thursday, 15 November 2018

Getting Started with C++ | Tony Gaddis | Chapter 2 | Problem Solutions | Asad Ali


All the solutions to the problems of the Chapter 2 out of book "Getting Started With C++ by Tony Gaddis"

Problem 1
/*
Sum of Two Numbers
Write a program that stores the integers 62 and 99 in variables, and stores the sum of
these two in a variable named total.
*/
#include <iostream>
using namespace std;
int main(){
int num1 = 62,
num2 = 99,
total;

total = num1 + num2;

cout << "Sum of 62 and 99 is " << total << endl;
return 0;
}
Problem 2
/*
Sales Prediction
The East Coast sales division of a company generates 62 percent of total sales. Based
on that percentage, write a program that will predict how much the East Coast division
will generate if the company has $4.6 million in sales this year.
*/
#include <iostream>
using namespace std;
int main(){
double per_TotalSales = 0.62,   // 62 %
totalSales = 4600000;      // 4.6 million dollar
int profit;

profit = totalSales * per_TotalSales;

cout << "East Cost Sales Divsion of company generates : " << profit << endl;
return 0;
}
Problem 3
/*
Sales Tax
Write a program that will compute the total sales tax on a $52 purchase. Assume the
state sales tax is 4 percent and the county sales tax is 2 percent.
*/
#include <iostream>
using namespace std;
int main(){
double state_tax = 0.04,
contry_tax = 0.02,
purchase = 52,
total_salesTax;

total_salesTax = ( state_tax + contry_tax ) * purchase;

cout << "The total sales tax on 52$ purchase is : " << total_salesTax << endl;
return 0;
}
Problem 4
/*
Restaurant Bill
Write a program that computes the tax and tip on a restaurant bill for a patron with a
$44.50 meal charge. The tax should be 6.75 percent of the meal cost. The tip should
be 15 percent of the total after adding the tax. Display the meal cost, tax amount, tip
amount, and total bill on the screen.
*/
#include <iostream>
using namespace std;
int main(){
double meal_charge = 44.50,
tax_per = 0.0675,
tip_per = 0.15,
tax_amount,
meal_cost_tax,
tip_amount,
total_meal_cost;

tax_amount = meal_charge * tax_per;
meal_cost_tax = meal_charge + tax_amount;
tip_amount = meal_cost_tax * tip_per;
total_meal_cost = meal_cost_tax + tip_amount;

cout << "Meal Cost : " << total_meal_cost << endl;
cout << "Tax Amount : " << tax_amount << endl;
cout << "Tip Amount : " << tip_amount << endl;
return 0;
}
Problem 5
/*
Average of Values
To get the average of a series of values, you add the values up and then divide the sum by
the number of values. Write a program that stores the following values in five different
variables: 28, 32, 37, 24, and 33. The program should first calculate the sum of these five
variables and store the result in a separate variable named sum. Then, the program
should divide the sum variable by 5 to get the average. Display the average on the screen.
*/
#include <iostream>
using namespace std;
int main(){
double num1 = 28, num2 = 32, num3 = 37, num4 = 24, num5 = 33, sum, average;

sum = num1 + num2 + num3 + num4 + num5;
average = sum / 5;

cout << "Average : " << average << endl;
return 0;
}
Problem 6
/*
Annual Pay
Suppose an employee gets paid every two weeks and earns $1700.00 each pay period. In a
year the employee gets paid 26 times. Write a program that defines the following variables:
payAmount This variable will hold the amount of pay the employee earns each pay
period. Initialize the variable with 1700.0.
payPeriods This variable will hold the number of pay periods in a year. Initialize the
variable with 26.
annualPay This variable will hold the employee’s total annual pay, which will be calculated.
The program should calculate the employee’s total annual pay by multiplying the
employee’s pay amount by the number of pay periods in a year, and store the result in
the annualPay variable. Display the total annual pay on the screen.
*/
#include <iostream>
using namespace std;
int main(){
int payAmount = 1700,
payPeriods = 26,
annualPay;

annualPay = payAmount * payPeriods;

cout << "Annual Pay of the Employee is : " << annualPay << endl;
return 0;
}
Problem 7
/*
Ocean Levels
Assuming the ocean’s level is currently rising at about 1.5 millimeters per year, write a
program that displays:
• The number of millimeters higher than the current level that the ocean’s level will
be in 5 years
• The number of millimeters higher than the current level that the ocean’s level will
be in 7 years
• The number of millimeters higher than the current level that the ocean’s level will
be in 10 years
*/
#include <iostream>
using namespace std;
int main(){
double rise_each_year = 1.5, five_year_later, seven_year_later, ten_year_later;
five_year_later = rise_each_year * 5;
seven_year_later = rise_each_year * 7;
ten_year_later = rise_each_year * 10;

cout << "The number of millimeters higher than the current level that the ocean’s level after 5 years is : " << five_year_later << endl;
cout << "The number of millimeters higher than the current level that the ocean’s level after 5 years is : " << seven_year_later << endl;
cout << "The number of millimeters higher than the current level that the ocean’s level after 5 years is : " << ten_year_later << endl;
return 0;
}
Problem 8
/*
Total Purchase
A customer in a store is purchasing five items. The prices of the five items are:
Price of item 1 = $12.95
Price of item 2 = $24.95
Price of item 3 = $6.95
Price of item 4 = $14.95
Price of item 5 = $3.95
Write a program that holds the prices of the five items in five variables. Display each
item’s price, the subtotal of the sale, the amount of sales tax, and the total. Assume
the sales tax is 6%.
*/
#include <iostream>
using namespace std;
int main(){
float item1 = 12.95,
item2 = 24.95,
item3 = 6.95,
item4 = 14.95,
item5 = 3.95,
sales_tax_per = 0.06,
subTotal = item1 + item2 + item3 + item4 + item5,
sales_tax_amount,
total;

sales_tax_amount = sales_tax_per * subTotal;
total = sales_tax_amount + subTotal;

cout << "Price of item 1 : " << item1 << endl;
cout << "Price of item 2 : " << item2 << endl;
cout << "Price of item 3 : " << item3 << endl;
cout << "Price of item 4 : " << item4 << endl;
cout << "Price of item 5 : " << item5 << endl;
cout << "Sub Total : " << subTotal << endl;
cout << "Total Sales Tax : " << sales_tax_amount << endl;
cout << "Total amount : " << total << endl;
return 0;
}
Problem 9
/*
Cyborg Data Type Sizes
You have been given a job as a programmer on a Cyborg supercomputer. In order to
accomplish some calculations, you need to know how many bytes the following data
types use: char, int, float, and double. You do not have any manuals, so you can’t
look this information up. Write a C++ program that will determine the amount of
memory used by these types and display the information on the screen.
*/
#include <iostream>
using namespace std;
int main(){
cout << "Size of data type char is (bytes) : " << sizeof(char) << endl;
cout << "Size of data type int is (bytes) : " << sizeof(int) << endl;
cout << "Size of data type float is (bytes) : " << sizeof(float) << endl;
cout << "Size of data type double is (bytes) : " << sizeof(double) << endl;
return 0;
}
Problem 10
/*
Miles per Gallon
A car holds 12 gallons of gasoline and can travel 350 miles before refueling. Write a
program that calculates the number of miles per gallon the car gets. Display the result
on the screen.
Hint: Use the following formula to calculate miles per gallon (MPG):
MPG = Miles Driven / Gallons of Gas Used
*/
#include <iostream>
using namespace std;
int main(){
int gallons_stored = 12,
miles_traveled = 350,
miles_per_gallon;

miles_per_gallon = miles_traveled / gallons_stored;

cout << "Miles Per Gallon of The Car : " << miles_per_gallon << endl;
return 0;
}
Problem 11
/*
Distance per Tank of Gas
A car with a 20-gallon gas tank averages 21.5 miles per gallon when driven in town
and 26.8 miles per gallon when driven on the highway. Write a program that calculates
and displays the distance the car can travel on one tank of gas when driven in
town and when driven on the highway.
Hint: The following formula can be used to calculate the distance:
Distance = Number of Gallons × Average Miles per Gallon
*/
#include <iostream>
using namespace std;
int main(){
double gallons = 20,
miles_per_gallon = 21.5,
distance_traveled;

distance_traveled = gallons * miles_per_gallon;

cout << "Distance Traveled by Car: " << distance_traveled << endl;
return 0;
}
Problem 12
/*
Land Calculation
One acre of land is equivalent to 43,560 square feet. Write a program that calculates
the number of acres in a tract of land with 389,767 square feet.
*/
#include <iostream>
using namespace std;
int main(){
long int acre = 43560, // int can only store 32,767 so we used "long int"
land = 389767;
double numberOfAcres;
numberOfAcres = land / acre;
cout << "Number of Acres : " << numberOfAcres << endl;
return 0;
}
Problem 13
/*
Circuit Board Price
An electronics company sells circuit boards at a 40 percent profit. Write a program
that will calculate the selling price of a circuit board that costs $12.67. Display the
result on the screen.
*/
#include <iostream>
using namespace std;
int main(){
double profit_per = 0.4,
profit_price,
circuit_cost = 12.67,
total_price;

profit_price =  profit_per * circuit_cost;
total_price = circuit_cost + profit_price;

cout << "Total circuit price :  " << total_price << endl;
return 0;
}
Problem 14
/*
Personal Information
Write a program that displays the following pieces of information, each on a separate line:
Your name
Your address, with city, state, and ZIP code
Your telephone number
Your college major
Use only a single cout statement to display all of this information.
*/
#include <iostream>
using namespace std;
int main(){
cout << "Asad Ali\nJohar Town, Lahore, Punjab, 5400\n+923043388800\nUniversity of Lahore -DRC" << endl;
return 0;
}
Problem 15
/*
Triangle Pattern
Write a program that displays the following pattern on the screen:
   *
  ***
 *****
*******
*/
#include <iostream>
using namespace std;
int main(){
cout << "Triangle Pattern" << endl;
cout << endl;
cout << "   *   " << endl;
cout << "  ***  " << endl;
cout << " ***** " << endl;
cout << "*******" << endl;
return 0;
}
Problem 16
/*
Diamond Pattern
Write a program that displays the following pattern:
   *
  ***
 *****
*******
 *****
  ***
   *
*/
#include <iostream>
using namespace std;
int main(){
cout << "Diamond Pattern" << endl;
cout << endl;
cout << "   *   " << endl;
cout << "  ***  " << endl;
cout << " ***** " << endl;
cout << "*******" << endl;
cout << " ***** " << endl;
cout << "  ***  " << endl;
cout << "   *   " << endl;
return 0;
}
Problem 17
/*
Stock Commission
Kathryn bought 600 shares of stock at a price of $21.77 per share. She must pay her
stock broker a 2 percent commission for the transaction. Write a program that calculates
and displays the following:
• The amount paid for the stock alone (without the commission)
• The amount of the commission
• The total amount paid (for the stock plus the commission)
*/
#include <iostream>
using namespace std;
int main(){
double number_shares = 600,
price_per_share = 21.77,
commission_per = 0.02,
subtotal_price = number_shares * price_per_share,
commission_price,
total_price;

commission_price = commission_per * subtotal_price;
total_price = commission_price + subtotal_price;

cout << "The amount paid for stock alone : " << subtotal_price << endl;
cout << "The amount of commission : " << commission_price << endl;
cout << "The total amount paid : " << total_price << endl;
return 0;
}
Problem 18
/*
Energy Drink Consumption
A soft drink company recently surveyed 12,467 of its customers and found that
approximately 14 percent of those surveyed purchase one or more energy drinks per
week. Of those customers who purchase energy drinks, approximately 64 percent of
them prefer citrus flavored energy drinks. Write a program that displays the following:
• The approximate number of customers in the survey who purchase one or more
energy drinks per week
• The approximate number of customers in the survey who prefer citrus flavored
energy drinks
*/
#include <iostream>
using namespace std;
int main(){
double total_dirinks = 12467,
energy_per = 0.14,
citrus_per = 0.64,
nEnergy = energy_per * total_dirinks,
nCitrus = citrus_per * total_dirinks;
cout << "The approximate number of customers in the survey who purchase one or more energy drinks per week is : " << nEnergy << endl;
cout << "The approximate number of customers in the survey who prefer citrus flavored energy drinks : " << nCitrus << endl;
return 0;
}

Share:

0 comments:

Post a Comment

Thanks for visiting my Blog.

Have any problem, feel free to contact us via Email: asadalipkp9@gmail.com

Have a nice day!

padacode.blogspot.com

Powered by Blogger.