Question: WIth command every line of code Write a c++ program that accepts input like the program in Display 5.4 and that outputs a bar graph

WIth command every line of code

Write a c++ program that accepts input like the program in Display 5.4 and that

outputs a bar graph like the one in that program, except that your program will

output the bars vertically rather than horizontally. A two-dimensional array may

be useful.

Display 5.4 Production Graph Program (part 1 of 4)

1 //Reads data and displays a bar graph showing productivity for each plant.

2 #include

3 #include

4 using namespace std;

5 const int NUMBER_OF_PLANTS = 4;

6 void inputData(int a[], int lastPlantNumber);

7 //Precondition: lastPlantNumber is the declared size of the array a.

8 //Postcondition: For plantNumber = 1 through lastPlantNumber:

9 //a[plantNumber-1] equals the total production for plant number

//plantNumber.

10 void scale(int a[], int size);

11 //Precondition: a[0] through a[size-1] each has a nonnegative value.

12 //Postcondition: a[i] has been changed to the number of 1000s (rounded to

13 //an integer) that were originally in a[i], for all i such that 0 <= i

//<= size-1.

14 void graph(const int asteriskCount[], int lastPlantNumber);

15 //Precondition: a[0] through a[lastPlantNumber-1] have nonnegative values.

16 //Postcondition: A bar graph has been displayed saying that plant

17 //number N has produced a[N-1] 1000s of units, for each N such that

18 //1 <= N <= lastPlantNumber

19 void getTotal(int& sum);

20 //Reads nonnegative integers from the keyboard and

21 //places their total in sum.

22 int round(double number);

23 //Precondition: number >= 0.

24 //Returns number rounded to the nearest integer

Display 5.4 Production Graph Program (part 2 of 4)

25 void printAsterisks(int n);

26 //Prints n asterisks to the screen.

27 int main( )

28 {

29 int production[NUMBER_OF_PLANTS];

30 cout << "This program displays a graph showingn"

31 << "production for each plant in the company.n";

32 inputData(production, NUMBER_OF_PLANTS);

33 scale(production, NUMBER_OF_PLANTS);

34 graph(production, NUMBER_OF_PLANTS);

35 return 0;

36 }

37 void inputData(int a[], int lastPlantNumber)

38 {

39 for (int plantNumber = 1;

40 plantNumber <= lastPlantNumber; plantNumber++)

41 {

42 cout << endl

43 << "Enter production data for plant number "

44 << plantNumber << endl;

45 getTotal(a[plantNumber 1]);

46 }

47 }

48 void getTotal(int& sum)

49 {

50 cout << "Enter number of units produced by each department.n"

51 << "Append a negative number to the end of the list.n";

52 sum = 0;

53 int next;

54 cin >> next;

55 while (next >= 0)

56 {

57 sum = sum + next;

58 cin >> next;

59 }

60 cout << "Total = " << sum << endl;

61 }

62

63 void scale(int a[], int size)

64 {

Display 5.4 Production Graph Program (part 3 of 4)

65 for (int index = 0; index < size; index++)

66 a[index] = round(a[index]/1000.0);

67 }

68 int round(double number)

69 {

70 return static_cast(floor(number + 0.5));

71 }

72 void graph(const int asteriskCount[], int lastPlantNumber)

73 {

74 cout << "nUnits produced in thousands of units:nn";

75 for (int plantNumber = 1;

76 plantNumber <= lastPlantNumber; plantNumber++)

77 {

78 cout << "Plant #" << plantNumber << " ";

79 printAsterisks(asteriskCount[plantNumber 1]);

80 cout << endl;

81 }

82 }

83 void printAsterisks(int n)

84 {

85 for (int count = 1; count <= n; count++)

86 cout << "*";

87 }

Sample Dialogue

This program displays a graph showing

Production for each plant in the company.

Enter production data for plant number 1

Enter number of units produced by each department.

Append a negative number to the end of the list.

2000 3000 1000 -1

Total = 6000

Enter production data for plant number 2

Enter number of units produced by each department.

Append a negative number to the end of the list.

2050 3002 1300 -1

Total = 635

Display 5.4 Production Graph Program (part 4 of 4)

Enter production data for plant number 3

Enter number of units produced by each department.

Append a negative number to the end of the list.

5000 4020 500 4348 -1

Total = 13868

Enter production data for plant number 4

Enter number of units produced by each department.

Append a negative number to the end of the list.

2507 6050 1809 -1

Total = 10366

Units produced in thousands of units:

Plant #1 ******

Plant #2 ******

Plant #3 **************

Plant #4 **********

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!