document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //4.13: Gas Mileage
  2. //Develop a C++ program that uses a WHILE statement to input the miles driven and gallons used for each trip.
  3.  
  4. //include statements
  5. #include <iostream>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. //initialize variable for miles driven to 0.
  11. unsigned int milesDriven{0};
  12. //initialize variable for total miles driven to 0.
  13. unsigned int milesTotal{0};
  14. //initialize variable for gallons used to 0.
  15. unsigned int gallonsUsed{0};
  16. //initialize variable for total gallons used to 0.
  17. unsigned int gallonsTotal{0};
  18.  
  19. unsigned int counter{0};
  20.  
  21. int main(){
  22. //while input value is greater than -1:
  23. do {
  24.         //prompt user to input number of miles driven.
  25.         cout << "Enter miles driven (-1 to quit): ";
  26.         cin >> milesDriven; //obtain user input
  27.         if (milesDriven == -1) {
  28.              cout << "MPG calculation complete." << endl;
  29.         } else {
  30.         //prompt user to input gallons used.
  31.         cout << "Enter gallons used: ";
  32.         cin >> gallonsUsed;
  33.         //add to milestotal and gallonstotal
  34.         milesTotal = milesTotal + milesDriven;
  35.         gallonsTotal = gallonsTotal + gallonsUsed;
  36.         //turn mpg into a double and calculate
  37.         double mpgTrip{static_cast<double>(milesDriven) / gallonsUsed};
  38.         //display mpg
  39.         cout << "MPG this trip: " << mpgTrip << endl;
  40.         //turn total mpg into a double
  41.         double mpgTotal{static_cast<double>(milesTotal) / gallonsTotal};
  42.         //display total mpg
  43.         cout << "Total MPG: " << mpgTotal << endl;
  44.         counter = counter + 1;
  45.         }
  46. } while (milesDriven!= -1);
  47. }
  48. //This should allow the user to input miles and gallons, receive an average per trip,
  49. //and total average mpg. When the user presses -1 as miles driven, the program will cancel. And it did!
  50.  
  51. //4.17 : Find the Largest
  52. //write a c++ program that uses a while statement to determine the largest of 10 numbers input by the user.
  53.  
  54. #include <iostream>
  55. #include <string>
  56. using namespace std;
  57.  
  58. //initialize variables:
  59.     unsigned int counter{1}; //to count to 10, initialize to 1
  60.     unsigned int number{}; //to input the current number into the program
  61.     unsigned int largest{};
  62.  
  63. int main() {
  64. //while counter is less than or equal to 10:
  65. std::cout << "Please enter 10 numbers." << std::endl;
  66.  
  67. while (counter <= 10) {
  68.         //prompt user to input an integer
  69.         std::cout << "Number " << counter << ": " << endl;
  70.         std::cin >> number; //obtain user input
  71.         if (number > largest) {
  72.             largest = number;
  73.         }
  74.         counter++;
  75. }
  76. std::cout << "The largest number is: " << largest << std::endl;
  77. }
  78. //This program should prompt the user 10 times, and output the largest number inputted at the end of the program.
');