Bubble Sort...

I need to make a bubble sort that arranges the variable known as "reimbursement" from largest to smallest. I'm not really sure where to go with it.



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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Edward Granger
// 11/16/11
// Dr. Olsen CSCI 207
#include <iostream>
#include <iomanip>
#include <fstream>

#define SIZE 150

using namespace std;

// Psuedocode for using classes

// 1. Open files for input and output. 
// 2. Print file heading. 
// 3. Initialize variables from file. 
// 4. While the file has not reached the end-of-file marker...
//		a. While the employee's meals are greater than or equal to zero...
//			i. Sum up the total of the meals taken by the employee. 
//			ii.	Read the meals the employee has taken. 
//		b. Calculate reimbursement.
//				reimbursement = (miles * 0.43) + lodging + totalmeals
//		c. Print result from file. 
//		d. Re-initilize total of the meals taken by the employee back to zero. 
//		e. Read in new values for the variables. 
// 5. Close files for input and output. 

class expenseData
{
private:
	int employeeid;
	double miles;
	double lodging;
	double totalmeals;
	double reimbursement;

public:

void setemployeeid(int id)
{   employeeid = id;
}

void setmiles(double m)
{
	miles = m;
}

void setlodging(double l)
{
	lodging = l;
}

void settotalmeals(double tm)
{
	totalmeals = tm;
}

void setreimbursement(double r)
{
	reimbursement = r;
}

void calcReimbursement();

int getemployeeid()
{return employeeid; }

double getmiles()
{ return miles; }

double getlodging()
{ return lodging; }

double gettotalmeals()
{ return totalmeals; }

double getreimbursement()
{return reimbursement; }

void readFile( ifstream &inFile); 

void readmeals(double &meals, ifstream &inFile);

//^^^^^when reading from a file rather than printing into one, all variables must be a reference;

void printheading(ofstream &outFile);

void printresult(ofstream &outFile);

}; // end class


void expenseData::printresult(ofstream &outFile)
{
	outFile<<setw(10)<<employeeid<<setw(15)<<miles<<setw(15)<<lodging<<setw(20)<<totalmeals<<setw(25)<<reimbursement<<endl<<endl;

return;
}

int main ()
{
	expenseData e[SIZE];

	int employeeid = 0, sub = 0;
	double miles = 0, meals = 0 , reimbursement = 0, lodging = 0, totalmeals = 0;	
	
	// make sure to initilize the total for a sum, visual basic will not catch this error because the total is located within a data record. 

	ofstream outFile;
		outFile.open("Program5out.out");

	ifstream inFile;
		inFile.open("Program5in.txt");

	e[sub].printheading(outFile);

	outFile<<setprecision(2)<<fixed<<showpoint;

	e[sub].readFile(inFile);

	e[sub].readmeals(meals, inFile);

			while(!inFile.eof() && sub < SIZE)
			{
				while(meals >= 0)
				{
					totalmeals = meals + totalmeals;

					e[sub].readmeals(meals, inFile);	
				}

			e[sub].settotalmeals(totalmeals);

			e[sub].setreimbursement(reimbursement);

			e[sub].calcReimbursement();

			int j, n, i, a, t;

			for(j = 1; j < n; j++)
				for(i = 0; i < n - j; i++)
					if(e[i]  e[i + 1])
						{  // swap
						t = e[i];
						e[i] = e[i + 1];
						e[i+1] = t;
						}
			e[sub].printresult(outFile);
		
			totalmeals = 0;

			sub++;

			e[sub].readFile(inFile);

			e[sub].readmeals(meals, inFile);
		
	}
			// The main program looks much neater with functions haha!

	outFile.close();

	inFile.close();

	return 0;
}

void expenseData::readFile(ifstream &inFile)
{	
	inFile>>employeeid>>miles>>lodging;

	setemployeeid(employeeid);
	setmiles(miles);
	setlodging(lodging);

	//cout<<employeeid<<"   "<<lodging<<endl;


return;
}

void expenseData::readmeals(double &meals, ifstream &inFile)
{

	inFile>>meals;
}

void expenseData::calcReimbursement()
{

	reimbursement = (miles * 0.43) + lodging + totalmeals;

	//cout<<reimbursement<<endl;

return;
}

void expenseData::printheading(ofstream &outFile)
{
	outFile<<setw(80)<<"********************************************************************************"<<endl<<endl;
	outFile<<setw(26)<<"Employee"<<setw(12)<<"Expense"<<setw(11)<<"Report"<<endl;
	outFile<<setw(38)<<"Edward Granger"<<endl<<endl;
	outFile<<setw(38)<<"11-15-11"<<endl<<endl<<endl;
	outFile<<setw(80)<<"********************************************************************************"<<endl<<endl;
	outFile<<setw(10)<<"ID"<<setw(15)<<"Miles"<<setw(15)<<"Lodging"<<setw(20)<<"Meals"<<setw(25)<<"Reimbursement"<<endl<<endl;

	return;
}
Search for "Bubble Sort" and there're tons of sample code on the web.
BTW, you seem to be exposing your real name here. XD
if i remember right write your code to resemble this
put inside a double for loop

for(i=0;i<size;i++;)
{
for(i=0;i<size;i++;)
{
if (e[i].reimbursement < e[i+1].reimbursement)
{
temp=e[i].reimbursement;
e[i].reimbursement=e[i+1].reimbursement;
e[i+1].reimbursement=temp;
}
}
}
put that into a double for loop to go though every variable because it will only move one at a time so you have to do a check for each value. I think that might work
Topic archived. No new replies allowed.