1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
//Reads data and displays a bar graph showing productivity for each plant.
#include <iostream>
#include <cmath>
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 has 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: asterisk_count[0] through asterisk_count[last_plant_number− 1]
//have nonnegative values.
//Postcondition: A bar graph has been displayed saying that plant
//number N has produced asterisk_count[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.
double 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 find_max(const int arr[], int number_used);
using namespace std;
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:
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:
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 cmath:
double round(double number)
{
return static_cast<int>(floor(number + 0.5));
}
//Uses iostream:
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]);
int Max;
Max= find_max(asterisk_count, last_plant_number);
for (int i=Max; i>0; i--)
{
cout << asterisk_count[i] << endl;
for (int j=0; j<last_plant_number;j++)
{
asterisk_count[j]= asterisk_count[i];
if (asterisk_count[j]=Max)
cout << "*";
else
cout <<" ";
}
}
cout << endl;
//}
}
int find_max(const int arr[], int number_used)
{
int Max =0;
for (int i=1;i<number_used;i++)
{
if(arr[i]>Max)
Max=arr[i];
}
return Max;
}
//Uses iostream:
void print_asterisks(int n)
{
for (int count = 1; count <= n; count++)
cout << "*";
}
|