0

Here's my code:

include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main(){

int ID;
double largest;
double secondLargest;
double sales;
int largestCount = 0;
int secondLargestCount = 0;

ifstream infile ("sales3.txt");
	if (!infile){
		cout << "Error: cannot find input data!\n";
		return 1;
	}

cout << "  Acme Sales Report \n";
cout << "   ID         Sales  \n";
cout << "--------   ----------\n";

infile >> ID >> sales;

largest = sales;
secondLargest = sales;

while (infile){

           if(sales > largest){
		largestCount++;
		largest = sales;
		}
           if (sales < largest && sales > secondLargest) {
		secondLargestCount++;
		secondLargest = sales;
	}
	cout << setw(6) << ID << setw(14)<< fixed << setprecision(2)<< sales << endl;

	infile >> ID >> sales;
}

cout << "The largest sale is: " << largest << " It occurs: " 
     << largestCount << " times." << endl;
cout << "The second largest sale is: " << secondLargest << " It occurs: "
     << secondLargestCount << " times." << endl;

infile.close();
return 0;

}

The title was a bit misleading - I meant to say largest and second largest values - not maximum and minimum values. I know the code is wrong - it can correctly find the maximum values, but doesn't always find the second largest values from a data file, but I'm not sure how to count the occurences of each. I'm positive I need to add 2 more if statements to figure this out, but I'm not sure. Any help would be appreciated.

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by vijayan121
0
#include <iostream>
#include <iomanip>
#include <fstream>
#include <limits>
#include <cmath>
using namespace std;

inline bool is_equal( double x, double y )
{
   const double epsilon = std::numeric_limits<double>::epsilon() ;
   return std::fabs( x-y ) <= epsilon * std::fabs(x) ;
   // see Knuth's The Art of Computer Programming
   // Volume 2 - Seminumerical Algorithms
   // section 4.2.2 pages 217-218
}

int main()
{
  double largest = -1.0 ;
  double second_largest = -1.0 ;
  int largest_count = 0 ;
  int second_largest_count = 0 ;

  ifstream infile("sales3.txt");
  int id ;
  double sales ;
  cout << fixed << setprecision(2) << showpoint ;
  
  while( infile >> id >> sales )
  {
    cout << id << ' ' << sales << '\n' ;
    if( is_equal( sales, largest ) ) ++largest_count ;
    else if( sales > largest )
    {
      second_largest = largest ;
      second_largest_count = largest_count ;
      largest = sales ;
      largest_count = 1 ;
    }
    else if( is_equal( sales, second_largest ) ) ++second_largest_count ;
    else if( sales > second_largest )
    {
      second_largest = sales ;
      second_largest_count = 1 ;
    }
  }
  
  cout << "largest: " << largest << " occurs " << largest_count
       << "times\nsecond largest: " << second_largest << " occurs "
       << second_largest_count << "times\n" ;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.