Sort Function is not fully sorting array

The sort_words function is not fully sorting the array. When executed the first half looks sortedthen after that the array is just in random order.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188


int main(int argc, char * argv[])
{
	char list[10000][30] = { " " };
	char temp[30] = { " " };
	int count[10000] = { 0 };
	char line[100] = { " " };
	int list_counter = 0;

	bool readline = true;
	bool output = true;

	ifstream fin;
	ofstream fout;

	if (argc != 3)
	{
		cout << "Incorect usage. Must provide input and output file names. Quitting." << endl;
		return -1;
	}

	fin.open(argv[1]);

	if (!fin)
	{
		cout << "There was an error opening one of the files" << endl;
		fin.close();
		return -1;
	}
	while (read_line(fin, line) == true)
	{

		process_line(list, line, temp, count, list_counter);
	}

	fin.close();

	sort_words(list, count, list_counter);

	output = write_list(fout, list, count, argv, list_counter);

	if (output == false)
	{
		cout << "Output to file had an error occur" << endl;
	}

	cout << swap_counter + 1 << endl;
}

void add_word(char list[][30], char temp[], int count[], int &list_counter)
{
	int i = 0;
	while (i < list_counter)
	{
		if (stricmp(list[i], temp) == 0)
		{
			count[i]++;
			return;
		}
		i++;
	}

	strcpy(list[list_counter], temp);

	count[list_counter] = count[list_counter] + 1;

	list_counter++;

}
//adds word to list, or increments counter if found
void sort_words(char list[][30], int count[], int &list_counter)
{
	char hold[30] = { " " };
	int holder = 0;
	int i = 0;
	int j = 0;
	int bound1 = 0;
	int bound2 = 0;

	for (i = list_counter - 2; i >= 0; i--)
	{
		j = i;
		bound1 = list_counter - 1;
		bound2 = stricmp(list[j + 1], list[j]);
		while( j < bound1 && bound2 < 0)
		{
			strcpy(hold, list[j + 1]);

			strcpy(list[j + 1], list[j]);

			strcpy(list[j], hold);

			holder = count[j + 1];

			count[j + 1] = count[j];

			count[j] = holder;

			j++;
		}
	}
}
//sorts the list, starting at the high end for 20 words
bool read_line(ifstream &fin, char line[])
{

	while (!fin.eof())
	{
		fin.getline(line, 100);

		return true;
	}

	return false;

}
//reads a line, returns false at end of file
bool extract_word(char line[], char temp[])
{
	static int i = 0;
	int j = 0;

	while (i < strlen(line))
	{
		if (isalpha(line[i]))
		{
			while (line[i] != ' ' && line[i] != '\0')
			{
				temp[j] = line[i];
				i++;
				j++;
			}

			while (!isalpha(line[i]))
			{
				i--;
				j--;
			}
			i++;
			j++;
			temp[j] = '\0';
			return true;
		}
		i++;
	}

	i = 0;

	return false;
}
//gets a word, returns false if no word found
void process_line(char list[][30], char line[], char temp[], int count[], int &list_counter)
{
	while (extract_word(line, temp))
	{
		add_word(list, temp, count, list_counter);
		if (list_counter % 19 == 0)
		{
			sort_words(list, count, list_counter);
			swap_counter++;
		}
	}

}
//uses extract_word(), add_word()
bool write_list(ofstream &fout, char list[][30], int count[], char *argv[], int &list_counter)
{
	int i = 0;

	fout.open(argv[2]);

	for (i = 0; i < list_counter; i++)
	{
		fout << list[i] << '\t' << '\t' << count[i] << endl;
	}

	cout << "Successfully wrote " << list_counter << " words to " << argv[2] << endl;

	if (!fout)
	{
		fout.close();
		return false;
	}
	fout.close();
	return true;
}
//writes list to output file, returns false if file fails 
Don't leave out the prototypes of the functions and the includes that you are using.
Also, provide an example input.
Topic archived. No new replies allowed.