In: Computer Science
Problem:
You will write a program to compute some statistics based on monthly average temperatures for a given month in each of the years 1901 to 2016. The data for the average August temperatures in the US has been downloaded from the Climate Change Knowledge Portal, and placed in a file named “tempAugData.txt”, available on the class website. The file contains a sequence of 116 values. The temperatures are in order, so that the first one is for 1901, the second is for 1902, and so on through 2016.
The statistics you should compute in your program are:
• The average of the monthly average temperatures for the entire time period.
• The number of years that the monthly average reached at least X degrees where X is a value input from the user. These years should also be displayed to the screen.
• The maximum monthly average temperature for the time period and in what year it occurred.
• The minimum monthly average temperature for the time period and in what year it occurred.
Input: Your program should ask the user for the name of the file, and then open that file for input. It should then ask the user for a boundary temperature (the X in the second bullet above) that is used to calculate some of the statistics. Processing: Compute the statistics requested above.
Output: Display the statistics, labeled, and with the temperatures formatted to 1 decimal place. Also output the count of the years above X before outputting the list of the years.
Sample output:
Please enter the name of the temperature data file: tempAugData.txt Please enter the boundary temperature: 68.0
Climate Data statistics: Average temperature: 66.2
Years that averaged at least 68.0 degrees: 7 1936 1995 2003 2007 2010 2011 2016
Maximum average temperature: 68.9 occurred in 2007
Minimum average temperature: 63.9 occurred in 1927
Additional Requirements:
• Your program must compile and run, otherwise you will receive a 0.
• Your program should test for file open errors.
• I recommend temporarily echoing the input from the file to the screen (using cout) to be sure you are reading the input correctly into your array.
• You should have many separate loops in your program. Do not try to compute everything in one single loop.
• For partial credit, implement some subset of the features completely. This will probably lead to a better score than implementing every feature poorly
USING ARRAYS.
SOURCE CODE IN C++:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
string fileName;
cout << "Enter name of the file: "; //input prompt
cin >> fileName; //input
ifstream in(fileName); //opening file
if(in.is_open()==false)
{
cout << "File not found!" << endl;
return 1;
}
vector<double> temps; //to store the temperatures
double temp,total=0,x; //to input temperature, store sum of
temperatures and store user input temperature
int count=0; //to store number of years with temperature above
x
cout << "Enter the value of x: "; //input prompt
cin >> x; //input
while(in >> temp) //while there is still value in the file,
we read it into temp
{
temps.push_back(temp);
total+=temp;
if(temp>=x)
count++;
}
//output
printf("Average monthly average temperature:
%.1f\n",(total/temps.size()));
printf("Number of years with monthly average temperature above
%.1f: %d,",x,count);
//finding maximum and minimum temperature years
int minY=0,maxY=0;
for(int i=1;i<temps.size();i++)
{
if(temps[i]>temps[maxY])
maxY=i;
if(temps[i]<temps[minY])
minY=i;
if(temps[i]>=x)
printf("%d ",1901+i);
}
//output
printf("\n");
printf("Maximum temperature was %.1f in the year
%d\n",temps[maxY],1901+maxY);
printf("Minimum temperature was %.1f in the year
%d\n",temps[minY],1901+minY);
return 0;
}
OUTPUT:
'