My professor has given me a code file and a data file, and I need to alter just line 64 "if (p1->count > p2->count) {" to sort alphabetically instead of by count. I realize this is probably really easy, I just can't figure out what to do. Please help!
A better explanation of this would be useful. I don't have time right now to look through the entire program, so a step-by-step description of the program and a clearer question would be helpful in trying to answer your question.
The data file to be sorted is a list of numbers and words with those numbers , e.g.
1 word
2 flip
3 house
...and so on. I'm lost in the class so I really don't know what the rest of the program does exactly. I need to change line 64 so that it sorts the data file alphabetically instead of by count.
I don't have a coded solution, but here's how I would go about doing it (according to the question you asked in your previous post (I'm not even sure this is answering your question at all, but oh well, here it goes):
Step 1) Create an array of x number of strings (where x is the number of words in the data file).
Step 2) Use a for loop to assign one of the words to one of the 'positions' of the array (one at a time).
Step 3) Using the Bubble Sort, check the ascii value of the 1st character of one of the words (which is the 3rd character in the string if each word has a number and space before it) in the array, and if the ascii value of the 1st letter of the word before it is greater, switch the two words, using the swap() function (which takes 2 arguments, and is either part of iostream or iomanip). Make sure that when you swap the two words, that the number stays the same, while the word changes. For example, according to the example you gave above,
1 word
2 flip
3 house
the alphabetized version should look like this:
1 flip
2 house
3 word
NOT LIKE THIS (BAD!!!):
2 flip
3 house
1 word
notice here that the number that comes before the word doesn't change, and while the words are in order, THIS ISN'T COMPLETELY CORRECT.
Step 4) Open a new filestream, creating a 'new' data file with exactly the same name as the old data file (that way it will override the existing file). Using a for loop, write each word into the file, one at a time, REMEMBERING TO ADD A NEWLINE BETWEEN EACH WORD.
TA-DA!!! Then you're done!!!
Please tell me if this is the answer that you are looking for because some of your code confuses me (as I am new to C++). If there is anything else I can do to help, please let me know.
Well, you no longer want to sort by count, so the obvious thing to do is make the comparison on some other member of LIST. I wonder which member it could be?
Well, no. comparing the address of word in one object to the address of word in another object isn't going to do the trick. You want to compare the strings those members point to. It's essentially the same reason you use strcpy in your code instead of saying p1->word = p2->word.
...no, it worked. The words are in alphabetical order. But if you'd like to quit acting like Nick Burns, my company's computer guy, you can just tell me what you would have done.
I happen to be in this same class. Changing line 64 to
if (strcmp (p1->word, p2->word) > 0) puts the words from the input data file in alphabetical order with the counts of each word before hand. However, my output only got the words beginning with letters S through Z. All the words in the data file beginning with letters A through R are not included in the output. What reasons, if any, could explain this.
PS. , the data file is like a 49 page excerpt of a book, which is why i havent posted it.