string sort madness

This is a discussion on string sort madness within the C++ Programming forums, part of the General Programming Boards category; OK, so I've got these two functions for sorting a dictionary: Code: void dicsort(int diclength, char dic[][stringlength]) { int i, ...

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    string sort madness

    OK, so I've got these two functions for sorting a dictionary:
    Code:
    void dicsort(int diclength, char dic[][stringlength]) {
      int i, j;
      char temp[stringlength];
      for (i = 0; i < diclength; ++i)
        for (j = 0; j < diclength - 1; ++j) {
          if (strcomp(dic[j],dic[j+1]) > 0) {
            strcopy(dic[j],temp);
            strcopy(dic[j+1],dic[j]);
            strcopy(temp,dic[j]);
          }
        }
    }
    
    //-1 if str1 < str2, 0 if str1 == str2, 1 if str1 > str2
    int strcomp(char* str1, char* str2) {
      if (strequal(str1,str2))
        return 0;
      int i;
      for (i = 0; i <= strlength(str1); ++i) {
        if (i > strlength(str2))
          return 1;
        if (str1[i] < str2[i])
          return -1;
        if (str1[i] > str2[i])
          return 1;
      }
      return 0;
    }
    but when I run them, it seems I'm sending the same two strings to strcomp() every time. This is baffling me, and I can't use any <cstring> goodness. I know that there aren't any duplicates in dic, and I know all my other strwhatever()'s work fine. Does anybody have any idea why this is happening?
    Last edited by ichijoji; 11-24-2003 at 05:16 PM.
    Illusion and reality become impartiality and confidence.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Look what's happening here:

    >> strcopy(dic[j],temp);
    >> strcopy(dic[j+1],dic[j]);
    >> strcopy(temp,dic[j]);

    You are using 'dic[j]' to preserve 'temp' - is that what you wanted to do?

    One more thing:

    >> for (i = 0; i <= strlength(str1); ++i)
    >> if (i > strlength(str2))

    Each iteration of the loop you are calling 'strlength()' on 'str1' and 'str2'. You only need to calculate these lengths once.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    The strcopy function I made's prototype looks like:
    Code:
    void strcopy(char* from, char* to);
    , so the calls I have should be swapping str1 and str2 should work ok. Is there some standard function that I could be having a conflict with? I'm still stumped.
    Illusion and reality become impartiality and confidence.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21