C++ Bubble sort not working with array of records, does work with simple array.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Bubble sort not working with array of records, does work with simple array.

 
0
  #11
May 9th, 2008
The program crashes during sort because it is attempting to reference non-existant index value. I simplified alphasort() algorithm as shown below and everything works ok.
  1. struct if1
  2. {
  3. string idnum;//id number for the student
  4. string lastname;//last name of student
  5. string firstname;//first name of student
  6. int examscore[maxexam];//array of all the exam scores of the student
  7. int hwscore[maxhw];//array of all the home work scores of the student
  8.  
  9. void operator=(if1& f1)
  10. {
  11. idnum = f1.idnum;
  12. lastname = f1.lastname;
  13. firstname = f1.firstname;
  14. memcpy(examscore, f1.examscore, sizeof(examscore));
  15. memcpy(hwscore,f1.hwscore,sizeof(hwscore));
  16. }
  17. };
  18.  
  19. ...
  20. ...
  21. <snip>
  22. ...
  23.  
  24. void alphasort(if1 student[], int n)
  25. //the alphasort function will sort the records into alphabetical order by last name. It expects the list as well as the total number of
  26. //names and will return the sorted array. The basic format for this function came from the class handout on bubblesorting.
  27. {
  28. if1 temp;//used as a swapping mechanism
  29. int i; int j;// used for implementing for loop checks
  30. int f=1;//used for checking letters after the first
  31. for (i=0; i < n-1; i++)
  32. {
  33. for (j=0; j < n-(i+1); j++)
  34. {
  35. if(student[j].lastname[0] > student[j+1].lastname[0])
  36. {
  37. temp = student[j];
  38. student[j] = student[j+1];
  39. student[j+1] = temp;
  40. }
  41. }
  42. }
  43. }

Attached is the output file it produced
Last edited by Ancient Dragon; Mar 10th, 2009 at 1:13 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Solved Threads: 0
compumasta compumasta is offline Offline
Newbie Poster

Re: C++ Bubble sort not working with array of records, does work with simple array.

 
0
  #12
May 9th, 2008
ahh but see thats the problem. it only sorts them alphabetically by the first letter and not the leters thereafter too.

its still not quite right, see how brown comes before billy?

thats not alphabetical.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Bubble sort not working with array of records, does work with simple array.

 
0
  #13
May 9th, 2008
Oh yes, here the problem
if(student[j].lastname[0] > student[j+1].lastname[0])
change it to this and it will work
if(student[j].lastname > student[j+1].lastname)
Actually I would think you need to sort by both last name and first name, so that when two or more people have the same last name the function will sort those by first name.

  1. void alphasort(if1 student[], int n)
  2. //the alphasort function will sort the records into alphabetical order by last name. It expects the list as well as the total number of
  3. //names and will return the sorted array. The basic format for this function came from the class handout on bubblesorting.
  4. {
  5. if1 temp;//used as a swapping mechanism
  6. int i; int j;// used for implementing for loop checks
  7. int f=1;//used for checking letters after the first
  8. for (i=0; i < n-1; i++)
  9. {
  10. for (j=0; j < n-(i+1); j++)
  11. {
  12. string n1 = student[j].lastname;
  13. string n2 = student[j+1].lastname;
  14. if(n1 == n2)
  15. {
  16. n1 = student[j].firstname;
  17. n2 = student[j+1].firstname;
  18. }
  19. if(n1 > n2)
  20. {
  21. temp = student[j];
  22. student[j] = student[j+1];
  23. student[j+1] = temp;
  24. }
  25. }
  26. }
  27. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Solved Threads: 0
compumasta compumasta is offline Offline
Newbie Poster

Re: C++ Bubble sort not working with array of records, does work with simple array.

 
0
  #14
May 9th, 2008
thank you sir. it works now. we were told that there would be no students with the same last name, so that wasnt an issue, but thanks for finding it.

thanks for you help!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC