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
Head of
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.


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
0 1 2 3
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.