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

Modify the program to calculate the WVHT without ordering the array of values,Hi

ID: 3811531 • Letter: M

Question

Modify the program to calculate the WVHT without ordering the array of values,Hint:read and store the "top3rd" values in a new array, then compare each additional value to the smallest value in the new array

below is the example code to be used

/*----------------------------------------------------*/
/* Program chapter7_7                                 */
/* This program inputs wave height data from an       */
/* input file and calculates the significant wave     */
/* height(WVHT).                                      */

#include<iostream> //Required for cin, cout, cerr.
#include<fstream>   //Required for ifstream.
#include<string>    //Required for string.
#include<iomanip>   //Required for setw()
using namespace std;

//Function Prototypes
double average(double[], int);

int main()
{
//Declare and initialize objects
const int SAMPLE_SIZE = 20;
double waveHeights[SAMPLE_SIZE], WVHT, newVal;
int year, month, day, hour, minute;
string filename, header;
ifstream fin;

//Get filname and open file
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);
}
//Read header from input file
getline(fin,header);

//Read first line of input data
int i = 0;
fin >> year >> month >> day >> hour
      >> minute >> waveHeights[i];

//Echo header
cout << header << endl;

//Print starting date and time.
cout << "Starting time: " << endl << year
       << setw(3) << month << setw(3) << day
       << setw(3) << hour << setw(3) << minute << endl;

//Read remaining lines of input
//Order waveHeight in descending order
int pos;
for(i=1; i<SAMPLE_SIZE; ++i)
{

     fin >> year >> month >> day >> hour
         >> minute >> newVal;
     //find ordered position
     pos = 0; //start at top
     while(pos < i && newVal < waveHeights[pos])
     {
       ++pos;
     }
     if(pos == i)
     {
        //newVal belongs at end of array
        waveHeights[i] = newVal;
     }
     else
     {
       //Insert newVal at midpoint in array
       //Move values down to make room
       for(int k=i; k>pos; --k)
       {
         waveHeights[k] = waveHeights[k-1];
       }
       //Assign new value to array
       waveHeights[pos] = newVal;
     }
}//end for

//Calculate the WVHT
//WVHT is defined as the average of the
//the highest one-third of all wave heights.
//Average top 1/3 of array elements.
int top3rd = SAMPLE_SIZE/3;
WVHT = average(waveHeights, top3rd);

//Print ending date and time.
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

Here is the code for you:

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cstdlib>
using namespace std;
//Function prototypes
double average(double[], int);
void sort(double x[], int n);
int main()
{
//Declare and initialize objectss
const int SAMPLE_SIZE = 20;
double waveHeights[SAMPLE_SIZE], WVHT, newVal;
int year, month, day, hour, minute;
string filename, header;
ifstream fin;
  
//Get filename and open file
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);
}
//Read header from input file
//getline(fin.header);
  
//Read first line of input data
int i = 0;
fin >> year >> month >> day >> hour
>> minute >> waveHeights[i];
  
//Echo header
cout << header << endl;
  
//Print starting date and time.
cout << "Starting time: " << endl << year
<< setw(3) << month << setw(3) << day
<< setw(3) << hour << setw(3) << minute
<< endl;

//Read remaining lines of input
//Order waveHeight in descending order
int pos;
for (i=1; i<SAMPLE_SIZE; ++i)
{
fin >> year >> month >> day >> hour
>> minute >> newVal;
  
/*//find ordered position
pos = 0; //start at top
while(pos < i && newVal < waveHeights[pos])
{
++pos;
}
  
if(pos == i)
{
//newVal belongs at end of array*/
waveHeights[i] = newVal; //Simply reads the values to the waveHeights, without sorting.
/*}
  
else
{
//Insert newVal at midpoint in array
//Move values down to make room
for(int k=1; k>pos; --k)
{
waveHeights[k] = waveHeights[k-1];
}
//Assign new value to array
waveHeights[pos] = newVal;
}*/
}//end for
  
sort(waveHeights, SAMPLE_SIZE);   //Now sorts the waveHeight array.
  
//Calculate the WVHT
//WVHT is defined as the average of the
//highest one-third of all wave heights.
//Average top 1/3 of array elements.
int top3rd = SAMPLE_SIZE/3;
WVHT = average(waveHeights, top3rd);
  
//Print ending date and time.
cout << " ending time: " << endl << year
<< setw(3) << month << setw(3) << setw(3) << day
<< setw(3) << hour << setw(3) << minute << endl;
cout << "WVHT is " << WVHT << endl;
  
fin.close();
return 0;
}
/*------------------------------------------------------------*/
/* This function returns the average of the first size */
/* elements in array. */
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;
}
/*------------------------------------------------------------*/
/* This function sorts the waveHeights after reading */
/* elements in array. */
void sort(double x[], int n)
{
    for(int i = 0; i < n-1; i++)
    for(int j = 0; j < n-i-1; j++)
       if(x[j] > x[j+1])
       {
          double temp = x[j];
          x[j] = x[j+1];
          x[j+1] = temp;
       }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote