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

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 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.

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?

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

Pans
          stop
          Pots
          Pots
          opt
          Sits
          it's
          snap

The out put will be

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:

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

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

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?

I agree.

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

*shrug*

This question has already been answered. Start a new discussion instead.