C++ (a) Compute the median of data file. The median is the number that has the s
ID: 3763036 • Letter: C
Question
C++ (a) Compute the median of data file. The median is the number that has the same number of data elements greater than the numbers as there are less than the number. For purposes of this problem, you are to assume that the data is sorted (that is, is in increasing order). The median is the middle element of the file if there are an odd number of elements, or the average of the two middle elements if the file has an even number of elements. You will need to open the file, count the members, close the file and calculate the location of the middle of the file, open the file again (recall the “start over” discussion in this chapter], count up to the file entries you need, and calculate the middle. While testing construct several files on your own, including one with an even number of data points, increasing, and one with an odd number, also increasing. When you are confident your program is correct, modify it to read data from a file called dataMedianQuartiles
(b) For a sorted file, a quartile is one of three numbers: The first has one fourth the data values less than or equal to it, one-fourth the data values between the first and second numbers, one-fourth the data points between the second and the third, and one-fourth above the third quartile. 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 your job done. (You have the second quartile already.) You also should recognize that you have done almost all the work toward finding the other two quartiles as well.
Explanation / Answer
#include <set>
#include <iostream>
using namespace std;
int main()
{
typedef pair< unsigned int, double > Data;
typedef std::set< Data > DataSet;
typedef DataSet::const_iterator DataSetCIter;
DataSet mySet;
mySet.insert( Data(5, 1.23f) );
mySet.insert( Data(1, 3.33f) );
mySet.insert( Data(3, 9.39f) );
mySet.insert( Data(3, 9.29f) );
for ( DataSetCIter it = mySet.begin(); it != mySet.end(); ++it )
{
cout << "unsigned int = " << it->first << " "
<< "double = " << it->second << " "
<< endl;
}
return 0;
}
b.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.