User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 403,326 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,042 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 535 | Replies: 13 | Solved
Reply
Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
compumasta compumasta is offline Offline
Newbie Poster

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

  #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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,741
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 884
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #2  
May 9th, 2008
structs are nearly identical to classes, so maybe you need to add an = operator
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;
       // etc for the rest of the structure members

   }
};
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Rep Power: 0
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.

  #3  
May 9th, 2008
havent learned classes yet, so i dont really follow what your doing.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,741
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 884
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #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
void swap(if1& f1, if1& f2)
{
    if1 temp;
    temp.idnum = f1.idnum;
    temp.lastname = f1.lastname;
    temp.firstname = f1.firstname;
    temp.examscore = f1.examscore;
   temp.hwscore = f1.hwscore;

   f1.idnum = f2.idnum;
   // etc for f1

   f2.idnum = temp.idnum;
   // etc for f2
}

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 7:56 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Rep Power: 0
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.

  #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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,741
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 884
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #6  
May 9th, 2008
please post your code. Its kind of hard for me to see your monitor from where I am sitting.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Rep Power: 0
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.

  #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. ////////////////////
  320.  
Last edited by compumasta : May 9th, 2008 at 8:16 pm.
Reply With Quote  
Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Rep Power: 0
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.

  #8  
May 9th, 2008
quite literally everything works except the sorting...
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,741
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 884
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #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.

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));
  }
};
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Rep Power: 0
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.

  #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 fi