Answered step by step
Verified Expert Solution
Link Copied!

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[12]={0.00}; // Create an array to store rainfall data for each month
string months[]={"1","2","3","4","5","6","7","8","9","10","11","12"};
double getTotal =0.00;
double getLargest =0.00;
double getSmallest =0.00;
string largestMonth, smallestMonth;
// Loop through each month to collect rainfall data
for (int i =0; i <12; i++){
bool validInput = false;
while (!validInput){
cout << "Enter the rainfall (in inches) for month #"<< months[i]<<": ";
cin >> rainfall[i];
// Input verification: Ensure input is not negative
if (rainfall[i]<0){
cout << "Rainfall must be 0 or more." << endl;
cout << "Please re-enter: ";
} else {
validInput = true; // If the input is valid, exit the input loop
getTotal += rainfall[i]; // Add total monthly rainfail
// Check for largest amount of rainfall
if (rainfall[i]> getLargest || i ==0){
getLargest = rainfall[i];
largestMonth = months[i];
}
// Check for smallest amount of rainfall
if (rainfall[i]< getSmallest || i ==0){
getSmallest = rainfall[i];
smallestMonth = months[i];
}
}
}
}
cout << endl;
double getAverage = getTotal /12.0; // Monthly average
// Display the requested statistics
cout << "The total rainfall for the year is "<< fixed << setprecision(2)<< getTotal <<" inches." << endl;
cout << "The average rainfall for the year is "<< fixed << setprecision(2)<< getAverage <<" inches." << endl;
cout << "The largest amount of rainfall was "<< fixed << setprecision(2)<< getLargest <<" inches in month "<< largestMonth <<"."<< endl;
cout << "The smallest amount of rainfall was "<< fixed << setprecision(2)<< getSmallest <<" inches in month "<< smallestMonth <<"."<< endl;
cout << endl;
cout << "Here are the rainfall amounts, sorted in ascending order:" << endl;
cout <<"----------------------------------------"<< endl;
for (int i =0; i <12; i++){
for (int j = i +1; j <12; j++){
if (rainfall[j]< rainfall[i]){
swap(rainfall[j], rainfall[i]);
swap(months[j], months[i]);
}
}
cout << rainfall[i]<< endl;
}
return 0;
}
Im getting an output error and compilation error, please help.
My output:
Enter the rainfall (in inches) for month #1: Rainfall must be 0 or more.
Please re-enter: Enter the rainfall (in inches) for month #1: Enter the rainfall (in inches) for month #2: Enter the rainfall (in inches) for month #3: Enter the rainfall (in inches) for month #4: Enter the rainfall (in inches) for month #5: Enter the rainfall (in inches) for month #6: Enter the rainfall (in inches) for month #7: Enter the rainfall (in inches) for month #8: Enter the rainfall (in inches) for month #9: Enter the rainfall (in inches) for month #10: Enter the rainfall (in inches) for month #11: Enter the rainfall (in inches) for month #12:
The total rainfall for the year is 78.00 inches.
The average rainfall for the year is 6.50 inches.
The largest amount of rainfall was 12.00 inches in month 12.
The smallest amount of rainfall was 1.00 inches in month 1.
Here are the rainfall amounts, sorted in ascending order:
----------------------------------------
1.00
2.00
3.00
4.00
5.00
6.00
7.00
8.00
9.00
10.00
11.00
12.00
Expected output:
Enter the rainfall (in inches) for month #1: Rainfall must be 0 or more.
Please re-enter: Enter the rainfall (in inches) for month #2: Enter the rainfall (in inches) for month #3: Enter the rainfall (in inches) for month #4: Enter the rainfall (in inches) for month #5: Enter the rainfall (in inches) for month #6: Enter the rainfall (in inches) for month #7: Enter the rainfall (in inches) for month #8: Enter the rainfall (in inches) for month #9: Enter the rainfall (in inches) for month #10: Enter the rainfall (in inches) for month #11: Enter the rainfall (in inches) for month #12:
The total rainfall for the year is 78.00 inches.
The average rainfall for the year is 6.50 inches.
The largest amount of rainfall was 12.00 inches in month 12.
The smallest amount of rainfall was 1.00 inches in month 1.
Here are the rainfall amounts, sorted in ascending order:
----------------------------------------
1.00
2.00
3.00
4.00
5.00
6.00
7.00
8.00
9.00
10.00
11.00
12.00

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

b. What are its goals and objectives?

Answered: 1 week ago

Question

600 lb 20 0.5 ft 30 30 5 ft

Answered: 1 week ago