Sorting related two arrays based on another

Hi. I'm new to C++ programming. I got a task on sorting. Given a text file with three column information's which are matricNum, marks and gender. I need to sort marks in ascending order and i had done with the code and its work, but the matricNum and gender doesn't change accordingly to the marks. I don't have any idea on how to do it. Hope anyone can help me.

Input file:
001 65.5 2
002 56.7 2
003 35.8 1
004 42 2
005 49.4 1
006 55.1 1
007 87.5 2

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
#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()

{
	int i, j;
	double temp;
	int matricNum[7];
	double marks[7];
	int gender[7];

	// store data into array.
    {
        // open the file for input
        ifstream inputFile("myFile.txt");

        // the input file was opened 
        for( int i = 0; i < 7; i++ )
        inputFile >> matricNum[i] >> marks[i] >> gender[i];

        // the file is closed
    }

	//sort marks in ascending order using bubble sort

	for (i=0;i<7;i++)
	{
		for (j=i+1;j<=6;j++)
		{

			if (marks[i] > marks[j])
			{
				temp = marks[i];
				marks[i] = marks[j];
				marks[j] = temp;
			}
		}
	}

	for (i=0;i<=6;i++)
	{
		cout << matricNum[i] << ", " << marks[i] << ", " << gender[i] << endl;
	}

	system("pause");
}


The actual answer should be:
003 35.8 1
004 42 2
005 49.4 1
006 55.1 1
002 56.7 2
001 65.5 2
007 87.5 2
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
#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()

{
  int i, j;
  double temp;
  int matricNum[7];
  double marks[7];
  int gender[7];

  // store data into array.
    {
        // open the file for input
        ifstream inputFile("myFile.txt");

        // the input file was opened 

        // I've also removed int here, because you've already declared an int i
        // above, so there is really no point in redeclaring it here unless you
        // soley need to use it here. But you don't just use it here, you also
        // use it down below.

        for(i = 0; i < 7; i++ )
        inputFile >> matricNum[i] >> marks[i] >> gender[i];

        // the file is closed
    }

  //sort marks in ascending order using bubble sort

  for (i=0;i<7;i++)
  {
    for (j=i+1;j<=6;j++)
    {

      if (marks[i] > marks[j])
      {
        /* I commented these three out, but you only swapped one item when you
         * should have really been swapping three things as parallel arrays.
         * C++ has a swap function where you can pass in two arguments to swap
         * their values. This is what I've done for you below... */

          //temp = marks[i];
          //marks[i] = marks[j];
          //marks[j] = temp;

        swap(marks[i], marks[j]);
        swap(matricNum[i], matricNum[j]);
        swap(gender[i], gender[j]);
      }
    }
  }

  for (i=0;i<=6;i++)
  {
    cout << matricNum[i] << ", " << marks[i] << ", " << gender[i] << endl;
  }
  system("pause"); // You should avoid this...
}
Last edited on
Hello yat89,

What you have, I believe, is known as parallel arrays. So if you sort one array you also have to move the data of the other two arrays as fiji885 has shown you.

Starting at line 37 you are sorting one array, but you also have to sort the other two arrays at the same time.

Hope that helps,

Andy
Hi fiji885 and Andy,

Thanks for your help and its really work. I have learn something new today from both of you. Thanks again for your kindness.
you have some really nice data.
your file already has the array index (off by 1) as one of the columns.
if you sort column 1 and 2 (2 is the sort-key) then 3 is free, you don't have to move data for 3, you can just index 3 off the first one.

that is,
007 87.5 is tied to 3rd column [6] (first column -1)
and 001 65.5 is tied to 3rd[0]
and so on.

----------------
if you did not have a column like your first one that lets you cheat, you can *construct* one yourself, here that would be adding a 4th column with 0-N (no need to be off by one if you created it yourself) and then do the parallel sort on the sort-key column and your created column, and index the others off the sorted constructed column.

Parallel arrays are rare in c++, though. Usually one would make a user defined type that held all 3 columns and just sort a container of the type off the key, and it would be done.
Topic archived. No new replies allowed.