Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

08,pa Rainfall analysis The goal is to write a complete C++ program to analyze m

ID: 3727959 • Letter: 0

Question

08,pa Rainfall analysis The goal is to write a complete C++ program to analyze mont Chicago's Midway airpo hly rainfall data. The data we'll use is from rt, but the data could be from any location. The file format consists of an information by N > 0 rows of data. For example, here's the contents of the input file midway-1bxt Chicago_Midway_Airport_2015 2017 2015 1.23 0.24 0.48 2.94 4.13 4.37 2.25 3.46 4.97 1.59 4.14 5.40 2016 0.65 0.21 3.05 2.50 5.56 1.56 4.95 6.42 1.19 4.22 1.66 1.25 2017 2.65 2.40 4.18 6.27 3.77 2.60 4.23 1.90 0.56 5.98 2.33 0.17 Each data row starts with an integer year, followed by 12 real winner d numbers denoting the inches of rainall for each month (Jan, Feb, ., Dec). The program analyzes this data and outputs the following: hicse Mioway,Airport s,30 Progran Average by year (inches) 1. The average rainfall for each year 2016: 2.76833 (-) 2017: 3.8667 () Trends: e, +1,- 2. The trend (s compared to the previous year The average rainfall for each month (across all years) The maximum rainfall amount and date The minimum rainfall amount and date 3. Average by month (inches) an: 1.53 Feb: e.95 Mar: 2.57 4. 5. Apr: 3.99333 Nay: 4.48667 An example program execution is shown to the right, analyzing the file midway-1.txt. Notice that the filename is provided by the user 243 : J.82667 Oet: 3.93 Nov: 2.71 Dec: 2.27333 at the keyboard.

Explanation / Answer


//copy the code and run it
//comment are in between the code
//if have any problem feel free to ask in the comment section
//atleast 3 non trivial functino

//input_function,print_month2string,max_min,avg_by_year

//2 d array input[200][13] for storing the input

// file is open only once


#include <iostream>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <math.h>

using namespace std;

int input_function(char* filename,float input[][13]){

    // return starting_year;
    freopen(filename,"r",stdin);

    string info;
    cin>>info;
    cout<<info;

    int j=0;
    while(1){
        for(int i=0;i<13;i++)
            cin>>input[j][i];
        j++;
        if(feof(stdin))
            break;
    }


    return j;

}

void avg_by_year(float input[][13],int n){

    int i = 0;
    float avg=0;
    float prev=0;
    float diff=0;
    //hash values for trends count
    int hash[3]={0};

    while(i<n){
        for(int j =1;j<13;j++){
            avg+=input[i][j];
        }
        if(i==0){
            cout<<(int)input[i][0]<<": ";
            cout.setf( std::ios::fixed, std:: ios::floatfield ); // floatfield set to fixed
            cout.precision(4);
            cout<<(avg/12)<<endl;
            prev = (avg/12);
        }else{
            diff = (avg/12)-prev;

            //floating point set precision to 0.1
            diff = roundf(diff*10)/10;
          
            //sets sign according to the diff
            string sign;

            if(diff>-0.1 && diff<0.1){sign="(=)";hash[0]++;}
            if(diff>-1.0 && diff<=-0.1){sign="(-)";hash[1]++;}
            if(diff==-1.0 || diff<-1.0)sign="(--)";
            if(diff>=0.1 && diff<1.0){sign="(+)";hash[2]++;}
            if(diff==1.0 || diff>0.1)sign="(++)";

            cout<<(int)input[i][0]<<": "<<(avg/12)<<" "<<sign<<endl;

        }      
    i++;
    avg=0;
    }

    //hash[0] stores no of = sign
    //hash[1] stores no of - sign
    //hash[2] stores no of + sign
    cout<<"Trends: ="<<hash[0]<<", +"<<hash[2]<<", -"<<hash[1]<<endl;
}


void print_month2string(int month){
    if(month==1)cout<<"Jan :";
    else if(month==2)cout<<"Feb :";
    else if(month==3)cout<<"Mar :";
    else if(month==4)cout<<"Apr :";
    else if(month==5)cout<<"May :";
    else if(month==6)cout<<"June :";
    else if(month==7)cout<<"Jul :";
    else if(month==8)cout<<"Aug :";
    else if(month==8)cout<<"Sep :";
    else if(month==10)cout<<"Oct :";
    else if(month==11)cout<<"Nov :";
    else cout<<"Dec :";

}

void avg_by_months(float input[][13],int n){

    float avg=0;
    for(int i =1;i<13;i++){
        int j=0;
        while(j<n){
            avg+=input[j][i];
            j++;
        }
    print_month2string(i);
    cout<<avg<<endl;
    avg=0;
    }
}

//to print the maximum and minimum
void max_min(float input[][13],int n){
    int i=0;
    float max_rainfall = -10000;
    float min_rainfall = 10000;
    int month_max,month_min,year_max,year_min;
    while(i<n){
        for(int j=1;j<13;j++){
            //for findind the maximum
                if(input[i][j]>max_rainfall){
                    max_rainfall = input[i][j];
                    month_max = j;
                    year_max = i;
                }


            //for finding the minimum
            if(input[i][j]<min_rainfall){
                    min_rainfall = input[i][j];
                    month_min = j;
                    year_min = i;
                }      
            }
        i++;
        }

    cout<<"Maximum Rainfall: "<<max_rainfall<<" inches on ";
    print_month2string(month_max);
    cout<<" "<<(int)input[year_max][0]<<endl;

    cout<<"Minimum Rainfall: "<<min_rainfall<<" inches on ";
    print_month2string(month_min);
    cout<<" "<<(int)input[year_min][0]<<endl;
}


int main(int argc, char** argv)
{
  
    int tot_rows_data=0;  

    cout<<"Rainfall Analysis program"<<endl;

    cout<<" ";

    float input[200][13];
    //input_function will read the data from the file and store it into a 2D array and return the total no of rows
    tot_rows_data=input_function(argv[1],input);
  
    cout<<"Average by year(inches)"<<endl;
    avg_by_year(input,tot_rows_data);

    cout<<" ";

    cout<<"Average by month(inches)"<<endl;
    avg_by_months(input,tot_rows_data);  

    cout<<" ";

    max_min(input,tot_rows_data);

    cout<<" ";
    cout<<"**DONE**";
}