Question

Write a program in C++ that lets the user enter the total rainfall for each of...

Write a program in C++ that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Input validation: Do not accept negative numbers for monthly rainfall figures.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

double sumArray(double[], int);
double averageArray(double[], int);
double maxArray(double[], int, int &);
double minArray(double[], int, int &);
void sortRainfall(double[], int, string[]);
void printRainfall(double[], int, string[]);

int main()
{
   const int MONTHS = 12;   //MONTHS
   double monthlyRainfall[MONTHS];
   string monthName[MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };  
  
   double totalRainfall;
   double averageRainfall;
   double highestRainfall;
   double lowestRainfall;
   int max = 0;
   int low = 0;


   for (int i = 0; i < MONTHS; i++)
   {
       cout << "Enter rainfall in " << monthName[i] << ": ";
       cin >> monthlyRainfall[i];

       while (monthlyRainfall[i] < 0)
       {
           cout << "Rainfall should not be negative." << endl;
           cout << "Reenter the rainfall in " << monthName[i] << ": ";
           cin >> monthlyRainfall[i];
       }
   }

   totalRainfall = sumArray(monthlyRainfall, MONTHS);
   averageRainfall = averageArray(monthlyRainfall, MONTHS);
   highestRainfall = maxArray(monthlyRainfall, MONTHS, max);
   lowestRainfall = minArray(monthlyRainfall, MONTHS, low);


   cout << "Total rainfall: " << totalRainfall << " inches " << endl;
   cout << "Average rainfall: " << averageRainfall << " inches " << endl;
   cout << "Most rainfall in " << monthName[max] << endl;
   cout << "Least rainfall in " << monthName[low] << endl;

   sortRainfall(monthlyRainfall, MONTHS, monthName);
   printRainfall(monthlyRainfall, MONTHS, monthName);

   //system("pause");
   return 0;
}

double sumArray(double totalRainfall[], int n)
{
   double total = 0;
   for (int i = 0; i < n; i++)
       total += totalRainfall[i];

   return total;
}

double averageArray(double totalRainfall[], int n)
{
   double average = 0.0;
   double total = 0;

   for (int i = 0; i < n; i++)
       total += totalRainfall[i];

   average = total / n;

   return average;
}


double maxArray(double totalRainfall[], int n, int &monthIndex)
{
   double high;

   int i = 0;
   high = totalRainfall[i];
   while (i < n)
   {
       if (totalRainfall[i] > high)
       {
           high = totalRainfall[i];
           monthIndex = i;
       }

       i++;
   }

   return high;
}

double minArray(double totalRainfall[], int n, int &monthIndex)
{
   double least;

   int i = 0;
   least = totalRainfall[i];
   while (i < n)
   {
       if (totalRainfall[i] < least)
       {
           least = totalRainfall[i];
           monthIndex = i;
       }

       i++;
   }

   return least;
}


void sortRainfall(double totalRainfall[], int n, string months[])
{
   for (int i = 0; i < n ; i++)
   {
       int minPos = i;  
       double minValue = totalRainfall[i];
       string minMonth = months[i];

       for (int j = i + 1; j < n; j++)
       {
           if (totalRainfall[j] < minValue)  
           {
               minPos = j;
               minValue = totalRainfall[j];
              
               minMonth = months[j];  
           }
       }

       totalRainfall[minPos] = totalRainfall[i];
       totalRainfall[i] = minValue;
       months[minPos] = months[i];
       months[i] = minMonth;
   }
}

void printRainfall(double totalRainfall[], int n, string months[])
{

   for (int i = 0; i < n; i++)
   {
       cout << totalRainfall[i] << " inches in " << months[i] << endl;
   }
   cout << endl;
}

Add a comment
Know the answer?
Add Answer to:
Write a program in C++ that lets the user enter the total rainfall for each of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Rainfall Statistics Write a program that lets the user enter the total rainfall for each of...

    Rainfall Statistics Write a program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November, December....

  • Design a program that lets the user enter the total rainfall for each of 12 months...

    Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amount. PLEASE MODULARIZED THE CODE   PLEASE USE C PROGRAMMING AND ADD PSEUDOCODE

  • The Problem Design a program that lets the user enter the total rainfall for each of...

    The Problem Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the number of months above and below the average rainfall, and the highest rainfall for each quarter of the year. The Assignment Write a Java program that contains five method/functions as follows: main(). Calls the functions enterRain, rainStats, aboveBelow, and quarterlyRain. enterRain(). Uses...

  • Write a Python program that allows the user to enter the total rainfall for each of...

    Write a Python program that allows the user to enter the total rainfall for each of 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January: 8.9 inches February: 11.1 inches March: 13.4 inches April: 6.9 inches May: 8.7 inches June: 9.1 inches July: 15.9 inches August: 4.4 inches September: 3.1 inches October: 5.6 inches November: 7.8...

  • Write a C++ console application that allows your user to capture rainfall statistics. Your program should...

    Write a C++ console application that allows your user to capture rainfall statistics. Your program should contain an array of 12 doubles for the rainfall values as well as a parallel array containing the names of the months. Using each of the month names, prompt your user for the total rainfall for that month. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should...

  • pseudocode and Python Repl.it :Recall that Programming Exercise 3 in Chapter 8 asked you to design...

    pseudocode and Python Repl.it :Recall that Programming Exercise 3 in Chapter 8 asked you to design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Enhance the program so it sorts the array in ascending order and displays the values it contains.

  • Write a program that uses nested loops to collect data and calculate the average rainfall over...

    Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should prompt the user for the number of years. Be sure to ask for each months average rainfall for each year. The outer loop will iterate once for each year while the inner loop will iterate 12 times(one time per month) prompting for the months average rainfall on each iteration. Display the number of months, the total inches...

  • Write a program that scores the total rainfall for each of 12 months into an array...

    Write a program that scores the total rainfall for each of 12 months into an array double. A sample array definition is shown below double thisYear[] = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7}; The program should have the four functions to calculate the following 1. The total rainfall for the year 2. The average monthly rainfall 3. The month with most rain 4. The month with least rain Output: What to deliver? Your .cpp...

  • The following is a program from Starting out with C++ by Toni Gaddis. I am getting the following error messages pertain...

    The following is a program from Starting out with C++ by Toni Gaddis. I am getting the following error messages pertaining to the lines: cin>>month[i].lowTemp; and months[i].avgTemp = (months[i].highTemp + month[i].lowTemp)/2; The error messages read as follows: error C2039: 'lowTemp': is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' and it continues. The second error message is identical. The program is as follows: Ch. 11, Assignment #3. pg. 646. Program #4. //Weather Statistics. //Write a program that uses a structure to store the...

  • qbasic is the programming language Chapter 8 Do Programming Exercise 8-3 om page 414. Create a...

    qbasic is the programming language Chapter 8 Do Programming Exercise 8-3 om page 414. Create a string array to hold the months of the year. Call it anything you want to just make sure it's a string array is at the end of the name, e.g. months). Create a DATA statement that holds the month names: DATA "jan","Feb", "Mar" etc. Next you'll need a FOR loop to READ the DATA into the array, Call a SUB to get the rainfall...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT