| | |
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 |
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.
Attached is the output file it produced
C++ Syntax (Toggle Plain Text)
struct if1 { string idnum;//id number for the student string lastname;//last name of student string firstname;//first name of student int examscore[maxexam];//array of all the exam scores of the student int hwscore[maxhw];//array of all the home work scores of the student void operator=(if1& f1) { idnum = f1.idnum; lastname = f1.lastname; firstname = f1.firstname; memcpy(examscore, f1.examscore, sizeof(examscore)); memcpy(hwscore,f1.hwscore,sizeof(hwscore)); } }; ... ... <snip> ... void alphasort(if1 student[], int n) //the alphasort function will sort the records into alphabetical order by last name. It expects the list as well as the total number of //names and will return the sorted array. The basic format for this function came from the class handout on bubblesorting. { if1 temp;//used as a swapping mechanism int i; int j;// used for implementing for loop checks int f=1;//used for checking letters after the first for (i=0; i < n-1; i++) { for (j=0; j < n-(i+1); j++) { if(student[j].lastname[0] > student[j+1].lastname[0]) { temp = student[j]; student[j] = student[j+1]; student[j+1] = temp; } } } }
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.
•
•
Join Date: May 2008
Posts: 9
Reputation:
Solved Threads: 0
Re: C++ Bubble sort not working with array of records, does work with simple array.
0
#12 May 9th, 2008
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
change it to this and it will work
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.
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.
C++ Syntax (Toggle Plain Text)
void alphasort(if1 student[], int n) //the alphasort function will sort the records into alphabetical order by last name. It expects the list as well as the total number of //names and will return the sorted array. The basic format for this function came from the class handout on bubblesorting. { if1 temp;//used as a swapping mechanism int i; int j;// used for implementing for loop checks int f=1;//used for checking letters after the first for (i=0; i < n-1; i++) { for (j=0; j < n-(i+1); j++) { string n1 = student[j].lastname; string n2 = student[j+1].lastname; if(n1 == n2) { n1 = student[j].firstname; n2 = student[j+1].firstname; } if(n1 > n2) { temp = student[j]; student[j] = student[j+1]; student[j+1] = temp; } } } }
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.
•
•
Join Date: May 2008
Posts: 9
Reputation:
Solved Threads: 0
Re: C++ Bubble sort not working with array of records, does work with simple array.
0
#14 May 9th, 2008
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Check the size of a file
- Next Thread: Getting a constant conversion error
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






