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

write a C++ program. TAX PROGRAM A prominent senator recently pledged to greatly

ID: 2988188 • Letter: W

Question

write a C++ program.

TAX PROGRAM

A prominent senator recently pledged to greatly simplify the tax laws. His staff came up with the following plan:

1.         The taxable income calculation is based on filing status. For filing status J (married filing jointly), taxable income is equal to gross income minus two standard deductions minus $1,000 for each exemption. For any other filing status, taxable income is equal to gross income minus one standard deduction minus $1,000 for each exemption. The standard deduction is $3,000. The maximum number of allowable exemptions is 12. Taxable income cannot be less than zero; if it is then set it equal to zero.

2.         The tax rate is computed from the following table based on the filing status and the taxable income:

Filing status                            Taxable income                      Tax rate

Single                                      less than $5,000                      15%

                                                $5,000 - $20,000                     22%

                                                more than $20,000                 31%

Married                                   less than $10,000                    15%

                                                $10,000 - $40,000                   22%

                                                more than $40,000                 31%

Married filing jointly              less than $7,000                      17%

                                                $7,000 - 25,000                       24%

                                                more than $25,000                 33%

3.         The amount of tax due is the taxable income times the tax rate.

The program to implement this new tax plan should run in a loop. Each time through the loop, read the following information from a text file:

Taxpayer ID (social security number) - a long integer

Filing status - a single character, must be 'S' for single, 'M' for married, or 'J' for married filing jointly (upper or lower case). Validate incoming data, displaying an error message for records with an invalid filing status.

Gross income - any floating number (negative allowed, representing a loss).

Number of exemptions - an integer between 0 and 12, inclusive. Validate incoming data, displaying an error message for records with an invalid number of exemptions.

For each input record, validate the filing status and number of exemptions and produce an appropriate error message if necessary. For each valid input record, call a function to display the taxpayer ID, taxable income, tax rate, and tax amount. Continue processing until the end of the input file is reached.

After processing all records, display a summary containing the number of taxpayers processed (both valid and invalid records should be included), the average tax amount (include valid records only), the highest tax amount (include valid records only), and the taxpayer ID of the highest tax amount.

111111111s 7000 0
222222222m43500 4
333333333j28152 3
444444444x33509 0
555555555m82000 12
666666666J65197 6
777777777s29000 7
888888888s21543 15
999999999J54308 2
101010101S95602 1
121212121M18525 6
131313131j 8600 3

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>

void tax(long, float, float, float); //the function prototype

int main()
{
   long taxpayerid, hightaxid; // Variable Declaration
   int exempt, number, stdev = 3000, invtax = 0, validtaxpayers = 0;
   float grosinc, taxinc, taxrate, avetax, hightax = 0, taxdue, avetaxtot = 0;
   char status; // S = Single, M = Married, J = Jointly
   FILE *taxproj; // file declaration
   taxproj = fopen("tax.txt","r"); // read file
   if(taxproj == NULL) { //test if file exist
   printf("The tax file does not exist");
   scanf("%9d", &taxpayerid); // Tax id starts the process
   exit(1);// file does not exist
    }
   printf("       Tax Table");
   printf(" Tax Payer ID      Taxable Income      Tax Rate        Tax Amount");
   printf("--------------     ----------- ------       --------");
   while(fscanf(taxproj,"%9d%c%.2f%2d", &taxpayerid, &status, &grosinc, &exempt)!= EOF) {
   number++;
   if(exempt > 12)
   {
   printf("Invalid Number of Exemptions"); // invalid taxpayers
   invtax = invtax + 1;
   continue;
    }
   // filinf status module
   if(status =='S'||status =='s')
       if(taxinc < 5000)
           taxrate = 0.15;
       else
       if(taxinc >= 5000 && taxinc <= 20000)
           taxrate = 0.22;
       else
           taxrate = 0.31;
       else
   // filing status married
   if(status =='M' || status == 'm')
       if(taxinc < 10000)
           taxrate = 0.15;
       else
       if(taxinc >= 10000 && taxinc <= 40000)
           taxrate = 0.22;
       else
           taxrate = 0.31;
       else
   // filing status jointly
   if(status =='J' || status == 'j')
       if(taxinc< 7000)
           taxrate = 0.17;
       else
       if(taxinc >= 7000 && taxinc <= 25000)
           taxrate = 0.24;
       else
           taxrate = 0.33;
       else
   {
        printf("Invalid filing status");
        invtax = invtax + 1;
        continue;
   }
        taxdue = taxinc*taxrate; //calculate taxdue
        if(taxdue > hightax)
        {
           hightax = taxdue;
           hightaxid = taxpayerid;
        }
         // module 4 final calculations
        validtaxpayers+= number - invtax;
        avetaxtot+= taxdue;
  
      // function call
  
  
      tax(taxpayerid, taxinc, taxrate, taxdue); //
     
        avetax = avetaxtot/validtaxpayers;
        printf(" Number of Taxpayers processed: %d", number);
        printf(" Number of valid taxpayers: %d", validtaxpayers);
        printf("]nAverage Tax Amount: %.2f", avetax);
        printf(" High Tax Amount: %.2f for ID: %9d", hightax, taxpayerid);
        fclose(taxproj);
     
        return(0);
      
  
      
       void tax(long tid, float inc, float rate, float due) // function header
       {
           printf("%9d%18.2f%12.2f%18.2f");
          
        }