const int LOW_END = 10; // low end of incomes considered const int HIGH_END = 100; // high end of incomes considered const int TABLE_SIZE = HIGH_END - LOW_END + 1; typedef int tableType[TABLE_SIZE]; int Index(int Group) // Returns the array index that corresponds to Group number. { return Group - LOW_END; } // end Index void ReadData(tableType IncomeData, int Low, int High, bool& DataError) // --------------------------------------------------------- // Reads and organizes income statistics. // Precondition: The calling program gives directions and // prompts the user. Each input line contains exactly two // integers in the form G N, where N is the number of // people with an income in the G-thousand-dollar group and // Low <= G <= High. An input line with values of zero // for both G and N terminates the input. // Postcondition: IncomeData[G-Low] = total number of people // with an income in the G-thousand-dollar group. The values // read are displayed. If either G or N is erroneous (G and // N are not both 0, and either G < Low, G > High, or // N < 0), the function ignores the data line, sets // DataError to tRUE, and continues. In this case, the // calling program should take action. DataError is fALSE // if the data is error free. // --------------------------------------------------------- { int Group, Number; // input values DataError = false; // no data error found as yet for (Group = Low; Group <= High; ++Group) // clear array IncomeData[Index(Group)] = 0; for (cin >> Group >> Number; (Group != 0) || (Number != 0); cin >> Group >> Number) { // Invariant: Group and Number are not both 0 cout << "Input line specifies that income group " << Group << "\ncontains " << Number << " people.\n"; if ((Group >= Low) && (Group <= High) && (Number >= 0)) // input data is valid -- add it to tally IncomeData[Index(Group)] += Number; else // error in input data: set error flag and // ignore input line DataError = true; } // end for } // end ReadData