SHARE
TWEET

Exam 2 Program

a guest May 23rd, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. bool divisibleByThree(int divisor) { // This funcion validates that the given integer is fully divisible by three
  7.     if((divisor % 3) == 0) return true;
  8.     else return false;
  9. }
  10.  
  11. float divide(float operandA, float operandB) { // This function is used to define ratios of price/pound in the betterDeal function
  12.     return (operandA / operandB);
  13. }
  14.  
  15. void betterDeal(float priceOne, float priceTwo, float poundsOne, float poundsTwo) {
  16.     float firstRatio = divide(priceOne, poundsOne), secondRatio = divide(priceTwo, poundsTwo);
  17.     cout << setprecision(2) << fixed;
  18.     cout << "Item 1 costs $" << firstRatio << " per pound, and" << endl;
  19.     cout << "Item 2 costs $" << secondRatio << " per pound." << endl << endl;
  20.     if(firstRatio < secondRatio) {
  21.         cout << "Item 1 is the better deal!" << endl << endl;
  22.     } else if(secondRatio < firstRatio) {
  23.         cout << "Item 2 is the better deal!" << endl << endl;
  24.     } else if(firstRatio == secondRatio) {
  25.         cout << "Both items are the same per pound! They're BOTH a great deal!" << endl << endl;
  26.     }
  27. }
  28.  
  29. int main() {
  30.    
  31.     cout << "Counter-Controlled Loop Demonstration" << endl << endl;
  32.     cout << "This is a counter loop that will display numbers on a line" << endl;
  33.     cout << "based on what lower bound you provide to it." << endl << endl;
  34.     cout << "To start, enter the lower bound as an integer." << endl;
  35.     cout << "For this loop, the upper bound will be preset to 10, " << endl;
  36.     cout << "and all numbers that are divisible by three will be excluded." << endl;
  37.     int lowBound;
  38.     cout << "Enter the lower bound: ";
  39.     cin >> lowBound;
  40.     int highBound = 10;
  41.     cout << endl;
  42.    
  43.     for(int counter = highBound; counter >= lowBound; counter--) { // This counter uses the divisibleByThree function to include values that do not meet the function's conditions
  44.         if(divisibleByThree(counter) == false) {
  45.             cout << counter << " ";
  46.         }
  47.     }
  48.    
  49.     cout << endl << endl << "--------------------" << endl << endl;
  50.    
  51.     cout << "Character-Printing Function Demonstration" << endl << endl;
  52.     cout << "This is a loop that will display the letters of the string" << endl;
  53.     cout << "\"Hello World!\" in order, one line for each letter." << endl << endl;
  54.    
  55.     string message = "Hello World!";
  56.     for(int index = 0; index < message.length(); index++) { // This loop uses the indexes of the given string to print them one-by-one per line
  57.         cout << message[index] << endl;
  58.     }
  59.    
  60.     cout << endl << "--------------------" << endl << endl;
  61.    
  62.     cout << "Better Deal Function Loop" << endl << endl;
  63.     cout << "This is a loop that will run a function that will help you" << endl;
  64.     cout << "find which of two given item-per-cost transactions are the better deal." << endl << endl;
  65.     cout << "Would you like to find the better deal? (y/n): ";
  66.     char input;
  67.     cin >> input;
  68.     while(input == 'Y' || input == 'y') { // As long as the given input is one of these two, the loop will continue to run
  69.         float priceOne, priceTwo, poundsOne, poundsTwo;
  70.         cout << endl << "Wonderful. To begin, enter the price of item 1, in U.S. dollars: $";
  71.         cin >> priceOne;
  72.         cout << "Next, enter its weight, in pounds: ";
  73.         cin >> poundsOne;
  74.         cout << "Now enter the price of item 2, in U.S. dollars: $";
  75.         cin >> priceTwo;
  76.         cout << "And its weight, in pounds: ";
  77.         cin >> poundsTwo;
  78.         cout << endl;
  79.        
  80.         if(priceOne > 0 && priceTwo > 0 && poundsOne > 0 && poundsTwo > 0) { // This condition validates that all given inputs are positive
  81.             betterDeal(priceOne, priceTwo, poundsOne, poundsTwo);
  82.         } else { // ... if one or more is negative, an error occurs.
  83.             cout << "Error: One or more of your given prices or weights is" << endl;
  84.             cout << "less than zero, and in order to run this function," << endl;
  85.             cout << "all values must be greater than zero. Please try again." << endl << endl;
  86.         }
  87.        
  88.         cout << "Would you like to find another great deal? (y/n): "; // Rerun the loop if the char inputs 'Y' or 'y' are met again
  89.         cin >> input;
  90.        
  91.         if(input == 'N' || input == 'n') break; // Inputting the char 'N' or 'n' will terminate the loop
  92.     }
  93.    
  94.     cout << endl << "Thank you for viewing these awesome loop demonstrations!" << endl;
  95.    
  96.     return 0;
  97. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top