Am trying to write a structure to store weather data for a particular month.
total rainfall
High temp
Low temp
Average temp

Am trying to calculation but not getting anywhere.
I need help please.
(1)Not sure how to add the 12 rainfall to get the total rainfall and the average rainfall
(2) Add the average temp to get the yearly average temp
(3) Find the highest and the lowest temps and the month they occur in

I have complete the structure for the month but its the calculation is giving me problem

Recommended Answers

All 6 Replies

Post what you have done so far. You will need an array of those structures, one for each month. Then just loop through the array and sum up the values.

This is what I have so far.

This what I have so far

#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
const int NUM_MONTHS = 12;
    struct MonthDat
  int main()
     MonthDat mval[NUM_MONTHS];
     int index;
     char monthname[][NUM_MONTHS] = {"January", "Febuary", "March", "April", "May", "June",
                                   "June", "August", "September", "October", "November", "December" }; 
    double rainfall;            
    double hightemp;           
    double lowtemp;            
    double avgtemp;                                
           cout << "\nData entry for the month\n";
          for (int index = 0; index < 12; index++)
    cout << "Enter the rainfall for " << monthname[index] << ": ";
    cin >> rainfall; 
    cout << "Enter the high temperature for " << monthname[index] << ": ";
    cin >> hightemp;
    cout << "Enter the low temperature for " << monthname[index] << ": ";
    cin >> lowtemp;
    avgtemp = (hightemp + lowtemp) / 2;
    cout << setprecision(2) << fixed;
    cout << "Average Temperature " << avgtemp << endl;     
       return EXIT_SUCCESS;

I suppose the first thing you need to do is complete that structure

struct MonthDat
    double rainfall;
    // etc

>> char monthname[][NUM_MONTHS] = {"January", "Febuary", "March", "April", "May", "June",
"June", "August", "September", "October", "November", "December" };

That's not the right way to do that. Here is a better way, which is simply an array of pointers to strings. You don't have to specify the number of elements in the array -- let the compiler to that for you.

char *monthname[] = {"January", "Febuary", "March", "April", "May", "June",
                                   "June", "August", "September", "October", "November", "December" };

Ok i know i need th structure but i just cant figure out the calculation

If I gave you a pencil and paper and then I gave you 12 numbers that you had to find the average of, how would you do it? I suspect you would add each number to a running total to accumulate a total and once you had the total accumulation then you'd divide by the number of values I gave you. So, model your program after that approach. Declare a variable to act like an accumulator and initialize it to zero outside the loop. Then with each new value you enounter add it to the current total and eventually calculate the average.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.