//Reads data and displays a bar graph showing productivity for each plant. #include #include const int NUMBER_OF_PLANTS = 4; void input_data(int a[], int last_plant_number); //Precondition: last_plant_number is the declared size of the array a. //Postcondition: For plant_number = 1 through last_plant_number: //a[plant_number-1] equals the total production for plant number plant_number. void scale(int a[], int size); //Precondition: a[0] through a[size-1] each have a nonnegative value. //Postcondition: a[i] has been changed to the number of 1000s (rounded to //an integer) that were originally in a[i], for all i such that 0 <= i <= size-1 void graph(const int asterisk_count[], int last_plant_number); //Precondition: a[0] through a[last_plant_number-1] have nonnegative values. //Postcondition: A bar graph has been displayed saying that plant //number N has produced a[N-1] 1000s of units, for each N such that //1 <= N <= last_plant_number void get_total(int& sum); //Reads nonnegative integers from the keyboard and //places their total in sum. int round(double number); //Precondition: number >= 0. //Returns number rounded to the nearest integer. void print_asterisks(int n); //Prints n asterisks to the screen. int main( ) { int production[NUMBER_OF_PLANTS]; cout << "This program displays a graph showing\n" << "production for each plant in the company.\n"; input_data(production, NUMBER_OF_PLANTS); scale(production, NUMBER_OF_PLANTS); graph(production, NUMBER_OF_PLANTS); return 0; } //Uses iostream.h: void input_data(int a[], int last_plant_number) { for (int plant_number = 1; plant_number <= last_plant_number; plant_number++) { cout << endl << "Enter production data for plant number " << plant_number << endl; get_total(a[plant_number - 1]); } } //Uses iostream.h: void get_total(int& sum) { cout << "Enter number of units produced by each department.\n" << "Append a negative number to the end of the list.\n"; sum = 0; int next; cin >> next; while (next >= 0) { sum = sum + next; cin >> next; } cout << "Total = " << sum << endl; } void scale(int a[], int size) { for (int index = 0; index < size; index++) a[index] = round(a[index]/1000.0); } //Uses math.h: int round(double number) { return floor(number + 0.5); } //Uses iostream.h: void graph(const int asterisk_count[], int last_plant_number) { cout << "\nUnits produced in thousands of units:\n"; for(int plant_number = 1; plant_number <= last_plant_number; plant_number++) { cout << "Plant #" << plant_number << " "; print_asterisks(asterisk_count[plant_number - 1]); cout << endl; } } //Uses iostream.h: void print_asterisks(int n) { for (int count = 1; count <= n; count++) cout << "*"; }