Array sorting program code

Hi everyone,

I have been having so much trouble with an exercise lately and I would appreciate it so much if someone could help me out.

I am a very beginner for C++ and right now I need to process data from an input file which has max. 100,000 lines like the following:

 
2014269619 Sally Clare Smith Female 95


These are the ID number, name (2 or 3 words), gender and exam score.

I must create an array of max. 100,000 length for each of these attributes. Also, the first number within the input file will be the number of lines in that file.

So, I create arrays with length x, given that x is the first number in the input file.

Once I set up an array, I need to use a given function to sort the names by alphabetical string order (ascending) of Name and then put these into an output file. My challenge here is that although the given function sorts the names, it does not move the ID, gender, and score together with the names, resulting in a useless database.

I have tried doing to first part (setting up arrays) in the following way but it seems wrong:

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

void  ordering_sort( double  x[],  int  length)
{	
	int  i,  j,  k;
	double  t;
	
	for  (i=0;  i<length-1;  ++i)  {	    
		k = i;		 //find next smallest elm, x[k]
		
		for  (j=i+1;  j< length;  ++j)
			if  (x[k] > x[j])  k = j;
		
		//swap x[i] with x[k] 
		t = x[i];   x[i] = x[k];  x[k] = t;   
	}
}


int main () {

ifstream fin;
ofstream fout;

fin.open("input.txt");
fout.open("output.txt");

if (fin.fail()) {
    cout << "Fail to open inout.txt" << endl;
    exit(1);
}

int x; 
fin >> x; //this is the first number within the input text file, 
// indicating the number of lines in the file, thus the size of the arrays

int ID [x];
string name [x];
string gender [x];
int score [x];

int y = 0;

// In the following part, I am trying to extract the information into the different arrays,
// one by one, increasing the number of the element from 0 up till x. 
// Problem is that getline does not work for int arrays but I must use int.

for (int y=0 ; y<x, y++) {
    getline(fin, ID [y], '\t'); // This does not work.
    getline(fin, name [y], '\t');
    getline(fin, gender [y], '\t');
    getline(fin, score [y], '\t'); //This does not work either.
    break;
    }

ordering_sort( name,  int  length)

// trying to use the function, which will not work as of now


Basically, I would need help with:
- extracting the data from input file into the arrays
- making sure that sorting will apply to every line not just individual arrays
- finding a way to make getline work for int arrays
- I shall output the data into an output file but that should not be a problem.
- All of this must be done using arrays and without using structures, vectors, maps, other libraries

Thanks so much!
Last edited on
Use a struct

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

struct Record {
	unsigned id_;
	string name_;
	string gener_;
	unsigned score_;
};

int main() {

	Record records[100];

	records[0].id_ = 100;
	records[0].name_ = "First Record";

	records[1].id_ = 101;
	records[1].name_ = "Second Record";

	return 0;
}
Thank you for your response but unfortunately I am not allowed to use structures -as mentioned above:(
Then you need to swap each of your arrays during your sort. Not just the ID array
Topic archived. No new replies allowed.