DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2007
    Posts
    18

    Sorting states by unique characters help

    Ok so I have a program where I am supposed to load a txt file into an array and have it sort the states according to the number of unique letter that they have. I have this working almost like it is supposed to I am only having problems with two states... 'Hawaii' and 'Tennessee' now I have noticed the common factor here is the duplicate last letter. I was wondering if someone would look through this code and tell me how I can remedy this problem.

    // stateArray is the array that the states are loaded in alphabetical order
    // numArray is an array that is supposed to hold the number of unique
    //characters by this sort

    void fillNum(char stateArray[][80], int numArray[])
    {
    int firstVar;
    char lowerArray[80];
    int a;
    int b;
    int c = 0;
    int d = 0;
    for (firstVar = 0; firstVar < 50; ++firstVar)
    {
    lower(lowerArray, stateArray, firstVar);
    for (a = 0; lowerArray[a] != '\0'; ++a)
    {
    for (b = a + 1; lowerArray[b] != '\0'; ++b)
    {
    if (isspace(lowerArray[a]))
    {
    c = 0;
    break;
    }
    if (lowerArray[a] == lowerArray[b])
    {
    c = 0;
    break;
    }
    else
    {
    c = 1;
    }
    }
    d = d + c;
    }
    numArray[firstVar] = d;
    d = 0;
    }
    }

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    tennessee should not break it if it were related to the last part of the string because it already has counted the 'e' earlier. The bug might be something else?

  3. #3
    Join Date
    Mar 2007
    Posts
    18
    I don't know why it made a difference but I reversed the sort and if fixed it

    Code:
    void fillNum(char stateArray[][80], int numArray[])
    {
       int firstVar;
       char lowerArray[80];
       int a;
       int b;
       int c = 0;
       int d = 0;
       for (firstVar = 0; firstVar < 50; ++firstVar)
       {
          lower(lowerArray, stateArray, firstVar);
          for (a = 1; lowerArray[a] != '\0'; ++a)
          {
             for (b = a - 1; lowerArray[b] != '\0'; --b)
             {
                if (isspace(lowerArray[a]))
                {
                   c = 0;
                   break;
                }
                if (lowerArray[a] == lowerArray[b])
                {
                   c = 0;
                   break;
                }
                else
                {
                   c = 1;
        }
             }
             d = d + c;
          }
          numArray[firstVar] = d;
          d = 0;
       }
    }
    Last edited by emohs; 03-22-2007 at 11:16 PM.

Similar Threads

  1. unique sorting problem in JAVA:Please help
    By Subhra_786 in forum Java
    Replies: 8
    Last Post: 03-19-2007, 08:49 AM
  2. STL map ( no unique element )
    By overule in forum C++
    Replies: 3
    Last Post: 02-25-2007, 08:23 AM
  3. special characters in resource bundle
    By ewsrean in forum Java
    Replies: 0
    Last Post: 08-07-2006, 08:11 AM
  4. Replies: 0
    Last Post: 02-16-2006, 01:27 PM
  5. Replies: 0
    Last Post: 06-14-2000, 10:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links