// PROGRAM: dcheung_agegroup.cpp // PROGRAMMER: David Cheung // DATE: 11/28/06 // PURPOSE: Prints an age group distribution table and bar graph // INPUT: Age // OUTPUT: Program function // Exit instruction // The age group distribution table // Age distribution bar graph // DESCRIPTION: /* Algorithm: * * Declare const int constOldest * Declare int varAge, loopHighestBar, loopXaxis, loopYaxis, * loopBarCountTop/Bottom and loopRangeSymbol variables * Declare int varAgeCount[13] array * * ** Variable input section ** * * Display Program function * Display Instructions for input and exit * * Input varAge * * while (varAge >= 0) * { * Check for input error first, just to make sure * if (varAge <= constOldest) * varAgeCount[varAge/10] and increment by one * else * Display error message * Ask for another input or exit * } * * ** Display age group and frequency distribution ** * * Display age group, setw(), and varAgeCount[1 through 13] * * ** Display age group distribution bar graph ** * * To calculate the maximum height of the graph: * for (loopXaxis = 2; loopXaxis <= 12; loopXaxis++) * { * if (varAgeCount[loopXaxis] > loopHighestBar) * { * loopHighestBar = varAgeCount[loopXaxis]; * } * } * * And then display the graph: * for (loopYaxis = loopHighestBar; loopYaxis >= 1; loopYaxis--) * { * Display the Y axis; * for (loopXaxis = 0; loopXaxis <= 12; loopXaxis++) * { * if (varAgeCount[loopXaxis] >= loopYaxis) print a spaced out * * else print spaces only * } * } * * Display the bottom X axis "------" * Display the bottom labels using: * do * { * Display loopBarCountTop and then increment by 10 * } while (loopBarCountTop < 120) * Display the "|" symbols using a for loop * and do * { * Display loopBarCountBottom and then increment by 10 * } while (loopBarCountBottom < 129) * Display the axis label and end program */ #include #include using namespace std; // Declare all the constants and variables const int constOldest = 129; // Age limit, no older than this age is allowed int varAge, // Individual ages go here loopHighestBar, // This helps set the bar graph size by setting it to the tallest bar loopXaxis, // This constitutes the X-axis on the bar graph loopYaxis, // This constitutes the Y-axis on the bar graph loopBarCountTop = 0, // Define loopBarCountBottom = 9, // the Age Groups loopRangeSymbol; // axis labels int varAgeCount[13]; // Age group frequency counter array int main(int argv, char *argc[]) { // Program starts here cout << "Age Group Distribution" << endl << endl; cout << "Ages are in groups of 10." << endl; cout << "Highest possible age is 129." << endl << endl; cout << "*****Please enter a negative number when done.*****" << endl << endl; cout << "Please enter the first age -> "; cin >> varAge; // This loop checks to see if the user enters a negative number (to exit) while (varAge >= 0) { // Check for input error while (!cin) { cout << "Invalid input. Please enter a valid age -> "; cin.clear(); cin.ignore(2000,'\n'); cin >> varAge; } if (varAge <= constOldest) // Is the entered age larger than the Oldest human? { varAgeCount[varAge/10]++; // Add one to the appropriate counter } else // Too old! { cout << "Age is too big - ignore." << endl; } cout << "Please enter another age (neg. to quit) -> "; cin >> varAge; // Enter another age and check again ^_^ } // This section displays the columns Age Group and Frequency cout << endl << "Age Group Frequency" << endl; cout << "--------- ---------" << endl; cout << " 0 - 9" << setw(24) << varAgeCount[0] << endl; cout << " 10 - 19" << setw(24) << varAgeCount[1] << endl; cout << " 20 - 29" << setw(24) << varAgeCount[2] << endl; cout << " 30 - 39" << setw(24) << varAgeCount[3] << endl; cout << " 40 - 49" << setw(24) << varAgeCount[4] << endl; cout << " 50 - 59" << setw(24) << varAgeCount[5] << endl; cout << " 60 - 69" << setw(24) << varAgeCount[6] << endl; cout << " 70 - 79" << setw(24) << varAgeCount[7] << endl; cout << " 80 - 89" << setw(24) << varAgeCount[8] << endl; cout << " 90 - 99" << setw(24) << varAgeCount[9] << endl; cout << "100 - 109" << setw(24) << varAgeCount[10] << endl; cout << "110 - 119" << setw(24) << varAgeCount[11] << endl; cout << "120 - 129" << setw(24) << varAgeCount[12] << endl; cout << "Smack any key to continue ..."; cin.ignore(); cin.get(); // Pause it (a platform-agnostic way of doing it) cout << endl << endl; // This is the part about the bar graph cout << "Age Distribution Bar Graph" << endl << endl; loopHighestBar = varAgeCount[1]; // Initialize this variable for (loopXaxis = 2; loopXaxis <= 12; loopXaxis++) // Calculate the tallest { if (varAgeCount[loopXaxis] > loopHighestBar) // bar and set it to { loopHighestBar = varAgeCount[loopXaxis]; // this variable } } for (loopYaxis = loopHighestBar; loopYaxis >= 1; loopYaxis--) // Creates the Y axis { cout << endl << setw(4) << loopYaxis << " |"; // Display the Y axis for (loopXaxis = 0; loopXaxis <= 12; loopXaxis++) // Loop to create { if (varAgeCount[loopXaxis] >= loopYaxis) // the asterisks to { cout << " *"; // display the bars } else { cout << " "; } } } cout << endl << " +----------------------------------------------------" << endl; // Draw the X axis cout << " "; // Position the cursor for the next operation do // Display the 0 range { cout << setw(4) << loopBarCountTop; loopBarCountTop = loopBarCountTop + 10; } while (loopBarCountTop <= 120); cout << endl << " "; // Position the cursor for the next operation for (loopRangeSymbol = 0; loopRangeSymbol < 13; loopRangeSymbol++) // Display the | symbol { cout << setw(4) << "|"; } cout << endl << " "; // Position the cursor for the next operation do // Display the 9 range { cout << setw(4) << loopBarCountBottom; loopBarCountBottom = loopBarCountBottom + 10; } while (loopBarCountBottom <= 129); cout << endl << endl << " Age Groups" << endl; return 0; } // Finito