(C++) Using an input file similar to the one below, write a program to determine
ID: 3768139 • Letter: #
Question
(C++) Using an input file similar to the one below, write a program to determine the highest concentration recorded and the number of days the photochemical oxidant concentration exceeded the standard; display number of days and percentage (based on the number of days per month). Use at least two programmer defined functions.
Sample Input file:
80 83 79 180 190 200 220 220 200 200 198 225 199 183 160 119
162 180 122 121 121 119 80 83 79 119 121 121 122 119
Implementation:
The number of days in the month are determined from input file. The input file can vary in size depending on the month.
Once array is populated, the contents of the array can not be changed.
At least one function must have an array as a parameter.
Sample output:
Enter input file name =>list.dat
The highest reading recorded was 225 micrograms/m^3
What level is the federal standard =>160
During this month, the federal standard for photochemical oxidants was exceeded on 13 days.
Percentage of days for month is 43.3333%
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int maxreading(int arr[],int n){
int max=INT_MIN;
for(int i=0;i<n;i++){
if(arr[i]>max){
max=arr[i];
}
}
return max;
}
int exedays(int arr[],int n,int val){
int count=0;
for(int i=0;i<n;i++){
if(arr[i]>val){
count++;
}
}
return count;
}
int main(){
ifstream inf;
string s;
cout<<"Enter input file name =>";
cin>>s;
inf.open(s.c_str());
int count=0;
int x;
vector<int> vec;
while(inf>>x){
vec.push_back(x);
count++;
}
int arr[count];
for(int i=0;i<count;i++){
arr[i]=vec[i];
}
cout<<"The highest reading recorded was "<<maxreading(arr,count)<<" micrograms/m^3"<<endl;
cout<<"What level is the federal standard =>";
int fedstd;
cin>>fedstd;
int exdays=exedays(arr,count,fedstd);
cout<<"During this month, the federal standard for photochemical oxidants was exceeded on "<<exdays<<" days"<<endl;
cout<<"Percentage of days for month is "<<(exdays*1.0)/(count*1.0)*100.0<<"%"<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.