Solved

Sorting an array of a class  by last names..

Posted on 2003-12-01
9
385 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 77
powerN  challenge 3 47
Sorting in Excel with Group Headers if the Exist 2 68
endX challenge 2 50
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now