Sorting a file with names II

Im continuing on this for a lack of space.

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
using namespace std;


const int ASCENDING_ORDER = 1;
const int DESCENDING_ORDER = 2;
const int SORT_BY_NAME = 3;
const int SORT_BY_SCORE = 4;

const int ASCENDING_NAME = ASCENDING_ORDER * SORT_BY_NAME; // 3
const int DESCENDING_NAME = DESCENDING_ORDER * SORT_BY_NAME; //6
const int ASCENDING_SCORE = ASCENDING_ORDER * SORT_BY_SCORE; //4
const int DESCENDING_SCORE = DESCENDING_ORDER * SORT_BY_SCORE; //8

bool processCommandLineArguments (int argc, char * argv[], 
         int & sortKey, int & sortOrder); // sortKey = ASCENDING_ORDER;

void displayProgramUsageAndQuit ();

unsigned int getNumDataItemsFromCIN ();

char ** allocateArrayOfNamePointers (unsigned int arraySize);

unsigned int * allocateArrayOfScores (unsigned int arraySize);

bool readAndParseDataLineFromCIN (char name [], unsigned int & score);

bool updateArrayOfNames (char * array [], unsigned int index, char name []);

void updateArrayOfScores (unsigned int array[], unsigned int index,unsigned int score);

void sortNameAndScoreArrays (char * nameArray[],unsigned int array[], unsigned int size,
                        int sortKey, int sortOrder); 

void sendResultsToOutput (char * nameArray[],unsigned int array[], unsigned int size);


void freeAllocatedMemory (char ** nameArray, unsigned int * scoreArray, unsigned int arraySize);

void main(int argc, char *argv[])
{
  //char * names [] = {"Joe", "Bob", "Sally", "Brenda", "Sam"};
  //unsigned int scores[] = {95, 100, 88, 66, 90};
  
  char name[80];
  unsigned int score;
  int lineIndex = 0;
  char line[80];
  cin.getline (line, 80);
  unsigned int count = atoi(&line[lineIndex]);
  char * names [100];
  unsigned int scores [100];
  unsigned int sizeOfData;
  
  
  //sizeOfData = getNumDataItemsFromCIN ();
  //allocateArrayOfNamePointers (sizeOfData); //allocate memory for array of names
  //allocateArrayOfScores (sizeOfData);
  //cout <<" sizeOfData   hello is  "<<sizeOfData<<endl;
  
  for (int index = 0; index < count; index++)
  {
     bool readLineOK = readAndParseDataLineFromCIN (name, score);
     if (readLineOK)
     {
	   //sortNameAndScoreArrays (names, scores, count, SORT_BY_NAME, ASCENDING_ORDER);
       
		//code 
		names[index] = name;
	   scores[index] = score;
	   //cout << name << endl;
       //cout << score << endl << endl;
	   
     }
     else cout << "Error reading line " << index + 1 << endl << endl;

  }

  if(string(argv[1]) == "name" && string(argv[2]) == "asc"){
	cout << "ascending names" << endl;
	sortNameAndScoreArrays (names, scores, count, SORT_BY_NAME, ASCENDING_ORDER);
	sendResultsToOutput (names, scores, count); 
  }
  else if(string(argv[1]) == "score" && string(argv[2]) == "asc"){
	cout << "ascending score" << endl;  
	sortNameAndScoreArrays (names, scores, count, SORT_BY_SCORE, ASCENDING_ORDER);
	sendResultsToOutput (names, scores, count); 
  }
  else if(string(argv[1]) == "score" && string(argv[2]) == "des"){
	cout << "descending score" << endl;
	sortNameAndScoreArrays (names, scores, count, SORT_BY_SCORE, DESCENDING_ORDER);
		sendResultsToOutput (names, scores, count); 
  }
  else if(string(argv[1]) == "name" && string(argv[2]) == "des"){
	cout << "names descending" <<  endl;
	sortNameAndScoreArrays (names, scores, count, SORT_BY_NAME, DESCENDING_ORDER); 
	sendResultsToOutput (names, scores, count); 
  }

 
  
} 


bool readAndParseDataLineFromCIN (char nameArray [], unsigned int & score)
{
  int lineIndex = 0, nameIndex = 0;
  char line[80];

  cin.getline (line, 80);

  while (line[lineIndex] != '\"') lineIndex++; // skip over leading space
  lineIndex++; // now lineIndex points to first char of name

  while (line[lineIndex] != '\"') // look for closing quote
  {
    nameArray[nameIndex] = line[lineIndex];
    nameIndex++; lineIndex++;
  }
  nameArray[nameIndex] = '\0'; // add null terminator
  
  lineIndex++;
	
	//looking for score
  score = atoi(&line[lineIndex]);

  return true;

}

