Solved

Sorting an array of a class  by last names..

Posted on 2003-12-01
9
420 Views
Last Modified: 2010-04-17
Im looking for some help or a good website with sorting information.  Im not going to post all my code. I had no problem sorting the array by voterid. Here is code i used for that:

void sortid(voter v1[], int size)
 {
    bool doMore;
    do {
        doMore = false;  // assume this is last pass over array
        for (int i=0; i<size-1; i++) {
            if (v1[i].idnumber > v1[i+1].idnumber) {
                // exchange elements
                voter temp = v1[i]; v1[i] = v1[i+1]; v1[i+1] = temp;
                doMore = true;  // after exchange, must look again
            }
        }
    } while (doMore);
}

The problem now is that the array needs to be sorted by last name. This is where im having problems. Not going to post all of code.

Class Declaration

class voter
{

friend void sortid(voter v1[], int size); <--- this is for sortby id number
friend void sortlastname(voter v1[], int size); <----sort by lastname


private:      
      int  idnumber;
      char lastname [25];
      char firstname[25];
      char party         [2];
      int  birthyear;
      int  currentage;

public:
      voter();
      voter(int,char[], char[], char[], int, int);
      voter(const voter&);
      void Add();
      void Edit();
      void operator = (const voter&);
      void print(int);
      void disksave(ofstream& outfile);
      ~voter();

};

and now here is the code i attempt to use for sorting by lastname.

void sortArray (voter n[], int size) {

    char temp[25; // temporary buffer for strings
    int swap;
   
    do {
        swap = 0;
        for (int count = 0; count < (size- 1); count++) {
            if (strcmp(n[count].lastname, n[count + 1].lastname) > 0) {
                strcpy(temp, n[count].lastname); // temp = n[count.lastname]
                strcpy(n[count].lastname, n[count + 1].lastname); // n[count] = n[count + 1]
                strcpy(n[count + 1].lastname, temp); // n[count+1].lastname = temp
                swap = 1;
            };
        };
    } while (swap != 0);

}

0
Comment
Question by:kaladran1974
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
9 Comments
 
LVL 22

Accepted Solution

by:
cookre earned 125 total points
ID: 9854052
You don't say what the problem is, so here are some guesses:

* strcmp is case dependant, use stricmp for case insensitive
* strcmp presumes single byte characters, use wcscmp or mbscmp for wide- or multi-byte
* strcmp is ascii collating and locale independant.  If locale matters, use strcoll
0
 

Author Comment

by:kaladran1974
ID: 9854361
The problem is they are not comparing, the list does not change..  made some changes to it and now i just get crashes.
0
 
LVL 22

Expert Comment

by:cookre
ID: 9859237
Just crashing sure sounds like you're trashing something.

At the top of sortArray I'd dump out 'size' and 'n' to make sure good stuff is coming in.  For example, if size>25 you're in dep dookie.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:pv12206
ID: 9864324
Selection sort should solve the problem. You have the idea here but I think instead of use do while loop, use both for loop to keep the current position of the index you have not sorted.

The algorithm:

for (index =0; index < size; index++)
{
   step 1: find the location of the smallest element first. You don't need to swap everytime you find the smaller element.
   step 2: Swap the smallest  element with n[index]. there's also a swap function predefine for string.

}

The Code should look like:
void sortArray (voter n[], int size) {
  int i, j;
  int min;  
  for ( i=0; i < size -1; i++ ) { //size- 1 because the last  element is automaticly in order
    min = i; // min is the index of smallest element that compare to the rest.
    /*this loop find the smallest element and */
    for (j = i+1; j < size; j++)
       if ( strcmp(n[j].lastname, n[min].lastname < 0 )
           min = j;
   /*after this for loop min should be the index  of smallest element
    n[i].lastname.swap(n[min].lastname);
  }
}

Ok this should work. good luck
0
 
LVL 22

Expert Comment

by:cookre
ID: 9889037
How's it going?
0
 

Expert Comment

by:pv12206
ID: 9891290
I'm miss that. I relize that what you're sorting here is the array of voter by lastname. not sorting the last name. So the swap code should be:

voter temp; //holder for swaping
....
..
....
temp = n[i];
n[i] = n[min];
n[min] = temp;

 Ofcourse, there is no "= " sign for type voter yet. In your class definition you need to have an Operator Overloading function to define the meaning of the " = " sign for voter type.
 I hope this will help you to solve the problem.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Progress

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question