sorting two parallel arrays

Hi,
I have a text file with two columns: date and grades. I want to read in the file and bubble sort the data by either date or grades. What I've done is to read the data from the file and put them into two parallel arrays. I did that because the data types are different. Now I'm trying to use the bubble sort function and spit out the output into a "final.txt" file.
I have several questions:
1. I want to convert the date from a string into long. Does the atol command work in class?
2. The sorting doesn't seem to work. Is it because I have the data into two separate arrays ? If so, do I need to combine the two arrays into a single array and use the function? I'm confused. Please help. The code is below.

**************************************************************

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
const int MAX_ELEMENTS = 251;
void bubbleSort (int[], const int);

class DATE {
       public:
              float grade[MAX_ELEMENTS];
              string date0[MAX_ELEMENTS];
              long atol( const char* date0);
              void show(){
                   for(int k = 0; k < MAX_ELEMENTS; ++k)
                      cout << date0[k] << setw(10) << grade[k] << endl;
                   }
       };

int main()
{
    const int arraysize = 251;
    int a[arraysize];
    DATE myDate;
      ifstream inputFile("C:\\grades.txt");
      ofstream outputFile ("C:\\final.txt");
     
      for(int i = 0; i < MAX_ELEMENTS; ++i)
        {
            inputFile >> myDate.date0[i] >> myDate.grade[i];
        }
    myDate.show();

    bubbleSort (a, arraysize);

      for (int i = 0; i < MAX_ELEMENTS; i++) // writing the output to the file
        {
            outputFile << myDate.date0[i] << setw(10) << myDate.grade[i] << endl;
        }

    system("Pause");
      return 0;
}

void bubbleSort (int a[], const int size)
{
  int hold;
  for (int pass = 0;  pass <size -1; pass++) // passes
    for (int i=0; i < size - 1; i++) // one pass
      if (a[i] > a [i+1]) // then compare
      {
    hold = a[i];
    a[i]= a[i+1];
    a[i+1]= hold;
      }
}

1. You mean like, a being a Class, calling atol(a)? If you do, then no. If you mean calling atol("2008-09-11"), then that won't work, either.
2. See http://en.wikipedia.org/wiki/Bubble_sort (Never mind.)
2. No. Just pass the other array and swap its i and i+1 elements.

And for the love of god, see this: http://en.wikipedia.org/wiki/Indent_style
Last edited on
Topic archived. No new replies allowed.