void sortNameAndScoreArrays (char * nameArray[],unsigned int scoreArray[], unsigned int size,
                        int sortKey, int sortOrder)
{
  unsigned int indexToFix;
  unsigned int indexOfLowestAlpha;
  char * tempPtr;
  unsigned int tempScore;
  unsigned int indexOfLowestScore;
  bool commandLineArgument;
  char * argv[3];
  int argc;
  unsigned int indexOfLowestNum;
  
  
  
  //commandLineArgument = processCommandLineArguments(argc, argv, sortKey, sortOrder);

			int sortCase =  sortKey * sortOrder;

			for (indexToFix = 0; indexToFix < size; indexToFix++)
			{
					switch (sortCase)
					{
						case ASCENDING_NAME:
						{
								indexOfLowestAlpha = indexToFix; // assume first name is lowest
								for (unsigned int indexOfCurrentName = indexToFix + 1; indexOfCurrentName < size; indexOfCurrentName++)
								{
									if (strcmp (nameArray[indexOfCurrentName],
												nameArray[indexOfLowestAlpha]) < 0)
									{
									   indexOfLowestAlpha = indexOfCurrentName; 
										
									}        

								} // end for
						  
						  // swap pointers in name array
							  tempPtr = nameArray[indexOfLowestAlpha];
							  nameArray[indexOfLowestAlpha] = nameArray[indexToFix];
							  nameArray[indexToFix] = tempPtr;

							  // swap scores in score array
							  tempScore = scoreArray[indexOfLowestAlpha];
							  scoreArray[indexOfLowestAlpha] = scoreArray[indexToFix];
							  scoreArray[indexToFix] = tempScore;
							  break;
						 
						}
						  /***************************************************************/

							case DESCENDING_NAME:
							{
								indexOfLowestAlpha = indexToFix;
								  for(unsigned int indexOfCurrentName = indexToFix + 1; indexOfCurrentName < size; indexOfCurrentName++)
								  {
									if(strcmp (nameArray[indexOfCurrentName], nameArray[indexOfLowestAlpha]) > 0)
									{
										indexOfLowestAlpha = indexOfCurrentName;
										
									}
								  }
								  
								  //swapping names
								  tempPtr = nameArray[indexOfLowestAlpha];
								  nameArray[indexOfLowestAlpha] = nameArray[indexToFix];
								  nameArray[indexToFix] = tempPtr;
								  
								  //swapping scores 
								  tempScore = scoreArray[indexOfLowestAlpha];
								  scoreArray[indexOfLowestAlpha] = scoreArray[indexToFix];
							      scoreArray[indexToFix] = tempScore;
								  break;
							}
						case DESCENDING_SCORE:
						{
							indexOfLowestNum = indexToFix; // check for lowest number
							
							for(unsigned int indexOfCurrentNum = indexToFix + 1; indexOfCurrentNum < size; indexOfCurrentNum++)
							{
								if(scoreArray[indexOfCurrentNum] > scoreArray[indexOfLowestNum])
								{
									indexOfLowestNum = indexOfCurrentNum;
								}
							}
							
							//swapping name 
							tempPtr = nameArray[indexOfLowestNum];
							nameArray[indexOfLowestNum] = nameArray[indexToFix];
							nameArray[indexToFix] = tempPtr;
							
							//swapping scores
						 tempScore = scoreArray[indexOfLowestNum];
						 scoreArray[indexOfLowestNum] = scoreArray[indexToFix];
						 scoreArray[indexToFix] = tempScore;
							break;
						}
					
						case ASCENDING_SCORE:
						{
							indexOfLowestNum = indexToFix; // check for lowest number
							
							for(unsigned int indexOfCurrentNum = indexToFix + 1; indexOfCurrentNum < size; indexOfCurrentNum++)
							{
								if(scoreArray[indexOfCurrentNum] < scoreArray[indexOfLowestNum])
								{
									indexOfLowestNum = indexOfCurrentNum;
								}
							}
							
							//swapping name 
							tempPtr = nameArray[indexOfLowestNum];
							nameArray[indexOfLowestNum] = nameArray[indexToFix];
							nameArray[indexToFix] = tempPtr;
							
							//swapping scores
							tempScore = scoreArray[indexOfLowestNum];
							scoreArray[indexOfLowestNum] = scoreArray[indexToFix];
							scoreArray[indexToFix] = tempScore;
							break;
						}
					
				
						  
				  
				  
							// check other cases

					}	 // end case

		  
			} // end for
 
	//}
	//else 
	//{
		//cout << "Your input was incorrect.";
	//}
} // end function

You should put this entire post into a reply on your first post, then delete this one. This will help keep everything on one page, and people will be more likely to read through that mess of code to help you (although such large amounts of code will put off most people anyways...).
Last edited on
I dont know how they would help me with my problem with one little snippet of code thats why i posted everything here because in the other one it only had where i had the problem but its linked to everything else.
You could post everything from this post as a reply in your first post. That's what I was getting at.
Topic archived. No new replies allowed.