Sorting a 2D Array of Strings

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 21
Reputation: Akilah712 is an unknown quantity at this point 
Solved Threads: 0
Akilah712 Akilah712 is offline Offline
Newbie Poster

Sorting a 2D Array of Strings

 
0
  #1
Jul 14th, 2007
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
Last edited by Akilah712; Jul 14th, 2007 at 4:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,730
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Sorting a 2D Array of Strings

 
0
  #2
Jul 14th, 2007
If you can sort one column you can sort two. It's just a matter of using the second column to drive your sort, and making sure that when you copy a row, you copy both columns.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 83
Reputation: ProgrammersTalk is an unknown quantity at this point 
Solved Threads: 7
ProgrammersTalk's Avatar
ProgrammersTalk ProgrammersTalk is offline Offline
Junior Poster in Training

Re: Sorting a 2D Array of Strings

 
0
  #3
Jul 14th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 21
Reputation: Akilah712 is an unknown quantity at this point 
Solved Threads: 0
Akilah712 Akilah712 is offline Offline
Newbie Poster

Re: Sorting a 2D Array of Strings

 
0
  #4
Jul 14th, 2007
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.

  1. for (int i = 0; i< the-array-lenght; i++)
  2. {
  3. get the array[i] and compare it to the next
  4. compare arry[i] to array[2]
  5. swap if smaller
  6. don't swap if they are equal
  7.  
  8. }
  9.  
  10. ???
  11.  
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 83
Reputation: ProgrammersTalk is an unknown quantity at this point 
Solved Threads: 7
ProgrammersTalk's Avatar
ProgrammersTalk ProgrammersTalk is offline Offline
Junior Poster in Training

Re: Sorting a 2D Array of Strings

 
0
  #5
Jul 14th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 21
Reputation: Akilah712 is an unknown quantity at this point 
Solved Threads: 0
Akilah712 Akilah712 is offline Offline
Newbie Poster

Re: Sorting a 2D Array of Strings

 
0
  #6
Jul 14th, 2007
Originally Posted by ProgrammersTalk View Post
I personally don't really get the project, can you give me an example of an input and output..? thx
The input is a file of words. The program has to find all the anagrams in the file and print them on the same line.

So if the file contains
  1.  
  2. Pans
  3. stop
  4. Pots
  5. Pots
  6. opt
  7. Sits
  8. it's
  9. snap
  10.  

The out put will be
  1.  
  2. Pans snap
  3. Pots Pots stop
  4. Sit it's
  5. opt
  6.  

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:
  1.  
  2. Pans anps
  3. stop opts
  4. Pots opts
  5. Pots opts
  6. opt opt
  7. Sit ist
  8. it's ist
  9. snap anps
  10.  

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 21
Reputation: Akilah712 is an unknown quantity at this point 
Solved Threads: 0
Akilah712 Akilah712 is offline Offline
Newbie Poster

Re: Sorting a 2D Array of Strings

 
0
  #7
Jul 14th, 2007
Originally Posted by ProgrammersTalk View Post
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?
I chose a 2D array because I have to keep track of the orginal words so that I can display them in the correct order.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 21
Reputation: Akilah712 is an unknown quantity at this point 
Solved Threads: 0
Akilah712 Akilah712 is offline Offline
Newbie Poster

Re: Sorting a 2D Array of Strings

 
0
  #8
Jul 15th, 2007
I have managed to sort the 2D array, finally.


Thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Sorting a 2D Array of Strings

 
0
  #9
Jul 15th, 2007
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
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*
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 21
Reputation: Akilah712 is an unknown quantity at this point 
Solved Threads: 0
Akilah712 Akilah712 is offline Offline
Newbie Poster

Re: Sorting a 2D Array of Strings

 
0
  #10
Jul 15th, 2007
I agree.

From what I was told, using the signatures made the process faster.

*shrug*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC