| | |
Sorting a 2D Array of Strings
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2007
Posts: 21
Reputation:
Solved Threads: 0
Hi,
I am working on a program for an assignment. It is to find all the anagrams contained in an input file.
I have read the file into a vector. Next I created a 2D array that contains the orginal words from the file in 1 column and the signatures for each in the next:
Currently this is what my 2D Array contains
Pans --> ansp
stop --> opts
Pots --> opts
Pots --> opts
opt --> opt
Sit --> its
it's --> sit
What I want to do is sort the 2D array using the second column. This way when I move one word in column 2 I will also move the word at the same index location in column 1.
The result would be
Pans --> anps
snap --> anps
Sit --> its
it's --> its
stop --> opts
Pots --> opts
Pots --> opts
opt --> opt
I am new to programming, please help me if you can.
Thanks
I am working on a program for an assignment. It is to find all the anagrams contained in an input file.
I have read the file into a vector. Next I created a 2D array that contains the orginal words from the file in 1 column and the signatures for each in the next:
Currently this is what my 2D Array contains
Pans --> ansp
stop --> opts
Pots --> opts
Pots --> opts
opt --> opt
Sit --> its
it's --> sit
What I want to do is sort the 2D array using the second column. This way when I move one word in column 2 I will also move the word at the same index location in column 1.
The result would be
Pans --> anps
snap --> anps
Sit --> its
it's --> its
stop --> opts
Pots --> opts
Pots --> opts
opt --> opt
I am new to programming, please help me if you can.
Thanks
Last edited by Akilah712; Jul 14th, 2007 at 4:54 pm.
I personally don't really get the project, can you give me an example of an input and output..? thx
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Hang out place of novice and intermediate programmers
•
•
Join Date: Jun 2007
Posts: 21
Reputation:
Solved Threads: 0
I'm actually new to sorting. I usually use the STL sort. I've never written a sort function.
So I guess my question is how do I write a sort function for an array of strings.
So I guess my question is how do I write a sort function for an array of strings.
C++ Syntax (Toggle Plain Text)
for (int i = 0; i< the-array-lenght; i++) { get the array[i] and compare it to the next compare arry[i] to array[2] swap if smaller don't swap if they are equal } ???
you're not necessarily to use multi dimentional arrays right? i guess for me it's easier to just use one dimentional array, but compare the index. Make sure the comparison is on the same index. would that work for you?
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Hang out place of novice and intermediate programmers
•
•
Join Date: Jun 2007
Posts: 21
Reputation:
Solved Threads: 0
•
•
•
•
I personally don't really get the project, can you give me an example of an input and output..? thx
So if the file contains
C++ Syntax (Toggle Plain Text)
Pans stop Pots Pots opt Sits it's snap
The out put will be
C++ Syntax (Toggle Plain Text)
Pans snap Pots Pots stop Sit it's opt
I have read the list of words into a file. To make the searching easier I created signatures of each word in the file ex. "Pans" has a signatore of "anps"
The orginal word and its signature are stored in a 2D Array like this:
C++ Syntax (Toggle Plain Text)
Pans anps stop opts Pots opts Pots opts opt opt Sit ist it's ist snap anps
Now I need to sort the 2D Array using the 2nd column. The idea is that when I move an element in column two I move the element at the same index in column 1.
Thanks
Last edited by Akilah712; Jul 14th, 2007 at 6:24 pm.
•
•
Join Date: Jun 2007
Posts: 21
Reputation:
Solved Threads: 0
•
•
•
•
you're not necessarily to use multi dimentional arrays right? i guess for me it's easier to just use one dimentional array, but compare the index. Make sure the comparison is on the same index. would that work for you?
Thanks
Um I know this is marked as solved, but I really don't get the deal with this signature malarky?
Is there any logic?
retarded.txt
pedantic.cpp
Output
Surely then all you'd need to do is eliminate duplicates from the output?
Is there any logic?
retarded.txt
Pans stop Pots opt Sit Pots it's snap
pedantic.cpp
#include <iostream> #include <fstream> #include <string> #include <vector> #include <cctype> using namespace std; struct to_lower { int operator() ( int ch ) { return std::tolower ( ch ); } }; class Anagram { //define public member functions public: string removeJunk ( string w ) { string newWord = ""; int length = w.length(); std::transform ( w.begin(), w.end(), w.begin(), to_lower() ); for ( int i = 0; i < length; i++ ) { if ( isalpha ( w[i] ) ) { newWord = newWord + w[i]; } } return newWord; } void getAnagrams ( string array ) { ifstream read ( "C:\\retarded.txt" ); //read file int sssh; sssh = array.length(); char alphabet[28] = {"@abcdefghijklmnopqrstuvwxyz"}; int hohoho[26]; int Blahh[26]; for ( int i = 1; i < 27; i++ ) { hohoho[i] = 0; } for ( int a = 0; a < sssh; a++ ) { for ( int j = 1; j < 27; j++ ) { if ( array[a] == alphabet[j] ) { hohoho[j]++; } } } string x; while ( read >> x ) { string y = removeJunk ( x ); int size; size = y.length(); for ( int i = 1; i < 27; i++ ) { Blahh[i] = 0; } for ( int i = 0; i < size; i++ ) { for ( int j = 1; j < 27; j++ ) { if ( y[i] == alphabet[j] ) { Blahh[j]++; } } } int counter = 0; for ( int k = 1; k < 27; k++ ) { if ( Blahh[k] == hohoho[k] ) { counter++; } } if ( counter == 26 ) { cout << y << " "; } } read.close(); } }; int main ( void ) { //create a test object Anagram test; ifstream in ( "C:\\retarded.txt" ); //read the file string line; while ( in >> line ) { string tmp = test.removeJunk ( line ); test.getAnagrams ( tmp ); cout << "\n"; } in.close(); cin.get(); }
Output
pans snap
stop pots pots
stop pots pots
opt
sit its
stop pots pots
sit its
pans snap
Surely then all you'd need to do is eliminate duplicates from the output?
Last edited by iamthwee; Jul 15th, 2007 at 2:02 pm.
*Voted best profile in the world*
![]() |
Similar Threads
- Sorting a 2d array of strings in C++?? (C++)
- bubble sorting in an array (C)
- Sorting arrays of pointers with function? (C)
- filling an array with strings using gets() (C)
- sorting an array of string (C)
Other Threads in the C++ Forum
- Previous Thread: Introduction/c++ uknown file locations
- Next Thread: Non existant lib file..
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






