Tuesday 9 December 2014

1. Create a class named weather report that holds a daily weather report with data members day_of_month,hightemp,lowtemp,amount_rain and amount_snow. The constructor initializes the fields with default values: 99 for day_of_month, 999 for hightemp,-999 for low emp and 0 for amount_rain and amount_snow. Include a function that prompts the user and sets values for each field so that you can override the default values. Write a C++/Java/Python program that creates a monthly report. a) Menu driven program with options to Enter data and Display report b) Report Format


/*Problem Statement: 

Create a class named weather report that holds a daily weather report with data members day_of_month,hightemp,lowtemp,amount_rain and amount_snow. The constructor initializes the fields with default values: 99 for day_of_month, 999 for hightemp,-999 for low emp and 0 for amount_rain and amount_snow. Include a function that prompts the user and sets values for each field so that you can override the default values. Write a C++/Java/Python program that creates a monthly report.
a) Menu driven program with options to Enter data and Display report
b) Report Format

*/

#include<iostream>
using namespace std;
class weather
{
    int d_o_m;
    float htemp,ltemp,amt_of_rain,amt_of_snow;
public:
    weather();
    void inputdata(int);
    void display();
    void average(weather *w);
};
weather::weather()
{
       htemp=999;
    ltemp=-999;
    amt_of_rain=0;
    amt_of_snow=0;
    d_o_m=99;
}
void weather::inputdata(int d)
{
    d_o_m=d;
    cout<<"Enter high Temp:";
    cin>>htemp;
    cout<<"\nEnter low Temp:";
    cin>>ltemp;
    cout<<"\nEnter Amt of Rain:";
    cin>>amt_of_rain;
    cout<<"\nEnter Amt of Snow:";
    cin>>amt_of_snow;
}
void weather::display()
{
         cout<<"\t"<<d_o_m;
     cout<<"\t\t"<<htemp;
     cout<<"\t"<<ltemp;
     cout<<"\t"<<amt_of_rain;
     cout<<"\t"<<amt_of_snow;
}
void weather::average(weather w[31])
{
    int train,tsnow,tltemp,thtemp;
    float avghtemp,avgltemp,avgrain,avgsnow;
    train=tsnow=tltemp=thtemp=0;
    int i,count=0;
    for(i=1;i<=31;i++)
    {
          if(w[i].d_o_m==90)
        continue;
        else
        {
          thtemp+=w[i].htemp;
        tltemp+=w[i].ltemp;
        train+=w[i].amt_of_rain;
        tsnow+=w[i].amt_of_snow;
        count++;
        }
    }
avghtemp=thtemp/count;
avgltemp=tltemp/count;
avgrain=train/count;
avgsnow=tsnow/count;
cout<<"\nAverage High Temp:"<<avghtemp;
cout<<"\nAverage Low Temp:"<<avgltemp;
cout<<"\nAverage Amount of Rain:"<<avgrain;
cout<<"\nAverage Amount of Snow:"<<avgsnow;
}
int main()
{
  weather data[12][31],temp[31],obj;
  int ch,i,day,month;
  char ans;
cout<<"\nWEATHER REPORT";
do
{
cout<<"\n Main Menu";
cout<<"\n1.Enter Data.";
cout<<"\n2.Display Report.";
cout<<"\n3.Exit.";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
 case 1:
           cout<<"\nEnter the month:";
    cin>>month;
    cout<<"\nEnter the day:";
    cin>>day;
    data[month][day].inputdata(day);
    break;
 case 2:
    cout<<"\nEnter the month:";
    cin>>month;
    cout<<"\n\n\tDay\tDay_o_M\tAmt_Rain\tAmt_Snow\tHigh_temp\tLow_temp";
    for(int i=1;i<=31;i++)
    {
             cout<<"\n"<<i;
        data[month][i].display();
    }
    for(int i=1;i<=31;i++)
    {
        temp[i]=data[month][i];
    }
    obj.average(temp);
    break;
}
cout<<"\nDo u want to continue?";
cin>>ans;
}while(ans=='y');
return 0;
}

6 comments:

  1. How it will be without constructor

    ReplyDelete
    Replies
    1. The compiler by default assumes its own constructor at the time of compilation. Hence we do not need to define it.

      Delete
  2. I want pictorial representation of the object from this program

    ReplyDelete
  3. I want output of the program

    ReplyDelete
  4. very informative blog and useful article thank you for sharing with us , keep posting learn more AngularJS Online Training Bangalore

    ReplyDelete