Determining Tax from a Table - Lab


The Problem

Income tax for individuals is based on two main factors, their income for a particular year and their filing status. Following is a simple tax table that represents the tax owed for individuals with certain incomes and filing statuses:

Filing Status
Income Single Married
Filing
Jointly
Married
Filing
Separately
Head of
Household
$0-$4,999$600$500$600$550
5,000-9,9991,2001,0001,2001,100
10,000-14,9991,8001,5001,8001,650
15,000-19,9992,4002,0002,4002,200
20,000-24,9993,0002,5003,0002,750
25,000-29,9993,6003,0003,6003,300
30,000-34,9994,9005,9505,2505,600
35,000-39,9995,6006,8006,0006,400
40,000-44,9996,3007,6506,7507,200
45,000-49,9997,0008,5007,5008,000
Each row represents a range of incomes. Each column represents a particular filing status.

For example, in order to find the tax owed for a person who made $27,950 and who is filing jointly with their spouse, we find the intersection of the row for incomes between $25,000 and $29,999 and the column for "Married Filing Jointly". The table shows that the tax owed is $3,000.


The Program

We'd like a program that determines the tax a person owes given their income and filing status. We'd also like it to sum all income from different sources (in case a person has income from several jobs or investments).

A run of this program might look like:

% tax
Welcome to the tax calculator.
Enter income amounts from each of your income sources rounded
to the nearest dollar.  Enter 0 (zero) when you are done.

Income: $10000

Income: $2500

Income: $0

Filing Statuses:
        0 = Single
        1 = Married Filing Jointly
        2 = Married Filing Separately
        3 = Head of Household

Enter status: 0

You owe $1800 on your income of $12500

Note that an income of 0 (zero) signals the end of incomes.

The file tax.cpp contains the core of such a tax lookup program.

Representation

Income Amounts

This program uses an array to store the individual income amounts that are entered. In this array, a value of 0 (zero) signals the end of amounts in the array:

Indices 0 1 2 3 4 5 6 7 8 9
Values 10000 2500 0              

Here is how such an array is defined in tax.cpp:

int income_list[MAX_INCOMES];  // Income amounts from different sources

Note how a constant, MAX_INCOMES, is used for the maximum number of amounts that can be stored in the array. Also note that it is not initialized, since the amounts will be read in from the user.

Tax Table

The tax table (see above), with its rows representing ranges of income and its columns representing filing statuses, is a 2-dimensional object. Since all the values in the table are of the same kind, i.e., dollar amounts, it makes sense to represent the table using a 2-dimensional array.

Column Indices
Row
Indices
0 1 2 3
0600500600550
11200100012001100
21800150018001650
32400200024002200
43000250030002750
53600300036003300
64900595052505600
75600680060006400
86300765067507200
97000850075008000
Here we show a schematic for such a 2-dimensional array, where rows and columns must be accessed using integer indices. For example, if this array was called tax_table, we could use tax_table[5][1] to get the amount of tax owed by a person who made $27,950 and who's status is "Married Filing Jointly". In fact, the indices to the table could be more complicated expressions with variables, i.e., tax_table[row_expression][column_expression].

Note that what the user enters for filing status (e.g., 0-3) corresponds directly to the column indices for the table.

In contrast, income does not directly correspond to the row indices in the table. The proper row in the table will have to be calculated based on the income.

Here is how such a table is defined and initialized as a 2-dimensional array in tax.cpp:

int tax_table[NUM_ROWS][NUM_STATUSES] = {
  { 600,  500,  600,  550 },
  { 1200, 1000, 1200, 1100 },
  { 1800, 1500, 1800, 1650 },
  { 2400, 2000, 2400, 2200 },
  { 3000, 2500, 3000, 2750 },
  { 3600, 3000, 3600, 3300 },
  { 4900, 5950, 5250, 5600 },
  { 5600, 6800, 6000, 6400 },
  { 6300, 7650, 6750, 7200 },
  { 7000, 8500, 7500, 8000 }
};

Note how constants have been used for the size of the array (i.e., NUM_ROWS for 10 rows and NUM_STATUSES for 4 columns).

Filing Status Labels

Finally, an additional 1-dimensional array, status_label, is provided so that the filing status names can easily be printed out. You only need to know how to use this array. For example, printing out status_label[1] will display "Married Filing Jointly".

This array has been made global (outside of any function) so that all functions can use it.


Your Task

Parts of the program need to be completed:

  1. void ReadIncome(int income_list[MAX_INCOMES])
    

    This function receives an array in which to place income amounts read from the user. Since arrays are always passed by reference, this function will be able to change the array passed to it. It should prompt the user for income amounts and stop when a 0 (zero) income is entered.

  2. int SumIncome(int income_list[MAX_INCOMES])
    

    This function receives an array holding incomes. The first value of 0 (zero) in the array signals the end of incomes. It should sum up all the incomes in the array and return that sum.

  3. int GetTax(int tax_table[NUM_ROWS][NUM_STATUSES],
               int income,
               int filing_status)
    {
      return tax_table[ /* row index */ ][filing_status];
    }
    

    This function receives the tax table, plus the income and filing status needed to look up the tax. This function is almost complete; it just needs some calculation to determine which row in the table to use for the given income. Hint: How can division (quotients and/or remainders) be used to get the row index from the income?

  4. Error Checking - Several types of errors are not checked by the program. For example, making sure that no more amounts than can be held in the income array are entered. Making sure statuses other than 0-3 are not accepted. Making sure only incomes >=$0 and <$50,000 are looked up in the tax table.
When you are done, compile the completed program, and run it a few times to test it.


BU CAS CS - Determining Tax from a Table - Lab
Copyright © 1993-2000 by Robert I. Pitts <rip at bu dot edu>. All Rights Reserved.