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: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Solved Threads: 0
compumasta compumasta is offline Offline
Newbie Poster

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

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

im new here, so i hope i didnt post in the wrong area or something...


using an array of structs this function does not work, while the same function using only a simple array of strings does work properly.

i cant figure out why there would be any difference.

the array of structs will not sort alphabetically using all letters in the name if necessary while the simple array will sort 100% correct.

any ideas?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,374
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
  #2
May 9th, 2008
structs are nearly identical to classes, so maybe you need to add an = operator
  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. // etc for the rest of the structure members
  13.  
  14. }
  15. };
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
  #3
May 9th, 2008
havent learned classes yet, so i dont really follow what your doing.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,374
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
  #4
May 9th, 2008
What I'm doing is writing my own version of the = operator. So when you way temp = student[j]; the compiler will call my version of the = operator instead of the compiler's default version. The parameter to the operator is the equilivent of passing student[j] by reference to any normal function.

I believe the reason your sort does not work as you wrote it is because the structure contains c++ STL class -- std::string. In such cases when the structure includes other class objects you should write the overloaded = operator.

That might also be similar to writing a swap function
  1. void swap(if1& f1, if1& f2)
  2. {
  3. if1 temp;
  4. temp.idnum = f1.idnum;
  5. temp.lastname = f1.lastname;
  6. temp.firstname = f1.firstname;
  7. temp.examscore = f1.examscore;
  8. temp.hwscore = f1.hwscore;
  9.  
  10. f1.idnum = f2.idnum;
  11. // etc for f1
  12.  
  13. f2.idnum = temp.idnum;
  14. // etc for f2
  15. }

As you can see, the above code is pretty lengthly. The overloaded = operator is consideratly shorter and less pron to typing errors.
Last edited by Ancient Dragon; May 9th, 2008 at 8:56 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
  #5
May 9th, 2008
i tried implementing your code into my program and it does compile without any errors. unfortunately the same problem exists when it sorts the names it still only sorts them by the first letter and does not compare the rest of the name in the event that the first letters match.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,374
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
  #6
May 9th, 2008
please post your code. Its kind of hard for me to see your monitor from where I am sitting.
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
  #7
May 9th, 2008
the entire program you mean?

oh and also, using bubblesort was a requirement for this program...

  1.  
  2. ///////////////////
  3. //
  4. //This program will use filestreams to read in some information from two files about students including their first and last name,
  5. //id number and grades on their homeworks and exams. It will sort the list of students by their last name and calculate percentage and
  6. //letter grades. The second input file containing the scores for exams and homeworks must contain a 'Q' symbol at the end of each line and
  7. //and an end line character after the last line in the file.
  8. //The output of this program involves four outputs. the first is a list of names, sorted alphabetically by last name and their
  9. //corresponding id numbers. The second is the list, again sorted the same way with the set of homework scores. if no homework score was
  10. //read, there would be a 0 in the place of that score.
  11. //The third output contains the list of sorted names and their corresponding scores on exams. again, if there is no score for an exam,
  12. //there would be a 0 in the place of that score.
  13. //The fourth and final output displays the sorted names and a percentage of their total grade(points earned vs. total points). and a
  14. //letter grade corresponding to the percentage(A,B,C,D, or F).
  15.  
  16. #include <iostream>
  17. #include <iomanip>
  18. #include <cctype>
  19. #include <cmath>
  20. #include <string>
  21. #include <fstream>
  22. using namespace std;
  23.  
  24. const int maxstu(30);//maximum students per class constant
  25. const int maxexam(3);//maximum exams per student constant
  26. const int maxhw(10);//maximum home work assignments per student constant
  27.  
  28. struct if1
  29. {
  30. string idnum;//id number for the student
  31. string lastname;//last name of student
  32. string firstname;//first name of student
  33. int examscore[maxexam];//array of all the exam scores of the student
  34. int hwscore[maxhw];//array of all the home work scores of the student
  35.  
  36. };
  37.  
  38. void nameread(ifstream&, if1[], int&);
  39. void currentname(string&, string&, int, int);
  40. void alphasort(if1[], int);
  41. void dataread(ifstream&, if1[], int);
  42. void gradecalc(float[], char[], int);
  43.  
  44. int main()
  45. {
  46. string input1;//name of first input file
  47. string input2;//name of second input file
  48. string output;//name of output file
  49. ifstream infile1;//the first input file via filestreams
  50. ifstream infile2;//the second input file via filestreams
  51. ofstream outfile;//the output file accessed via filestreams
  52. if1 student[maxstu];//array of type if1 that stores all student records
  53. int n;//counter used for totalling the number of students in the class
  54. int total[maxstu];//array for storing total points earned by each student
  55. float percent[maxstu];//array for storing percentage grades for each student
  56. char grade[maxstu];//array for storing letter grades for each student
  57.  
  58. cout << "please enter the name of the first input file" << endl;
  59. cin >> input1;
  60.  
  61. cout << "please enter the name of the second input file" << endl;
  62. cin >> input2;
  63.  
  64. cout << "please enter the name of the output file" << endl;
  65. cin >> output;
  66.  
  67. infile1.open(input1.c_str());
  68. infile2.open(input2.c_str());
  69. outfile.open(output.c_str());
  70.  
  71. nameread(infile1, student, n);
  72.  
  73. alphasort(student, n);
  74.  
  75. for(int v=0; v <n; v++)
  76. {
  77. total[v] = 0;
  78. }
  79.  
  80. for (int i = 0; i < n; i++)
  81. {
  82. for(int h=0; h < 10; h++)
  83. {
  84. student[i].hwscore[h] = 0;
  85. }
  86. for(int e=0; e < 4; e++)
  87. {
  88. student[i].examscore[e] = 0;
  89. }
  90. }
  91. dataread(infile2, student, n);
  92.  
  93. outfile << left << setw(25) << "NAME" << right << setw(17) << "ID#" << endl;
  94.  
  95. for(int j = 0; j < n; j++)
  96. {
  97. outfile << left << setw(student[j].lastname.length()) << student[j].lastname << ", " << setw(15) << student[j].firstname << setw(13-student[j].lastname.length()) << " " << right << setw(12) << student[j].idnum << endl;
  98. }
  99. outfile << endl;
  100.  
  101. outfile << right << setw(42) << "HOMEWORK" << endl;
  102.  
  103. outfile << left << setw(28) << "NAME";
  104.  
  105. outfile << left << setw(1) << " ";
  106.  
  107. for(int x = 1; x < 11; x++)
  108. {
  109. outfile << right << setw(6) << x;
  110. }
  111. outfile << endl;
  112.  
  113. for(int u = 0; u < n; u++)
  114. {
  115. outfile << left << setw(student[u].lastname.length()) << student[u].lastname << ", " << setw(14) << student[u].firstname << setw(13-student[u].lastname.length()) << " ";
  116.  
  117. for(int f = 0; f < 10; f++)
  118. {
  119. outfile << right << setw(6) << student[u].hwscore[f];
  120. }
  121. outfile << endl;
  122. }
  123. outfile << endl;
  124.  
  125. outfile << right << setw(42) << "EXAMS" << endl;
  126.  
  127. outfile << left << setw(28) << "NAME";
  128.  
  129. outfile << left << setw(1) << " ";
  130.  
  131. for(int z = 1; z < 4; z++)
  132. {
  133. outfile << right << setw(6) << z;
  134. }
  135.  
  136. outfile << endl;
  137.  
  138. for(int p = 0; p < n; p++)
  139. {
  140. outfile << left << setw(student[p].lastname.length()) << student[p].lastname << ", " << setw(14) << student[p].firstname << setw(13-student[p].lastname.length()) << " ";
  141.  
  142. for(int o = 0; o < 3; o++)
  143. {
  144. outfile << right << setw(6) << student[p].examscore[o];
  145. }
  146. outfile << endl;
  147. }
  148.  
  149. for(int r=0; r < n; r++)
  150. {
  151. for(int q=0; q < 3; q++)
  152. {
  153. total[r] = total[r] + student[r].examscore[q];
  154. }
  155. }
  156. for(int w=0; w< n; w++)
  157. {
  158. for(int t=0; t < 10; t++)
  159. {
  160. total[w] = total[w] + student[w].hwscore[t];
  161. }
  162. }
  163.  
  164. for(int y=0; y < n; y++)
  165. {
  166. percent[y]= static_cast<float>(total[y])/5.0;
  167. }
  168.  
  169. gradecalc(percent, grade, n);
  170.  
  171. outfile << endl;
  172.  
  173. outfile << left << setw(8) << "ID#" << setw(25) << "NAME" << right << setw(14) << "PCT" << setw(8) << "GRADE" << endl;
  174.  
  175. outfile << fixed << showpoint << setprecision(2);
  176.  
  177. for(int b=0; b < n; b++)
  178. {
  179. outfile << right << setw(3) << student[b].idnum << left << setw(5) << " " << setw(student[b].lastname.length()) << student[b].lastname << ", " << setw(14) << student[b].firstname << setw(13-student[b].lastname.length()) << " " << right << setw(10) << percent[b] << setw(8) << grade[b] << endl;
  180. }
  181.  
  182. return 0;
  183. }
  184.  
  185. void nameread(ifstream& in, if1 student[], int& n)
  186. //the nameread function reads in the names of the students along with their id numbers. it also counts the number of students as they come
  187. //in and calculates their name lengths for the formatting function.
  188. //it is passed the filestream for the first input file as well as the student array of records and the counter. It returns the names
  189. //formatted but not sorted.
  190. {
  191. n=0;
  192. int firstnamelength=0;//length of first name of the student initialized to zero
  193. int lastnamelength=0;//length of the last name of the student initialized to zero
  194. in >> student[n].idnum;
  195. in >> student[n].lastname;
  196. in >> student[n].firstname;
  197. while (!in.eof())
  198. {
  199. firstnamelength =student[n].firstname.length();
  200. lastnamelength = student[n].lastname.length();
  201. currentname(student[n].lastname, student[n].firstname, firstnamelength, lastnamelength);
  202. n++;
  203. in >> student[n].idnum;
  204. in >> student[n].lastname;
  205. in >> student[n].firstname;
  206. }
  207. }
  208.  
  209. void currentname(string& lastname, string& firstname, int firstnamelength, int lastnamelength)
  210. // the current name funtion is called from the read function. On each loop
  211. // it is passed that name, unformatted and formats it.
  212. //It will then pass back the name into the proper place in the array,
  213. //properly formatted.
  214. {
  215. lastname[0] = toupper(lastname[0]);
  216. firstname[0] = toupper(firstname[0]);
  217. for(int j = 1; j < lastnamelength; j++)
  218. {
  219. lastname[j] = tolower(lastname[j]);
  220. }
  221. for(int f=1; f < firstnamelength; f++)
  222. {
  223. firstname[f] = tolower(firstname[f]);
  224. }
  225. }
  226.  
  227. void alphasort(if1 student[], int n)
  228. //the alphasort function will sort the records into alphabetical order by last name. It expects the list as well as the total number of
  229. //names and will return the sorted array. The basic format for this function came from the class handout on bubblesorting.
  230. {
  231. if1 temp;//used as a swapping mechanism
  232. int i; int j;// used for implementing for loop checks
  233. int f=1;//used for checking letters after the first
  234. for (i=0; i < n-1; i++)
  235. for (j=0; j < n-(i+1); j++)
  236. if(student[j].lastname[0] > student[j+1].lastname[0])
  237. {
  238. temp = student[j];
  239. student[j] = student[j+1];
  240. student[j+1] = temp;
  241. }
  242. else if(student[j].lastname[0] == student[j+1].lastname[0])
  243. {
  244. while(student[j].lastname[f] == student[j+1].lastname[f])
  245. {
  246. f++;
  247. }
  248. if (student[j].lastname[f] > student[j+1].lastname[f])
  249. {
  250. temp = student[j];
  251. student[j] = student[j+1];
  252. student[j+1] = temp;
  253. }
  254. }
  255. }
  256.  
  257. void dataread(ifstream& in, if1 student[], int n)
  258. //the dataread function reads in and stores exam scores and home work scores into the proper place in the record for each student.
  259. //it is passed the filestream of the second input file as well as the student array of records and the counter.
  260. //it reads in an id number and checks for a match to a students record and if it exists, stores the necessary information into proper
  261. //spots in the record. if no match is found in all the list of records, the rest of that line is ignored and the next id number is read
  262. //in and checked in the same manner.
  263. //it returns the array of records complete with previously stored information and the newly aquired exam and homework scores.
  264. {
  265. string idnum;//id number from file 2 for comparison to the list of student id numbers
  266. char type;//type character for comparison of if it is an exam or homework score or if it is the end of line character marked by Q
  267. int number;//variable for which number of exam score is being read next.
  268. int number2;//variable for the grade of the homework score being read next.
  269. in >> idnum;
  270. while(!in.eof())
  271. {
  272. for (int j =0; j < n; j++)
  273. {
  274. if (idnum == student[j].idnum)
  275. {
  276. in >> type;
  277. while(type != 'Q')
  278. {
  279. if(type == 'E')
  280. {
  281. in >> number;
  282. in >> student[j].examscore[number-1];
  283. }
  284. else if(type == 'H')
  285. {
  286. in >> number2;
  287. in >> student[j].hwscore[number2-1];
  288. }
  289. in >> type;
  290. }
  291. }
  292. }
  293. in.ignore(1000,'\n');
  294. in >> idnum;
  295. }
  296. }
  297.  
  298. void gradecalc(float percent[], char grade[], int n)
  299. //the gradecalc function uses the array of percentages for the student and finds the proper letter grade for each percentage.
  300. //it will store the letter grade into the properly ordered spot in the grade array.
  301. //it is passed the percentages in an array, and an empty letter grade array, as well as the counter total.
  302. //it will pass back the unaltered percentages and a list of letter grades for each student.
  303. {
  304. for(int j = 0; j < n; j++)
  305. {
  306. if(percent[j] >= 90.0)
  307. grade[j] = 'A';
  308. else if(percent[j] < 90 && percent[j] >= 80)
  309. grade[j] = 'B';
  310. else if(percent[j] < 80 && percent[j] >= 70)
  311. grade[j] = 'C';
  312. else if(percent[j] < 70 && percent[j] >= 60)
  313. grade[j] = 'D';
  314. else if(percent[j] < 60)
  315. grade[j] = 'F';
  316. }
  317. }
  318.  
  319. ////////////////////
