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

Okay, so I have the following code: #include<iostream> #include<fstream> #includ

ID: 3806773 • Letter: O

Question

Okay, so I have the following code:

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

Now, I need to take this code and modify it. I need to modify the program so that it reads all of the wave-height data and stores it in an array; unordered, then calls the function:

void sort(double x[]. int n);

to sort the data before calculating the WVHT.

Explanation / Answer

#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