Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In c + + why is my output different from the expected output. Please help. Here's my code: #include #include using namespace std; int main
In c why is my output different from the expected output. Please help.
Here's my code:
#include
#include
using namespace std;
int main
double rainfall; Create an array to store rainfall data for each month
string months;
double getTotal ;
double getLargest ;
double getSmallest ;
string largestMonth, smallestMonth;
Loop through each month to collect rainfall data
for int i ; i ; i
bool validInput false;
while validInput
cout "Enter the rainfall in inches for month # monthsi: ;
cin rainfalli;
Input verification: Ensure input is not negative
if rainfalli
cout "Rainfall must be or more." endl;
cout "Please reenter: ;
else
validInput true; If the input is valid, exit the input loop
getTotal rainfalli; Add total monthly rainfail
Check for largest amount of rainfall
if rainfalli getLargest i
getLargest rainfalli;
largestMonth monthsi;
Check for smallest amount of rainfall
if rainfalli getSmallest i
getSmallest rainfalli;
smallestMonth monthsi;
cout endl;
double getAverage getTotal ; Monthly average
Display the requested statistics
cout "The total rainfall for the year is fixed setprecision getTotal inches." endl;
cout "The average rainfall for the year is fixed setprecision getAverage inches." endl;
cout "The largest amount of rainfall was fixed setprecision getLargest inches in month largestMonth endl;
cout "The smallest amount of rainfall was fixed setprecision getSmallest inches in month smallestMonth endl;
cout endl;
cout "Here are the rainfall amounts, sorted in ascending order:" endl;
cout endl;
for int i ; i ; i
for int j i ; j ; j
if rainfallj rainfalli
swaprainfallj rainfalli;
swapmonthsj monthsi;
cout rainfalli endl;
return ;
Im getting an output error and compilation error, please help.
My output:
Enter the rainfall in inches for month #: Rainfall must be or more.
Please reenter: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #:
The total rainfall for the year is inches.
The average rainfall for the year is inches.
The largest amount of rainfall was inches in month
The smallest amount of rainfall was inches in month
Here are the rainfall amounts, sorted in ascending order:
Expected output:
Enter the rainfall in inches for month #: Rainfall must be or more.
Please reenter: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #: Enter the rainfall in inches for month #:
The total rainfall for the year is inches.
The average rainfall for the year is inches.
The largest amount of rainfall was inches in month
The smallest amount of rainfall was inches in month
Here are the rainfall amounts, sorted in ascending order:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started