1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int MAX_OCCUPATIONS = 10;
bool openFile( ifstream& in );
void sortOccupations( occupation myOccupations );
double calcSalaries( double myOccupations.salary[MAX_OCCUPATIONS] );
void printSalaries( occupation myOccupations, double average, double median );
struct occupation
{
char jobs[MAX_OCCUPATIONS][31]; // titles of occupations and number of occupations
double salary[MAX_OCCUPATIONS]; // salary per occupations
};
int main()
{
occupation myOccupations;
char input_file[50];
double average, median; // average and median of all the salaries
ifstream in; // input file
while( openFile( input ) )
{
sortOccupations( myOccupations );
}
calcSalaries( myOccupations.salary );
printSalaries( myOccupations, average, median );
return( 0 );
}
/* openFile: prompts user for file and loads structure
Parameters:
in: ifstream object
Returns: true if input file is successfully opened
*/
bool openFile( ifstream& in )
{
occupation myOccupations; // structure where info is put in
char input_file[50]; // input file used to get info
int i;
// prompt for input file
cout << "Enter the name of the input file\n";
cin.getline( input_file, 50 );
input.open( input_file );
if( input.fail() )
{
cout << "Input file " << input_file << "does not exist\n";
return false;
}
while( i < MAX_OCCUPATIONS && !input.eof() )
{
in.get( myOccupations.jobs[i] );
in.get( myOccupations.salary[i] );
}
}
/* sortOccupations: sorts occupations and puts them in a table
Parameters:
myOccupations: structure that holds all the info
Pre-conditions
Input file was successfully opened
Returns nothing
*/
void sortOccupations( occupation myOccupations )
{
int top, min, temp, i;
ifstream in;
// sort occupations
for( top = 0; top < myOccupations.jobs[i] - 1; top++ )
{
min = top;
for( i = top + 1; i < myOccupations.jobs[i]; i++ )
{
if( myOccupations.jobs[min] > myOccupations.jobs[i] )
{
min = 1;
}
}
swap( temp, myOccupations.jobs[top] );
swap( myOccupations.jobs[top], myOccupations.jobs[min] );
myOccupations.jobs[min] = temp;
}
// put info in table
while( myOccupations.jobs[i] < MAX_OCCUPATIONS )
{
cout << setw(11) << myOccupations.jobs[i] << " $" << setw(11) << myOccupations.salary[$
in >> myOccupations.jobs[i] >> myOccupations.salary[i];
}
}
/* calcSalaries: calculates average and median of salaries
Parameters:
myOccupations.salary: salary of each profession
Pre-Conditions
structure has been loaded with information
Returns: average and median of salaries
*/
double calcSalaries( double myOccupations.salary[MAX_OCCUPATIONS] )
{
double average, median, total;
int n, middle, middle1;
for( int j = 0; j < MAX_OCCUPATIONS; j++ )
{
total = total + myOccupations.salary[j];
}
average = total / myOccupations.salary[j];
if( n % 2 == 0 )
{
middle = n / 2;
middle1 = (n / 2) - 1;
median = (myOccupations.salary[middle] + myOccupations.salary[middle1]) / 2;
}
else
middle = n / 2;
median = myOccupations.salary[median] + 0.5;
}
/* printSalaries: prints the average and median into output file
Parameters:
myOccupations: structure with info
average: average of salaries
median: median of salaries
Pre-conditions:
average and median were successfully calculated
Returns: nothing
*/
void printSalaries( occupation myOccupations, double average, double median )
{
ofstream out;
// write to output file
out.open( "occupations.out" );
out << "Average salary: $ " << average << endl;
out << "Median salary: $ "<< median << endl;
out.close ( );
}
|