Sorting Parallel Arrays

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void SelectionSort (string Customer [], double Charge [], int counter)
{
	int i;
	int j;
	int min;

	for (i = 0; i < counter - 1; i++)
	{
		min = i;

		for (j = i + 1; j < counter; j++)
		{
			if (Customer [j] < Customer [min])
			{
				min = j;
				
				Customer [i].swap (Customer [min]);
				Charge [i].swap (Charge [min]);
			}
		}
	}
}


If I do this, it is yelling at me about "error C2228: left of '.swap' must have class/struct/union"
And highlighting "Charge" on the left side of the .swap
closed account (o3hC5Di1)
That's because the std::string class has a swap function defined:
http://www.cplusplus.com/reference/string/string/swap/

But double as a datatype does not - [edit: see ResidentBiscuit's post below]

Hope that makes sense.

All the best,
NwN
Last edited on
std::string has a swap function, but I think you're wanting to use the std::swap function in the algorithm header.

http://www.cplusplus.com/reference/algorithm/swap/

They are used differently and do slightly different things.
Unfortunately I am still not finding anything that works. It isn't making sense to me how I would swap data in the second array to match the order in the first array.
Do the swapping yourself:
1
2
3
4
5
6
7
string tempString = Customer[i];
Customer[i] = Customer[j];
Customer[j] = tempString;

double tempDouble = Charge[i];
Charge[i] = Charge[j];
Charge[j] = tempDouble
Last edited on
but for the assignment, i have to use selection sort with parallel arrays.
That is why I am having so much trouble. I can sort the first array (Customer), but I am having a hard time figuring out how to keep them aligned. Currently, it is yelling at me for swapping the customer array, and then attempting to swap the charge array, because the class type is double.
Like lowest one said, do the swap yourself. You don't have to use std::swap. This is a relatively basic problem. What issue are you having right now?
What LowestOne said works, I guess I just didn't understand that I could do that myself. I assumed because the example problem he gave us used swap....i had to as well. This makes a lot more sense, and works perfectly.
Great thing about programming, you can write anything you want.
It is good to try to keep things at the level your teacher is teaching though.

Another thing you can do is make another array to keep track of the order. I posted the code, you may have seen it, but when I read over it it seemed like it didn't work.

You have something like this:
1
2
3
4
5
6
7
8
9
10
int arrInt[6] = {3,4,2,5,1,6};
int arrIdx[6] = {0,1,2,3,4,5};

// When you sort, sort the arrIdx
// When it's done, arrIdx would look like this:
arrIdx = {4,2,0,1,3,5};

// Then go through the loop like so:
for (int i = 0; i < 6; i++)
  cout << arrInt[arrIdx[i]] << endl;


Bonus here is that you never change the parallel arrays. You could have hundreds of them and you never need to make your sorting loop more complicated.
Last edited on
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

// global constants
const int MAX_CUSTOMERS = 1000;

// function prototypes
bool OpenAndValidateInputFile (ifstream& textIn);
void ReadAndStoreInputFile (ifstream& textIn, string Customer[], double Charge[], int& counter);
void SelectionSort (string Customer[], double Charge [], int counter);
bool OpenAndValidateOutputFile (ofstream& textOut);
//void WriteToOutputFile (ofstream& textOut, string Customer [], double Charge [], int counter);
//void DisplayChargesForCustomers (ofstream& textOut, int counter, string Customer []);

//********************************************************************* 
//  FUNCTION: main
//  DESCRIPTION: XXXX
//  INPUT: 
//      Parameters: XXXX
//      File: XXXX
//  OUTPUT:  
//      Return Val: XXXX
//      Parameters: XXXX
//      File: XXXX
//  CALLS TO: XXXX
//**********************************************************************
int main()
{
	// local variables
	ifstream textIn;														// input file stream variable
	ofstream textOut;
	string Customer [MAX_CUSTOMERS];
	double Charge [MAX_CUSTOMERS];
	int counter = 0;

	if (OpenAndValidateInputFile (textIn))
	{
		ReadAndStoreInputFile (textIn, Customer, Charge, counter);

		SelectionSort (Customer, Charge, counter);

		OpenAndValidateOutputFile (textOut);

		//WriteToOutputFile (textOut, Customer, Charge, counter);

		cout << Customer [0] << endl;
		cout << Customer [1] << endl;
		cout << Customer [2] << endl;
		cout << Customer [3] << endl;
		cout << Customer [4] << endl;
		cout << Customer [5] << endl;
		cout << Customer [6] << endl;
		cout << Customer [7] << endl;
		cout << Customer [8] << endl;

		//DisplayChargesForCustomers (textOut, counter, Customer);

		system ("PAUSE");
		return 0;
	}
	else
	{
		return 1;
	}

	// CLOSE FILE!!!!!!
}

//********************************************************************* 
//  FUNCTION: OpenAndValidateInputFile
//  DESCRIPTION: Function opens data file CUSTOMERS.TXT and validates that
//               it can be open.  If succesful, function returns true.
//				 If unsuccessful, function returns false in order to
//				 exit program from main with a return code 1.
//  INPUT: 
//      Parameters: textIn - CUSTOMERS.TXT file
//      File: None
//  OUTPUT:  
//      Return Val: None
//      Parameters: None
//      File: None
//  CALLS TO: main function calls OpenAndValidateInputFile
//**********************************************************************
bool OpenAndValidateInputFile (ifstream& textIn)
{
    // open input file
    textIn.open ("CUSTOMERS.TXT");

	// check if file opens successfully
	// if not, print a warning and exit program from main with a return code 1
	if (!textIn.is_open ())
	{
		return false;
	}
	else
	{
		return true;
	}
}// end OpenAndValidateInputFile

//********************************************************************* 
//  FUNCTION: ReadAndStoreInputFile
//  DESCRIPTION: Function reads data from CUSTOMERS.TXT and stores data
//				 appropriately according to customerCode and utilityCharge
//				 in parallel arrays Customer[] and Charge[]
//  INPUT: 
//      Parameters: textIn - CUSTOMERS.TXT file
//      File: None
//  OUTPUT:  
//      Return Val: None
//      Parameters: Customer[] - array for stored customer codes
//					Charge[] - array for stored utility charges
//      File: None
//  CALLS TO: main function calls ReadAndStoreInputFile
//**********************************************************************
void ReadAndStoreInputFile (ifstream& textIn, string Customer[], double Charge[], int& counter)
{
	string customerCode;													// customer ID code - unique 4 capital letters followed by 4 digits
	double utilityCharge = 0;												// utility charge per unit

	// while file is still readable, and peek does not find end-of-file
	while (textIn.good () && textIn.peek () != EOF)
	{
		// read in data from CUSTOMERS.TXT
		textIn >> customerCode;
		textIn >> utilityCharge;

		// if the file contains too many lines of data, stop reading data and issue warning
		if (counter >= MAX_CUSTOMERS)
		{
			cout << "Error: file contains too many lines of data" << endl;
		}
		else
		{
			Customer [counter] = customerCode;

			Charge [counter] = utilityCharge;

			counter++;
		}
			
		// read newline
		textIn.get ();
	}
}// end ReadAndStoreInputFile

//********************************************************************* 
//  FUNCTION: SelectionSort
//  DESCRIPTION: Function sorts Customer [] in descending order based on
//				 customer code (Z -> A) and aligns parallel arrays. 
//  INPUT: 
//      Parameters: XXXX
//      File: XXXX
//  OUTPUT:  
//      Return Val: XXXX
//      Parameters: XXXX
//      File: XXXX
//  CALLS TO: main function calls SelectionSort
//**********************************************************************
void SelectionSort (string Customer [], double Charge [], int counter)
{
	// local variables
	int i;																	// integer place holder i
	int j;																	// integer place holder j
	int max;																// place holder for highest letter found for sort

	// setting parameter for i
	for (i = 0; i < counter - 1; i++)
	{
		max = i;

		// setting parameter for j
		for (j = i + 1; j < counter; j++)
		{
			// searching through array, and replacing lower value higher value if found
			if (Customer [j] > Customer [max])
			{
				max = j;

				string tempString = Customer[i];
				Customer[i] = Customer[j];
				Customer[j] = tempString;

				// aligning Charge [] with Customer []
				double tempDouble = Charge[i];
				Charge[i] = Charge[j];
				Charge[j] = tempDouble;
			}
		}
	}
} // end SelectionSort

//********************************************************************* 
//  FUNCTION: OpenAndValidateOutputFile
//  DESCRIPTION: Function opens output data file SORTED.TXT and validates
//				 that it can be open.  If succesful, function returns true.
//				 If unsuccessful, function returns false in order to
//				 exit program from main with a return code 1.
//  INPUT: 
//      Parameters: textOut - SORTED.TXT file
//      File: None
//  OUTPUT:  
//      Return Val: None
//      Parameters: None
//      File: None
//  CALLS TO: main function calls OpenAndValidateOutputFile
//**********************************************************************
bool OpenAndValidateOutputFile (ofstream& textOut)
{
    // open input file
    textOut.open ("SORTED.TXT");

	// check if file opens successfully
	// if not, print a warning and exit program from main with a return code 1
	if (!textOut.is_open ())
	{
		return false;
	}
	else
	{
		return true;
	}
}// end OpenAndValidateOutputFile

//********************************************************************* 
//  FUNCTION: WriteToOutputFile
//  DESCRIPTION: XXXX
//  INPUT: 
//      Parameters: XXXX
//      File: XXXX
//  OUTPUT:  
//      Return Val: XXXX
//      Parameters: XXXX
//      File: XXXX
//  CALLS TO: main function calls WriteToOutputFile
//**********************************************************************
/*void WriteToOutputFile (ofstream& textOut, string Customer [], double Charge [], int counter)
{
	// write each instance to output file for Customer [] and Charge []
	for (int i = 0; i < counter; i++)
	{
		textOut << Customer [i] << showpoint << fixed << setprecision (2) << setw (8) << Charge [i] << endl;
	}
}// end WriteToOutputFile

void DisplayChargesForCustomers (ofstream& textOut, int counter, string Customer [])
{
	cout << "CUSTOMER CODES on file are: " << endl;

	for (int i = 0; i < counter; i++)
	{
		cout << Customer [i] << "    ";
	}

	cout << endl << endl;
}*/
So far this is my entire code...I thought I had everything working correctly, so I moved forward with the directions. The next part of the assignment asked me to take the sorted arrays and write them to an output file called SORTED.TXT, which I did. Everything was working perfectly then as well. The next step asked me to display the customer codes on the console screen horizontally, which I did...and it worked correctly. However, my issue, the assignment shows 10-15 customer codes along with utility charges...my input file only had 4, so i opened my CUSTOMER.TXT input file, and added a few so I now have 8. When i ran the program again...nothing was sorted...I cannot understand why it is doing that.. I ONLY changed the data in the input file...and it is all in the same format.
Topic archived. No new replies allowed.
Pages: 12