C++. I am using Geany Only allow include <iostream> <fstream> <cstring> <vector>
ID: 3553799 • Letter: C
Question
C++. I am using Geany
Only allow include <iostream> <fstream> <cstring> <vector>
Write a program that reads from a file (ask user to type in the file name) an integer representing the number of data
sets to process. Following that integer value is a series of double values representing the total
rainfall for each of 12 months per data set. Read these values into a vector of doubles. The
program should calculate and display the following information per data set: the total rainfall for
the year, the average monthly rainfall, the months with the highest and lowest amounts, and the
individual monthly rainfall amounts sorted from lowest to highest. Implement the selection sort in
your program. Your output formatting should match the sample output exactly.
Note : the first number tells u how many set, if it said 2. that mean the next 12 numbers is set 1, the next 12 number is set 2.
Sample Input
2
0.48
0.47
0.42
0.21
0.28
0.12
0.35
0.49
0.28
0.21
0.43
0.38
3.49
2.46
2.70
1.15
0.88
0.37
0.50
0.60
0.70
1.83
2.81
3.14
(This file can be found at https://www.dropbox.com/s/usfq3b8htefeq21/rainfall.txt)
Sample Output
Set #1
The total rainfall for the year is 4.12 inches.
The average monthly rainfall for the year is 0.34 inches.
The largest monthly rainfall was 0.49 inches in August.
The smallest monthly rainfall was 0.12 inches in June.
Here are the rainfall amounts, sorted in ascending order:
----------------------------------------------------------
0.12
0.21
0.21
0.28
0.28
0.35
0.38
0.42
0.43
0.47
0.48
0.49
Set #2
The total rainfall for the year is 20.63 inches.
The average monthly rainfall for the year is 1.72 inches.
The largest monthly rainfall was 3.49 inches in January.
The smallest monthly rainfall was 0.37 inches in June.
Here are the rainfall amounts, sorted in ascending order:
----------------------------------------------------------
0.37
0.50
0.60
0.70
0.88
1.15
1.83
2.46
2.70
2.81
3.14
3.49
Explanation / Answer
#include<iostream>
#include<fstream>
#include<vector>
#include<cstring>
using namespace std;
int main()
{
ifstream m;
m.open("test.txt");
int set;
m>>set;
for(int l=1;l<=set;l++)
{
vector <double> a;
double temp;
for(int i=0;i<12;i++)
{
m>>temp;
a.push_back(temp);
}
double min=a[0],max=a[0],avg,sum=0;
for(int i=0;i<12;i++)
{
sum=sum + a[i];
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
avg = sum/12;
for(int i=11;i>=1;i--)
{
for(int j=0;j<i;j++)
{
if(a[j]>a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"Set #"<<l<<endl;
cout<<"The total rainfall for the year is "<<sum<<" inches."<<endl;
cout<<"The average monthly rainfall for the year is "<<avg<<" inches."<<endl;
cout<<"The largest monthly rainfall was "<<max<<" inches."<<endl;
cout<<"The smallest monthly rainfall was "<<min<<" inches."<<endl;
cout<<"Here are the rainfall amounts, sorted in ascending order:"<<endl;
cout<<"----------------------------------------------------------"<<endl;
for(int i=0;i<12;i++)
{
cout<<a[i]<<endl;
}
}
m.close();
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.