Solved

Sorting an array of a class  by last names..

Posted on 2003-12-01
9
407 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

791 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