parallel array sorting

Hi there everyone. Or anyone, at this late hour. I am doing a project, and could use some help. I am writing a program which sorts data from a file into an array, and does various things with it though a menu driven system. ( which is still in the works, please ignore it for now.) Right now, I am trying to create a function that will take the three parallel arrays created through the data files, sort them in ascending order by hire date, and finally display them. I am using a bubble sort, since there is not much data. I can get it done, but the problem I am running into is the display. This is what I have that works. The function I am working on is ListHD.

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
# include <iostream>
# include <fstream>
# include <cmath>
using namespace std;

const int MAX_ARRAY = 50;
void getdata(int &numElem, int hireDate[], int empID[], double salary[], ifstream &fileIn);
void showMenu();
void ListHD(int numElem,int array[]);


int main()
{
	ifstream fileIn;
	int hireDate[MAX_ARRAY];	//Array's for storage of hiredate, employee id, and salary
	int empID[MAX_ARRAY];
	double salary[MAX_ARRAY];
	int numElem;

	fileIn.open("employdata.txt");	//Open file

	if (fileIn.fail())	//test file for existance
	{
		cout << "Problem with file";
		exit(-1);
	}

	getdata(numElem, hireDate, empID, salary, fileIn);	//Read file and count values into 3 paralell arrays
	
	showMenu();

	ListHD(numElem, hireDate);

	system("pause");
	
}

void getdata(int &numElem, int hireDate[], int empID[], double salary[], ifstream &fileIn)
{
	int i = 0;
	fileIn >> hireDate[i] >> empID[i] >> salary[i];
	while (!fileIn.eof())
	{
		i++; 
		fileIn >> hireDate[i] >> empID[i] >> salary[i];
	}

	numElem = i;

	//for (int i = 0; i < numElem; i++)
        //cout << hireDate[i] << "  " << empID[i] << "  " << salary[i] << endl;

}

void showMenu()
{
	cout	<< "\n\t\tEmployee data menu\n\n"
			<< "1. List by hire date\n"
			<< "2. List by employee number\n"
			<< "3. Write total of salaries\n"
			<< "4. Add employee\n"
			<< "5. Delete employee\n"
			<< "6. Quit Program\n"
			<< "Please enter a valid menu choice: \n";
}

void ListHD(int numElem, int array[])
{
	
    int temp, end;

   for (end = numElem - 1; end >= 0; end--)
   {
       for (int count = 0; count < end; count++)
       {
          if (array[count] > array[count + 1])
          {
             temp = array[count];
             array[count] = array[count + 1];
             array[count + 1] = temp;
          }
       }
   }
   for (int i = 0; i < numElem; i++)
        cout << array[i] << endl;
}



When I use that, everything is fine, but I can only display one portion of the array sorted, be it the hire date, employee id, or salary. I want to display everything, all three parallel array's and display it as sorted from the hire date. So I am trying something like this, but its not working, and I am having problems finding resources online. Can anyone help?

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
	
# include <iostream>
# include <fstream>
# include <cmath>
using namespace std;

const int MAX_ARRAY = 50;
void getdata(int &numElem, int hireDate[], int empID[], double salary[], ifstream &fileIn);
void showMenu();
void ListHD(int numElem,int array[], int hireDate[], int empID[], double salary[]);


int main()
{
	ifstream fileIn;
	int hireDate[MAX_ARRAY];	//Array's for storage of hiredate, employee id, and salary
	int empID[MAX_ARRAY];
	double salary[MAX_ARRAY];
	int numElem;

	fileIn.open("employdata.txt");	//Open file

	if (fileIn.fail())	//test file for existance
	{
		cout << "Problem with file";
		exit(-1);
	}

	getdata(numElem, hireDate, empID, salary, fileIn);	//Read file and count values into 3 paralell arrays
	
	showMenu();

	ListHD(numElem, hireDate, empID, salary);

	system("pause");
	
}

void getdata(int &numElem, int hireDate[], int empID[], double salary[], ifstream &fileIn)
{
	int i = 0;
	fileIn >> hireDate[i] >> empID[i] >> salary[i];
	while (!fileIn.eof())
	{
		i++; 
		fileIn >> hireDate[i] >> empID[i] >> salary[i];
	}

	numElem = i;

	//for (int i = 0; i < numElem; i++)
        //cout << hireDate[i] << "  " << empID[i] << "  " << salary[i] << endl;

}

void showMenu()
{
	cout	<< "\n\t\tEmployee data menu\n\n"
			<< "1. List by hire date\n"
			<< "2. List by employee number\n"
			<< "3. Write total of salaries\n"
			<< "4. Add employee\n"
			<< "5. Delete employee\n"
			<< "6. Quit Program\n"
			<< "Please enter a valid menu choice: \n";
}

void ListHD(int numElem, int array[], int hireDate[], int empID[], double salary[])
{
	
    int temp, end;

   for (end = numElem - 1; end >= 0; end--)
   {
       for (int count = 0; count < end; count++)
       {
          if (array[count] > array[count + 1])
          {
             temp = array[count];
             array[count] = array[count + 1];
             array[count + 1] = temp;
          }
       }
   }
   for (int i = 0; i < numElem; i++)
        cout << array[i] << endl;
}
Why do you pass hireDate[] empId[] and salary[] to the ListHD function if you don't use them? If you want to display them, just display them in the for loop at the bottom instead of printing the contents of just array[].
i passed them because I saw that when I had just hireDate in the function in the first set of code, it output the hireDate, and so on and so forth for empID and salary. I thought that if I could find a way to put all three of them in it would sort and display all three, but I got too many function call errors, so that second portion of code was me trying to fix it and work around it, but to no avail
after doing some reading, apparently what I am trying to do is dual sorting, but with three arrays. I think in the first part of the code, what ever I was throwing in was only being sorted, not just displayed. What I want to do is sort hireDate, while keeping the corresponding elements with them, the empID and salary that goes with each hireDate in sync with each other. Still, I tried a few things and cannot get it done right.
Last edited on
Topic archived. No new replies allowed.