Question:
i need help with sorting char arrays c++?
?
2010-11-08 18:14:44 UTC
I have a file with a list of names.

Jane Huston
Bob Brown
Jack Tracy

they should be ordered by last line

like this

Bob Brown
Jane Huston
Jack Tracy

but my program wont compile i cant find the bug?

here is my code https://dl.dropbox.com/u/6790139/main.txt
Three answers:
Ilie
2010-11-08 19:17:39 UTC
Hi,



The problem with that code is that you do not understand arrays. Problems:



1) The prototypes for FindSmallest (btw, this one could make use of the "break" keyword) and Swap do not mention any arrays as arguments.

2) In SelectionSort, you are passing characters instead of arrays to both FindSmallest and Swap.

3) In order to do lexicographic ordering, you need to concatenate the string. Your key is only the first name - the rest you are not taking into account.

4) You are writing C-style C++ which is a very bad coding practice.



All of the above problems (esp. the last one) can be solved by reading a good C++ book. I recommend at least one of the following:



* The C++ Programming Language, special ed. by Bjarne Stroustrup



This book was written by Bjarne Stroustrup, the creator of C++. You should probably read this book often if you want to learn/know C++ (this book can be easily consulted as a reference). In it, he puts great emphasis on portability and good coding practices. He also argues that it is easier to go from C++ to C than the other way around.



* Thinking in C++, 2nd ed. by Bruce Eckel



This is a very popular book on C++ that presents the proper C++ mindset by following just a few simple rules laid out by the author (who, like Bjarne, is a member of the C++ standards committee). Luckly, he is kind enough to share it for free:

Volume 1: http://www.mindviewinc.com/downloads/TICPP-2nd-ed-Vol-one.zip

Volume 2: http://www.mindviewinc.com/downloads/TICPP-2nd-ed-Vol-two.zip



* Accelerated C++ by by Andrew Koenig & Barbara E. Moo



Good book for learning C++, a lot of people are fond of it.



Cheers,

Bogdan
Brandon
2010-11-08 19:13:37 UTC
In your swap function, your variable "array1" is just a char, but both within the Swap function and when you call it, you are using it like an array of chars. You might consider making a second array of just integers that stand for indexes into your input array. Then you can change your code to just swap the indexes of your input array, and have the output read out the list array1[indexArray[m]] instead of just array1[m].



There's about 4 different ways of solving this problem, and if you see a way of doing it your more readily understand than by all means ignore what I've said. But still, the compiler is complaining because you are trying to do array operations on a char around the Swap function.



NOTE: In future, consider including your compiler output in your question for faster results.
anonymous
2016-04-22 22:51:02 UTC
You have couple problems, starting here: { while (inFile >> letter) //reads file cin >> letter; ----- this you do not need here at all instead of int (letter) you should use a character variable, otherwise for some reason the file does not read Also braces { } are in the wrong places. Here is a modified code: #include #include //to include input file #include //for setw #include void printArray(const int [], int); using namespace std; int main() { ifstream inFile; string filename; int letter; char ch; unsigned int count[26]; cout << "ignore this" << endl; cout << "what is the name of the file that you wish to open\n"; getline(cin, filename); ifstream test1(filename.c_str()); if (!test1) { cout << "you did not enter the correct file\n" ; exit(1); } for (unsigned char a = 0; a < 26; a++) // clears the array { count[a] = 0; } inFile.open(filename.c_str()); //Open the file if(!inFile) cout << "There was an error opening the file sorry\n"; //Test if any error opening the file else { while (inFile>>ch) //reads file { //cin >> letter; if (ch >= 'a' && ch <= 'z') ch -= 0x20; //converts to upper case if (ch >= 'A' && ch <= 'Z') //Validates the letter count[ch - 'A']++; } } inFile.close(); for (letter = 'A'; letter <= 'Z'; letter++) //Prints the results printf("%c %d\n", letter, count[letter - 'A']); cin>>letter; return 0; } At the end you just need to sort the array count[] ,do not forget to keep track of which letter is where, you can do it with another array or make count[] array two dimensional. Even easier -- loop through the array 26 times, finding maximum value and output it, then assign the value a Zero. I let you do it yourself :)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...