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

#include<iostream> #include<fstream> #include<string> #include<iomanip> #include

ID: 3808036 • Letter: #

Question

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cstdlib>
using namespace std;

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

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;
       }
      
       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
  
   //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;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

Modify the program so that it reads all of the wave-height data and stores it in an array, unordered, then calls the function: (This function needs to sort the data before calculating the WVHT.)

/*----------------------------------------------*/
/* This function sorts an array with n elements */
/* into ascending order. */

void sort(double x[], int n)
{
   //Declare objects
   int m;
   double hold;
  
   //implement selection sort algorithm.
   for (int k=0; k<=n-2; ++k)
   {
       //Find position of smallest value in array
       //beginning at k
       m = k;
       for (int j=k+1; j<=n-1;   ++j)
       {
           if (x[j] < x[m])
               m=j;
       }
       //Exchange smallest value with value at k
       hold = x[m];
       x[m] = x[k];
       x[k] = hold;
   }
   //Void return
   return;
}
/*----------------------------------------------*/

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cstdlib>
using namespace std;

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

int main()
{
    //Declare and initialize objectss
    void sort(double[], int);
    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 >> waveHeights[i];
    }//end for
    //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.
    sort(waveHeights,SAMPLE_SIZE);
    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;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/*Modify the program so that it reads all of the wave-height data and stores it in an array, unordered, then
*calls the function: (This function needs to sort the data before calculating the WVHT.)
*/
/*----------------------------------------------*/
/* This function sorts an array with n elements */
/* into ascending order.                        */

void sort(double x[], int n)
{
    //Declare objects
    int m;
    double hold;
  
    //implement selection sort algorithm.
    for (int k=0; k<=n-2; ++k)
    {
        //Find position of smallest value in array
        //beginning at k
        m = k;
        for (int j=k+1; j<=n-1;    ++j)
        {
            if (x[j] > x[m])
                m=j;
        }
        //Exchange smallest value with value at k
        hold = x[m];
        x[m] = x[k];
        x[k] = hold;
    }
    //Void return
    return;
}
/*----------------------------------------------*/

I have edited the code. The code is now much simple and clean. I have made insertion simpler and sorted the array after insertion. In case you have any doubt, please comment below.