//(Financial application: compute tax) Rewrite Listing 3.3, ComputeTax.cpp,
//using arrays. For each filing status, there are six tax rates. Each rate is applied
//to a certain amount of taxable income. For example, from the taxable income
//of $400,000 for a single filer, $8,350 is taxed at 10%, (33,950–8,350) at 15%,
//(82,250–33,950) at 25%, (171,550–82,550) at 28%, (372,550–82,250) at
//33%, and (400,000–372,950) at 36%. The six rates are the same for all filing
//statuses, which can be represented in the following array:
//double rates[] = {0.10, 0.15, 0.25, 0.28, 0.33, 0.36};
//The brackets for each rate for all the filing statuses can be represented in a twodimensional array as follows:
//i//nt brackets[4][5] =
//{
// {8350, 33950, 82250, 171550, 372950}, // Single filer
// //{16700, 67900, 137050, 20885, 372950}, // Married jointly
// or qualifying
// widow(er)
// {8350, 33950, 68525, 104425, 186475}, // Married separately
// {11950, 45500, 117450, 190200, 372950} // Head of household
//};
//Suppose the taxable income is $400,000 for single filers. The tax can be computed
//as follows:
//tax = brackets[0][0] * rates[0] +
// (brackets[0][1] – brackets[0][0]) * rates[1] +
//(brackets[0][2] – brackets[0][1]) * rates[2] +
// (brackets[0][3] – brackets[0][2]) * rates[3] +
// (brackets[0][4] – brackets[0][3]) * rates[4] +
// (400000 – brackets[0][4]) * rates[5]
#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>
using namespace std;
double computeTax(int brackets[][5], double rates[],
int status, double income);
int getStatus();
// Driver code
int main()
{
double rates[] = { 0.10, 0.15, 0.25, 0.28, 0.33, 0.36 };
int brackets[4][5] =
{
{8350, 33950, 82250, 171550, 372950}, // Single filer
{16700, 67900, 137050, 20885, 372950}, // Married jointly
// or qualifying
// widow(er)
{8350, 33950, 68525, 104425, 186475}, // Married separately
{11950, 45500, 117450, 190200, 372950} // Head of household
};
cout << "(1-single filer, 2-married jointly, "
<< "or qualifying widow(er), " << endl
<< "3-married separately, 4-head of household)" << endl
<< "Enter the filing status: ";
int status = getStatus();
cout<< "Enter the taxable income: ";
double income;
cin >> income;
cout << "The tax for your income " << income << " is " << computeTax(brackets, rates, status, income);
}
double computeTax(int brackets[][5], double rates[],
int status, double income) {
double tax = 0, incomeTaxed = 0;
for (int i = 4; i >= 0; i--) {
if (income > brackets[status][i])
tax += (incomeTaxed = income - brackets[status][i]) * rates[i + 1];
income -= incomeTaxed;
}
return tax += brackets[status][0] * rates[0];
}
int getStatus() {
int status;
do {
cin >> status;
if (status < 0 || status > 3)
cout<<"Error: invalid status";
} while (status < 0 || status > 3);
return status;
}