Ok I know how to sort an array. That's not what I'm asking. Basically my problem analysis is:

I have to read in a file of employee reports

salesID Employee code numunits numdollars
6022 1 3 100.00
6023 1 6 200.00

ok so I read each value into arrays ( salesID[numemployees], employeecode[numemployees], etc)

Then I have to sort them and outpput to a file.

Here's where I have a problem. if salesID 6022 corresponds with 3 units sold, and I sort by numunits, how will it still have the right salesID with it. I hope my querry makes sense

7 Years
Discussion Span
Last Post by siddhant3s

When you swap elements in the salesID array, swao the corresponding elements in the other arrays.


It depends. In general you wouldn't have separate arrays, but a struct and then an array of that struct.

struct Employee {
    T1 salesID;     // whatever your types are
    T2 code;
    T3 numUnits;
    T4 numDollars;

Employee emp[numEmployees];

emp[5].salesID = 123;

Swapping emp[5] with emp[10] will swap all fields. Obviously if the record got much larger that would become inefficient, in which case you could sort an array of pointers to the array of structs (or array of record numbers for a file of records) and just move the pointers around.


Thanks. I haven't quite gotten that advanced yet so I don't think I can or should use that in my program. But thanks for your help man


Say, you have 3 Arrays A[], B[], and C[], you want to sort all these according to A[].
What you should do is Apply the sort on A[] and while swapping the elements of A[], swap the corresponding elements of B[] and C[] along.


Wanna help out on what the code for that might look like? although I think I know


Wanna help out on what the code for that might look like?
Hmm, I guess I will, but you need to tell me what sorting algorithm are you using.
I mean, First show me how your code will sort a single array numunit. Then I shall tell you what modification you must do to make it work.

>although I think I know
Well, if although you know, better start writing the code. Its always better to write your own rather than asking someone else to write for you ;) wish you luck

int I, J;
	int LowIndex, Temp;

	for (I = 0; I < N - 1; I++) {
		/* set up for case that this is smallest */
		LowIndex = I;

		/* find the smallest in the unsorted portion */
		for (J = I + 1; J < N; J++)
			if (numDollars[J] < numDollars[LowIndex])
				LowIndex = J;

		/* swap smallest with the head of the unsorted portion */
		Temp = numDollars[I];
		numDollars[I] = numDollars[LowIndex];
		numDollars[LowIndex] = Temp;

} /* end Selection sort for array of integers */

I suspect this is a copy-paste since there shouldn't be no mention of numDollars in your code.
Look at the three lines after the comment /* swap smallest with the head of the unsorted portion */ . Just repeat these three lines for all of your arrays.


Yep that's exactly what I did I just wanted some validation. and no not a copy and paste ;)


Yeah that's precisely what I tried I just wanted some validation. And no not a copy and paste ;)


hi all guys

I don't know if the thread have been marked solved or not (by the way, any guy can tell me how to judge that?), and maybe ellimist have fixed his problem already, I don't know, I just have a better solution

#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

struct Employee
	string salesID;
	int employeeCode;
	int numUnits;
	double numDollars;

	Employee(string id, int code, int units, double dollars):
	  salesID(id), employeeCode(code), numUnits(units), numDollars(dollars){}

bool sortByUnits(Employee e1, Employee e2)
	return (e1.numUnits < e2.numUnits);

int main(int argc, char *arg[])
	// test data
	Employee arr[3];	// Also, I recommend to use vector here

	arr[0] = Employee("6021", 1, 3, 100.10);
	arr[1] = Employee("6022", 2, 6, 200.10);
	arr[2] = Employee("6023", 3, 2, 400.10);

	// please notice sort algorithm here
	sort(arr, arr + 3, sortByUnits);

	for (int i = 0; i < 3; i++)
		cout << arr[i].salesID << endl;

	return 0;

Finally I wanna say, Use STL, Don't Reinvent the Wheel


>Finally I wanna say, Use STL, Don't Reinvent the Wheel
Perfect. You said what I, and even Bjarne Stroustup and even most of elegant programmer says. But here are few points for you:
1.The Thread Starter, on Post#5 says that he cannot go that advanced.
2. You should perhaps not give away codes like this. Read the sticky thread http://www.daniweb.com/forums/thread78223.html last point
3. The code you pasted was good :)

This article 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.