Modify the program to calculate the WVHT without ordering the array of values. h
ID: 3555069 • Letter: M
Question
Modify the program to calculate the WVHT without ordering the array of values.
hint: read and store "top3rd" values in a new array, then compare each additional value to the smallest value in the new array
data file
##YY MM DD HH MM WH(m)
2011 07 11 03 28 1.23
2011 07 11 03 29 1.27
2011 07 11 03 30 1.29
2011 07 11 03 31 1.51
2011 07 11 03 32 1.72
2011 07 11 03 33 1.85
2011 07 11 03 34 2.01
2011 07 11 03 35 2.12
2011 07 11 03 36 1.92
2011 07 11 03 37 1.71
2011 07 11 03 38 1.32
2011 07 11 03 39 1.26
2011 07 11 03 40 1.21
2011 07 11 03 41 1.02
2011 07 11 03 42 1.12
2011 07 11 03 43 1.24
2011 07 11 03 44 1.27
2011 07 11 03 45 1.29
2011 07 11 03 46 1.33
2011 07 11 03 47 1.73
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
double average(double[], int);
int main()
{
const int SAMPLE_SIZE = 20;
double waveHeights [SAMPLE_SIZE], WVHT, newVal;
int year, month, day, hour, minute;
string filename, header;
ifstream fin;
cout << "Enter name of input file: ";
cin >> filename;
fin.open(filename.c_str());
if(fin.fail())
{
cerr << "Could not open the file " << filename
<< "Goodbye." << endl;
exit (1);
}
getline(fin,header);
int i = 0;
fin >> year >> month >> day >> hour >> minute >> waveHeights [i];
cout << header << endl;
cout << "Starting time: " <<endl << year << setw(3) << month << setw(3) << day << setw(3) << hour << setw(3) << minute << endl;
int pos;
for(i=1; i<SAMPLE_SIZE; ++i)
{
fin >> year >> month >> day >> hour >> minute >> newVal;
pos = 0;
while(pos < i && newVal < waveHeights[pos])
{
++pos;
}
if(pos == i)
{
waveHeights[i] = newVal;
}
else
{
for (int k=i; k>pos; --k)
{
waveHeights[k] = waveHeights [k-1];
}
waveHeights[pos] = newVal;
}
}
int top3rd = SAMPLE_SIZE/3;
WVHT = average (waveHeights, top3rd);
cout << "ending time: " << endl << year
<< setw(3) << month << setw(3) << day
<< setw(3) << hour << setw(3) << minute << endl;
cout << "WVHT is " << WVHT << endl;
fin. close();
return 0;
}
double average(double array[], int size)
{
double sum = 0.0;
for(int i=0; i<size; ++i)
{
sum += array[i];
}
sum = sum/size;
return sum;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
double average(double[], int);
int main()
{
const int SAMPLE_SIZE = 20;
double waveHeights [SAMPLE_SIZE], WVHT, newVal;
int year, month, day, hour, minute;
string filename, header;
ifstream fin;
cout << "Enter name of input file: ";
cin >> filename;
freopen("filename.txt","r",stdin);
if(fin.fail())
{
cerr << "Could not open the file " << filename
<< "Goodbye." << endl;
exit (1);
}
getline(fin,header);
int i = 0;
cin >> year >> month >> day >> hour >> minute >> waveHeights [i];
cout << "Starting time: " <<endl << year <<" "<< setw(3) <<" "<< month <<" "<< setw(3) <<" "<< day <<" "<< setw(3) <<" "<< hour <<" "<< setw(3) <<" "<< minute << endl;
int pos;
string k;
for(i=1; i<SAMPLE_SIZE; ++i)
{
cin>>k>>k>>k>>k>>k>>k;
cin>> year >> month >> day >> hour >> minute >> newVal;
pos = 0;
while(pos < i && newVal < waveHeights[pos])
{
++pos;
}
if(pos == i)
{
waveHeights[i] = newVal;
}
else
{
for (int k=i; k>pos; --k)
{
waveHeights[k] = waveHeights [k-1];
}
waveHeights[pos] = newVal;
}
}
int top3rd = SAMPLE_SIZE/3;
WVHT = average (waveHeights, top3rd);
cout << "ending time: " << endl << year <<" "<< setw(3) <<" "<< month <<" "<< setw(3) <<" "<< day <<" "<< setw(3) <<" "<< hour <<" "<< setw(3) <<" "<< minute << endl;
cout << "WVHT is " <<" "<< WVHT << endl;
fin. close();
return 0;
}
double average(double array[], int size)
{
double sum = 0.0;
for(int i=0; i<size; ++i)
{
sum += array[i];
}
sum = sum/size;
return sum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.