27.6k views
1 vote
You've been hired by Maple Marvels to write a C++ console application that displays information about the number of leaves that fell in September, October, and November. Prompt for and get from the user three integer leaf counts, one for each month. If any value is less than zero, print an error message and do nothing else. The condition to test for negative values may be done with one compound condition. If all values are at least zero, calculate the total leaf drop, the average leaf drop per month, and the months with the highest and lowest drop counts. The conditions to test for high and low values may each be done with two compound conditions. Use formatted output manipulators (setw, left/right) to print the following rows:________.

September leaf drop
October leaf drop
November leaf drop
Total leaf drop
Average leaf drop per month
Month with highest leaf drop
Month with lowest leaf drop
And two columns:
A left-justified label.
A right-justified value.
Define constants for the number of months and column widths. Format all real numbers to three decimal places. The output should look like this for invalid and valid input:
Welcome to Maple Marvels
------------------------
Enter the leaf drop for September: 40
Enter the leaf drop for October: -100
Enter the leaf drop for November: 24
Error: all leaf counts must be at least zero.
End of Maple Marvels
Welcome to Maple Marvels
------------------------
Enter the leaf drop for September: 155
Enter the leaf drop for October: 290
Enter the leaf drop for November: 64
September leaf drop: 155
October leaf drop: 290
November leaf drop: 64
Total drop: 509
Average drop: 169.667
Highest drop: October
Lowest drop: November
End of Maple Marvels

User Mkrakhin
by
4.8k points

1 Answer

6 votes

Answer:

#include <iostream>

#iclude <iomanip>

#include <algorithm>

using namespace std;

int main(){

int num1, num2, num3,

int sum, maxDrop, minDrop = 0;

float avg;

string end = "\\";

cout << " Enter the leaf drop for September: ";

cin >> num1 >> endl = end;

cout << "Enter the leaf drop for October: ";

cin >> num2 >> endl = end;

cout << "Enter the leaf drop for November: ";

cin >> num3 >> endl = end;

int numbers[3] = {num1, num2, num3} ;

string month[3] = { "September", "October", "November"}

for ( int i =0; i < 3; i++) {

if (numbers[i] < 0) {

cout << "Error: all leaf counts must be at least zero\\";

cout << "End of Maple Marvels\\";

cout << "Welcome to Maple Marvels";

break;

} else if (number[i] >= 0 ) {

sum += number[i] ;

}

}

for (int i = 0; i < 3; i++){

cout << month[i] << " leaf drop: " << numbers[i] << endl = end;

}

cout << "Total drop: " << sum << endl = end;

cout << setprecision(3) << fixed;

cout << "Average drop: " << sum / 3<< endl = end;

maxDrop = max( num1, num2, num3);

minDrop = min(num1, num2, num3);

int n = sizeof(numbers)/sizeof(numbers[0]);

auto itr = find(number, number + n, maxDrop);

cout << "Highest drop: "<< month[ distance(numbers, itr) ] << endl = end;

auto itr1 = find(number, number + n, minDrop);

cout << "Lowest drop: " << month[ distance(numbers, itr1) ] << endl = end;

cout << "End of Maple Marvels";

Step-by-step explanation:

The C++ source code above prompts for user input for three integer values of the leaf drop count through the September to November. The total drop, average, minimum and maximum leaf drop is calculated and printed out.

User Tiwari
by
5.7k points