Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 string sort
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Jeevan25




PostPosted: Wed Dec 21, 2005 5:09 pm   Post subject: string sort

i am in need of help. i am a high school student taking c++. i am using dev c++ as my ide. i need to write a program that reades from a file. the file contains 1000 words in random order. one word per line. the program reads the file and sort the words in alphabetic order. then it outputs the sorted words to a new file. for example.
unsorted words.dat
code:
pile
game
they
apple


now the program will read this file and sort the words in alphabetic order and save to a new file.
sorted words.dat
code:
apple
game
pile
they
how do i do this? pls help. thanks.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Dec 21, 2005 5:13 pm   Post subject: (No subject)

Well, first you need to read the lines from the file into something like a:

c++:
std::vector<std::string>


Now, here's where you ask me what that is. Don't worry, specific questions are how you learn. Smile
Jeevan25




PostPosted: Wed Dec 21, 2005 7:42 pm   Post subject: (No subject)

yeap. i didnt go that far. my teacher told me three ways. select sort, insertion sort and bubble sort. i didn't learn what you typed. Very Happy my teacher ain't that great either. she tells me to come after school but she is never there.
wtd




PostPosted: Wed Dec 21, 2005 7:44 pm   Post subject: (No subject)

Well, as I said, the first step is to get the lines into some kindof collection, then you can worry about the sort.
Martin




PostPosted: Wed Dec 21, 2005 7:56 pm   Post subject: (No subject)

File input and output:
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html

How to use Vectors and arrays:
http://cplus.about.com/od/beginnerctutorial/l/aa050102a.htm

And finally, sorting. If you haven't heard of any of these, start with the bubble sort. It's horribly slow, but very easy to do and for 1000 entries will be more than fast enough.
http://en.wikipedia.org/wiki/Sort_algorithm

Your program will look something like this (assuming you're going for a functional (ie. non object oriented) approach):

1. Create a new vector.
2. Open the file.
3. Loop through the file and fill up the vector with the file's contents
4. Close the file.
5. Sort the contents of the vector.
6. Output results.
Jeevan25




PostPosted: Wed Dec 21, 2005 10:03 pm   Post subject: (No subject)

i need to know the loops and the if statements to sort the words. i know how to read and write to files.
bugzpodder




PostPosted: Wed Dec 21, 2005 10:37 pm   Post subject: (No subject)

if you find the examples martin give are not enough, then I suggest you go speak to your teacher. He will be more accessible and provide more relevant help
Jeevan25




PostPosted: Wed Dec 21, 2005 10:46 pm   Post subject: (No subject)

yeah right. i hate her. i ask for help with my code. somthing is wrong. she ask like 3 people who are getting moer than 80 to help me because she couldn' find problem. then she called the best student. he is the one who solved it. not the teacher. if there was a worst teacher of millenium she would be that one.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Dec 21, 2005 10:51 pm   Post subject: (No subject)

Shame the STL can't be fully utilized.

code:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>

using namspace std;

int main()
{
   vector<string> words;
   ifstream input_file("file_name");

   copy(istream_iterator<string>(input_file),
        istream_iterator<int>(),
        back_inserter(words));   
   sort(words);
   copy(words.begin(),
        words.end(),
        ostream_iterator<string>(cout, "\n"));

   return 0;
}
wtd




PostPosted: Wed Dec 21, 2005 11:22 pm   Post subject: (No subject)

Hmmm... actually...

code:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>

using namspace std;

int main()
{
   ifstream input_file("file_name");
   vector<string> words(istream_iterator<string>(input_file),
                        istream_iterator<string>());

   sort(words);
   copy(words.begin(),
        words.end(),
        ostream_iterator<string>(cout, "\n"));

   return 0;
}
MysticVegeta




PostPosted: Sun Dec 25, 2005 12:59 pm   Post subject: (No subject)

haha I love this sort commands which are not there in Turing by the ways, But I have a question, Didn't your teacher say about "shell sort"? I think thats the fastest way to sort...

http://linux.wku.edu/~lamonml/algor/sort/sort.html
Hikaru79




PostPosted: Sun Dec 25, 2005 2:59 pm   Post subject: (No subject)

MysticVegeta wrote:
Didn't your teacher say about "shell sort"? I think thats the fastest way to sort...

Haha, no, it's the fastest of the O(n^2) algorithms -- that's far, far away from being faster than quicksort or merge sort (for anything other than trivially-sized lists, that is).
wtd




PostPosted: Sun Dec 25, 2005 3:01 pm   Post subject: (No subject)

http://en.wikipedia.org/wiki/Sorting_Algorithm
MysticVegeta




PostPosted: Mon Jan 02, 2006 12:06 am   Post subject: (No subject)

Heap sort seems to beat merge and quicksort..
Geminias




PostPosted: Mon Jan 02, 2006 10:02 am   Post subject: (No subject)

Quote:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>

using namspace std; //typo should be "namespace"

int main()
{
ifstream input_file("file_name");
vector<string> words(istream_iterator<string>(input_file),
istream_iterator<string>());

sort(words);
copy(words.begin(),
words.end(),
ostream_iterator<string>(cout, "\n"));

return 0;
}


if namespace were spelled right would this code compile for you wtd?
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 27 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: