Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

20.2 C++: Calculate salary: Using functions. Separating calculations into functi

ID: 3669832 • Letter: 2

Question

20.2 C++: Calculate salary: Using functions.

Separating calculations into functions simplifies modifying and expanding programs.

The following program calculates the tax rate and tax to pay, using functions. One function returns a tax rate based on an annual salary.

Run the program below with annual salaries of 40000, 60000, and 0.

Change the program to use a function to input the annual salary. The function MUST be called "PromptForInteger" and it must have as an argument a string.   THIS IS WHAT I NEED!!! PLEASE!!!!!!!!!!!!!!

Run the program again with the same annual salaries as above. Are results the same?

#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;

int PromptForInteger(const string userPrompt);

double GetCorrespondingTableValue(int search, vector<int> baseTable, vector<double> valueTable) {
   int baseTableLength = baseTable.size();
   double value = 0.0;
   int i = 0;
   bool keepLooking = true;

   while ((i < baseTableLength) && keepLooking) {
      if (search <= baseTable.at(i)) {
         value = valueTable.at(i);
         keepLooking = false;
      }
      else {
         ++i;
      }
   }

   return value;
}

int main() {
   int annualSalary = 0;
   double taxRate = 0.0;
   int taxToPay = 0;
   int i = 0;
   vector<int> salaryBase(5);
   vector<double> taxBase(5);

   salaryBase.at(0) = 0;
   salaryBase.at(1) = 20000;
   salaryBase.at(2) = 50000;
   salaryBase.at(3) = 100000;
   salaryBase.at(4) = 99999999;

   taxBase.at(0) = 0.0;
   taxBase.at(1) = 0.10;
   taxBase.at(2) = 0.20;
   taxBase.at(3) = 0.30;
   taxBase.at(4) = 0.40;

   // FIXME: Change the input to come from a function
   cout << " Enter annual salary (negative to exit): " << endl;
   cin >> annualSalary;

   while (annualSalary > 0) {
      taxRate = GetCorrespondingTableValue(annualSalary, salaryBase, taxBase);

      taxToPay = static_cast<int>(annualSalary * taxRate); // Truncate tax to an integer amount
      cout << "Annual salary: " << annualSalary <<" "<<
              "Tax rate: " << taxRate << " "<<
              "Tax to pay: " << taxToPay << endl;

      // Get the next annual salary
      // FIXME: Change the input to come from a function
      cout << " Enter annual salary (negative to exit): " << endl;
      cin >> annualSalary ;
   }

   return 0;
}

Submitted: 08:02 pm on 02/19/16

Total: 1/2

Unit test

0/1

Testing if function PromptForInteger is valid...

Compare output

1/1

Input

60000 40000 1000000 -1

Your output

Enter annual salary (negative to exit): Annual salary: 60000 Tax rate: 0.3 Tax to pay: 18000 Enter annual salary (negative to exit): Annual salary: 40000 Tax rate: 0.2 Tax to pay: 8000 Enter annual salary (negative to exit): Annual salary: 1000000 Tax rate: 0.4 Tax to pay: 400000 Enter annual salary (negative to exit):

Input

60000 40000 1000000 -1

Your output

Enter annual salary (negative to exit): Annual salary: 60000 Tax rate: 0.3 Tax to pay: 18000 Enter annual salary (negative to exit): Annual salary: 40000 Tax rate: 0.2 Tax to pay: 8000 Enter annual salary (negative to exit): Annual salary: 1000000 Tax rate: 0.4 Tax to pay: 400000 Enter annual salary (negative to exit):

Explanation / Answer

Yes the results are the same