Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,394 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,137 people online right now. Registration is fast and FREE... Join Now!




Sort alphabetically

 
Reply to this topicStart new topic

Sort alphabetically

DeeViLiSh
31 Aug, 2006 - 04:55 AM
Post #1

D.I.C Head
Group Icon

Joined: 25 Jul, 2006
Posts: 175



Thanked: 1 times
Dream Kudos: 575
My Contributions
CODE
#include <iostream.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>

struct WORDS
{
        char a[50];
        char b[50];
};

bool check;
const int word_number = 5;
void sort_abc(int, WORDS[]);
bool check_abc(WORDS[]);
void wait (float)

//-----------------------------------------------

int main()
{
        bool check;
        WORDS words[word_number];
        int entered;

        for(entered = 0; entered < 5; ++entered)
        {
                cout<<endl<<"Enter a word : ";
                cin>>words[entered].a;
                strcpy(words[entered].b, words[entered].a);
        }
        sort_abc(entered, words);



        system("PAUSE");
        return 0;



}

//-----------------------------------------------

void sort_abc(int entered, WORDS words[])
{
        int row = 0;
        cout<<"The list alphabetized is :"<<endl;

        for(row = 0;check_abc(words) != true; ++row)
        {
              if(row > 4)
              row = 0;

                        if(strcmp (words[row].a, words[row+1].a) > 0)
                        {
                                cout<<"Changing letters : "<<words[row].a<<" & "<<words[row+1].a<<endl;
                                wait(1);
                                strcpy(words[row].a, words[row+1].b);
                                strcpy(words[row+1].a, words[row].b);
                        }

             check_abc(words);
        }
        cout<<words[0].a<<endl<<words[1].a<<endl<<words[2].a<<endl<<words[3].a<<endl<<words[4].a<<endl;
}

//-----------------------------------------------

bool check_abc(WORDS words[])
{
        if(strcmp(words[0].a, words[4].a) < 0 && strcmp(words[1].a, words[4].a) < 0 && strcmp(words[2].a, words[4].a) < 0 && strcmp(words[3].a, words[4].a) < 0)
                if(strcmp(words[0].a, words[3].a) < 0 && strcmp(words[1].a, words[3].a) < 0 && strcmp(words[2].a, words[3].a) < 0)
                        if(strcmp(words[0].a, words[2].a) < 0 && strcmp(words[1].a, words[2].a) < 0)
                                if(strcmp(words[0].a, words[1].a) < 0)
        check = true;

        return check;
}

//-----------------------------------------------

void wait(float seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLK_TCK;
  while (clock() < endwait) {}
}


I'm having a huge problem with this. This records 5 words, then sorts them out in alphabetical order.
If you change the order of one or two letters, it works. But if I change the whole thing, it screws up.
I know why one part of it is buggy, it's the
words[row+1].a
that goes over 4 and gets a weird number in the memory.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Sort Alphabetically
31 Aug, 2006 - 05:32 AM
Post #2

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Wow, it must be tedious to write the sort function using the if construct!!!

Use one of the Standard Sorting Algorithms that can be found in the C and C++ Snippets Section.

And your code won't work for a certain range of strings as you are using the IF code construct using hardcoded comparisons.

Use Arrays to make this sort job easier.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Sort Alphabetically
31 Aug, 2006 - 05:51 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Here is an example of an alphabetical sort...it can be expanded to accomodate more words.

http://www.cs.xu.edu/csci175/notes97m/sorting1.html
User is offlineProfile CardPM
+Quote Post

DeeViLiSh
RE: Sort Alphabetically
31 Aug, 2006 - 05:52 AM
Post #4

D.I.C Head
Group Icon

Joined: 25 Jul, 2006
Posts: 175



Thanked: 1 times
Dream Kudos: 575
My Contributions
I think you're confusing the sort_abc() and the check_abc() functions.
The if construct for the check_abc() is temporary, the code itself looks nasty but as soon as it works, that's when I affine it and clear these things up.

I didn't find a sorting algorithm in the C++ code snippet. Could you post a link?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Sort Alphabetically
31 Aug, 2006 - 05:59 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
C++ has several types of sort built into the STL = specifically the algorithm header:

http://msdn2.microsoft.com/en-us/library/chzkfc23.aspx

http://www.fredosaurus.com/notes-cpp/algor...ort-arrays.html

http://mathbits.com/MathBits/CompSci/Arrays/Sorting.htm

Are good starting places.

Also, see the link I posted above for string sorts.
User is offlineProfile CardPM
+Quote Post

DeeViLiSh
RE: Sort Alphabetically
31 Aug, 2006 - 06:15 AM
Post #6

D.I.C Head
Group Icon

Joined: 25 Jul, 2006
Posts: 175



Thanked: 1 times
Dream Kudos: 575
My Contributions
CODE
for(row = 0;check_abc(words) != true; ++row)
        {
              if(row > limit)
              row = 0;

                        if(strcmp (words[row].a, words[row+1].a) > 0)
                        {
                                cout<<"Changing letters : "<<words[row].a<<" & "<<words[row+1].a<<endl;
                                wait(1);

                                strncpy(temp, words[row].a, 19);
                                temp[19]='\0';
                                strncpy(words[row].a, words[row+1].a, 19);
                                words[row].a[19] = '\0';
                                strncpy(words[row+1].a, temp, 19);
                                words[row+1].a[19] = '\0';


                        }

             check_abc(words);
        }
        cout<<words[0].a<<endl<<words[1].a<<endl<<words[2].a<<endl<<words[3].a<<endl<<words[4].a<<endl;
}


It worked but I don't fully understand how importing '\0' from my string to the other fixed it.
It imports the 19th character then replaces it. What's up with that??

BTW, thx Amadeus for the link.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:41AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month