track k largest numbers

Please help me write a constructor in a separate LargestK.cpp file..I have written have included LargestK01.h and maindriver.cpp file.

I have written a program that gets a file name and number of largest entries from the keyboard, then reads the file and reports the largest entries. The program uses the LargestK class and I have written a declaration of that class. Your job is to determine any additional private data members for the LargestK class and write the definitions of a constructor and several methods for the class.




// LargestK01.h

// Declare the LargestK class.
// A LargestK object tracks the k largest doubles sent to
// it.


#ifndef _LARGEST_K_H_
#define _LARGEST_K_H_

#include <vector>
using std::vector;


class LargestK
{
public:
// constructors

// post: this LargestK object is tracking the k largest
// entries sent, via calls to putEntry, by the
// client code. No entries have been sent yet.
explicit LargestK( size_t k );

// mutators

// post: x has been added to the sequence of entries
// processed by this LargestK object.
void putEntry( double x );

// accessors

// post: The number of largest entries tracked by this
// LargestK object has been returned.
size_t getK() const;

// post: The number of entries sent to this LargestK
// object has been returned.
size_t getEntryCount() const;

// pre: 0 <= i < getK() and i < getEntryCount().
// post: The ith largest entry sent to this LargestK
// object has been returned ( i=0 for largest,
// i=1 for next largest, etc. ).
double getLargest( size_t i ) const;

private:

size_t k_;
vector<double> largest_;
};


#endif

// largestKDriver01.cpp

// Get k and file name from keyboard.
// Read file and report k largest doubles.


#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

#include <fstream>
using std::ifstream;

#include <iomanip>
using std::setw;

#include <algorithm>
using std::min;

#include "LargestK01.h"



int main()
{
// Get k and filename;
// Open file; construct LargestK object, largestK.

cout << "Enter file name: ";
string filename;
cin >> filename;
ifstream infile( filename.c_str() );
if( ! infile )
{
cout << filename << " opening failed" << endl;
}

cout << "Enter number of doubles to track: ";
long k;
cin >> k;
LargestK largestK( k );


// Send doubles from file to largestK.

double item;
while( infile >> item )
{
largestK.putEntry( item );
}


// Report k largest doubles.

size_t n = min( largestK.getK(), largestK.getEntryCount() );
for( size_t i=1; i<=n; ++i )
{
cout << setw(4) << i;
cout << setw(12) << largestK.getLargest( i-1 ) << endl;
}

return 0;
}
Topic archived. No new replies allowed.