Sie sind auf Seite 1von 3

(a) Compute the median of a data file.

The median is the number that has the same number of data elements greater than the number as there are less than the number. For purposes of this problem, you are to assume that the data is sorted (i.e., is in increasing order). Then median is the middle element of the file i f there are an odd number of elements, or the average of the two middle elements is the file has an even number of elements. You will need to open the file, cou nt the members, close the file and calculate the location of the middle of the f ile, open the file again, count up to the file entries you need and calculate th e middle. (b) For a sorted file, a quartile is one of three numbers; the first has 1 t he data values less than it, 1 the data values between the first and second numb ers, 1 the data points between the second and the third, and 1 above the third q uartile. Find the three quartiles for the data file you used for part(a). Hint: you should recognize that having done part (a) you have one third of y ou job done. (You have the second quartile already). You also should recognize t hat you have done almost all the work toward finding the other two quartiles as well. This program requires a bit of explaining. I first generated two files with different number of random numbers. One has an even number and the other has an odd number (I named them even.dat and odd.dat so I can remember them). I then so rted both files ascendingly. By the way I wrote a program that did all this for me, let me know if you a copy of it. I then ran the program "ch5p3.exe" twice fo r each of the data files. I passed the files (one at a time) to main() via the c ommand line (DOS prompt). /*** ch5p3.cpp ***/ #include <fstream.h> #include <stdlib.h> #include <conio.h> void topic();

// file streams, cout and cin too. // exit() // clrscr(); // print program topic

void main(int argc, char* argv[]) { ifstream inFile; // create file stream object int num, temp = 0; unsigned int median=0, firstQuartile = 0, thirdQuartile = 0; if (argc < 2 || argc > 2) { clrscr(); cerr << << << << "This program requires a file to be passed as\n" "an argument to " << argv[0] << endl "This program was tested and debugged at the\n" "command line (DOS Prompt).\n\n";

cerr << "I generated two files with random numbers,\n" << "one with even number of elements and the\n" << "other one with odd number of elements.\n\n"; cerr << "Sample Usage:\n\n" << '\t' << argv[0] << " even.dat" << endl; cerr << "\n\nPress any key to continue...";

getch(); clrscr(); exit(1); } else inFile.open(argv[1]); if (inFile.fail()) { cerr << argv[1] << " does not exist!" << endl; exit(1); } /*** count how many elements in file ***/ for (int count = 0; inFile >> num; count++) ; inFile.close(); // close file

cout << "\"" << argv[1] << "\" data file has " << count << " elements.\n\n"; if (count % 2) // if number of elements is odd { inFile.open(argv[1]); while (inFile >> num) { temp++; cout << num << endl;

// display the numbers to screen

if (temp == count / 4 + 1) firstQuartile = num; else if (temp == (count / 2 + 1)) median = num; else if (temp == count * 3 / 4 + 1) thirdQuartile = num; } // end while inFile.close(); cout << "\t The Median is " << median << endl; cout << "\t The 1st Quartile is " << firstQuartile << endl; cout << "\t The 2nd Quartile is " << median << endl; cout << "\t The 3rd Quartile is " << thirdQuartile << endl; } // end if else // else if number of elements is even { inFile.open(argv[1]); while(inFile >> num) { temp++; cout << num << endl;

// display numbers to screen

if ((temp == count / 4) || (temp == count / 4 + 1)) firstQuartile += num; else if ((temp == count / 2) || (temp == count / 2 + 1)) median += num; else if ((temp == count * 3 / 4) || (temp == count * 3 / 4 + 1))

thirdQuartile += num; } // end while inFile.close(); cout cout cout cout << << << << "\t "\t "\t "\t The The The The // close file Median is " << (median / 2) << endl; 1st Quartile is " << (firstQuartile / 2) << endl; 2nd Quartile is " << (median / 2) << endl; 3rd Quartile is " << (thirdQuartile / 2) << endl;

} // end else } // end main() /////////////////////////////////////////////////////////////////////

Das könnte Ihnen auch gefallen