Ch2_PracticeAnswers

txt

School

Collin County Community College District *

*We aren’t endorsed by this school

Course

1371

Subject

Computer Science

Date

Dec 6, 2023

Type

txt

Pages

2

Report

Uploaded by DeaconOstrichPerson267

//This file contains four complete programs //Answers to Chapter 2 Practice Questions for Programming I C++ /* 1. Write a program that will compute the total sales tax on a $95 purchase. Assume the state sales tax is 4 percent and the county sales tax is 2 percent. Display the total tax to the screen. Correct output: The total tax on a purchase of $95 is 5.7 */ #include <iostream> using namespace std; int main() { //Choosing to do this program with constants for the taxes const double STATE_TAX = 0.04; const double COUNTY_TAX = 0.02; double purchase = 95; double totalTax = (purchase*STATE_TAX) + (purchase*COUNTY_TAX); cout << "The total tax on a purchase of $" << purchase << " is " << totalTax << endl; return 0; } /* 2. A car holds 15 gallons of gasoline and can travel 375 miles before refueling. Write a program that calculates the number of miles per gallon the car gets. Display the result to the screen. Correct output: The miles per gallon for your car is 25 */ #include <iostream> using namespace std; int main() { //I chose doubles for the variables to avoid potential integer division issues double tankSize = 15; //car holds 15 gallons double distanceLimit = 375; //car can go a max distance of 375 on a tank double mpg = distanceLimit / tankSize; cout << "The miles per gallon for your car is " << mpg << endl; return 0; } /* 3. A car with a 20-gallon gas tank averages 23.5 miles per gallon when driven in town and 28.9 miles per gallon when driven on the highway. Write a program that calculates and displays the distance that the car can travel on one tank of gas when driven in town, and the distance it can travel on one tank of gas when driven
on the highway. Correct output: Your car can go 470 miles for all city driving. Your car can go 578 miles for all highway driving. */ #include <iostream> using namespace std; int main() { double cityAvg = 23.5; double highwayAvg = 28.9; double tankSize = 20; //car has a 20 gallon tank double cityDistance = tankSize * cityAvg; double highwayDistance = tankSize * highwayAvg; cout << "Your car can go " << cityDistance << " miles for all city driving. " << endl; cout << "Your car can go " << highwayDistance << " miles for all highway driving. " << endl; return 0; } /* 4. An electronics company sells circuit boards at a 35% profit. Write a program that will calculate the selling price of a circuit board that costs $14.95 to create. Display the result on the screen. Correct output: The circuit board selling price is $20.1825 */ #include <iostream> using namespace std; int main() { double boardCost = 14.95; double profit = 0.35; //35% represented as a decimal //the total selling price is the profit plus the original cost of the board double sellingPrice = (boardCost*profit) + boardCost; cout << "The circuit board selling price is $" << sellingPrice << endl; return 0; }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help