Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Source: thetripproblem.cpp
- // Author: Jeffrey Cartagena
- // Date: May 2, 2009
- #include <iostream>
- #include <fstream>
- #include <cmath>
- using namespace std;
- // Global variables
- double *allMoney;
- // Prototypes
- void readFromFile(ifstream &inFile, int & qty);
- void writeToFile(ofstream & outFile, double exchagedMoney);
- double calculateExchangedMoney(int qty);
- int main()
- {
- ifstream inFile;
- ofstream outFile;
- int qty;
- try
- {
- inFile.open("input.in");
- outFile.open("output.out");
- }
- catch(...)
- {
- cout << "There are problems opening the files\n";
- cin.ignore();
- return 1;
- }
- readFromFile(inFile, qty);
- while(!inFile.eof())
- {
- double exchangedMoney = calculateExchangedMoney(qty);
- delete[] allMoney;
- writeToFile(outFile, exchangedMoney);
- readFromFile(inFile, qty);
- }
- delete[] allMoney;
- cout << "Finishing the problem\n";
- cin.ignore();
- inFile.close();
- outFile.close();
- return 0;
- }
- double calculateExchangedMoney(int qty)
- {
- double total = 0.0;
- double average = 0.0;
- int count = 0;
- for(int i=0; i < qty; i++)
- average += allMoney[i];
- average = average / qty;
- for(int j=0; j<qty; j++)
- if(allMoney[j] - average > 0)
- {
- total += allMoney[j] - average;
- if(fmod(total, .01) <= 0.009)
- total = total - (fmod(total, .01));
- }
- return total;
- }
- void writeToFile(ofstream & outFile, double exchangedMoney)
- {
- if(fmod(exchangedMoney, 01) >= 0.009)
- outFile << '$' << exchangedMoney << endl;
- else
- outFile << '$' << exchangedMoney << ".00" << endl;
- }
- void readFromFile(ifstream & inFile, int & qty)
- {
- try
- {
- inFile >> qty;
- allMoney = new double[qty];
- for(int i=0; i<qty; i++)
- inFile >> allMoney[i];
- }
- catch(...)
- {
- cout << "There are problems in the format of the input File\n";
- exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement