Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #include using namespace std; //Function prototypes double average(double[], int); int main() { //Declare and initialize objectss const int SAMPLE_SIZE = 20;

#include #include #include #include #include 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> 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

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic Net Database Programming

Authors: Rod Stephens

1st Edition

0789726815, 978-0789726810

More Books

Students also viewed these Databases questions