Last edited by compumasta; May 9th, 2008 at 9:16 pm.
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
  #8
May 9th, 2008
quite literally everything works except the sorting...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,374
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
  #9
May 9th, 2008
Would you also post the input files? Just attach them to your post .

Here is what I had in mind for the structure, but I need the input files for testing.

  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. };
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
  #10
May 9th, 2008
ill give you the data because the files i use dont have any extensions and i do not know which ones will still keep everything in order.

(the names are completely made up...

input 1
567 white robert
43 blackburn donna
722 Grey jAMes
19 bRoWn jAnE
25 greta jamescoonsilr
8 josephinalar billy
0 josephinalay jamescoonsilr
999 jimi hendricks
382 billy bob
2 a d

input file 2

722 E 1 95 E 2 89 E 3 90 Q
567 E 1 67 H 1 20 H 5 20 E 2 76 Q
83 this line should be ignored, 83 is not a valid ID#
19 H 1 20 H 2 20 H 3 19 H 4 18 H 5 16 Q
43 E 3 91 Q
19 H 6 20 H 7 20 H 8 20 H 9 20 H 10 18 Q
1 me 1903094389 lkadjf
722 H 5 20 H 6 20 H 7 20 Q
19 E 1 85 E 2 90 Q
567 E 3 80 Q
722 H 4 15 H 3 14 H 2 20 H 1 20 Q
25 H 10 20 E 1 90 Q
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