944,127 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1597
  • C++ RSS
Aug 4th, 2005
0

Is it wrong to have a very long program if i did not find another way?

Expand Post »
HI:

Maybe some of you read my thread about how to sort a file that contained 17 students data:

ID LASTNAME FIRSTNAME 10 SCORES

My program had to read this file, sort the names alphabetically, then calculate average score and letter grades.

Finally send all data to a new file that contained the Lastname, a comma, firstname, 10 scores, average and letter grade for each student.

Also the file should look formatted( all justified).

I tried to do it in many ways, some of you gave me great ideas.

This is the program I created, but it is very long.

1) I read file and input data into arrays
2) concatenate the last name + , + firstname;

3)while reading i calculate average and grades

4) create a swap and sort function to sort names

5) Call function

6) output each student individually (i wrote on a paper the index each student had before sorting names, so after sorting i just add scores, grades and average using the idex the student had before to not loose the student original data)

While processing each student, i format each one of them

"i know it is a very tedious program, but it does what it is suppossed to do"

so, i just wanted to know if you think that the teacher would accept this kind of programs or not!

THIS IS MY LONG LONG PROGRAM!

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. // Swap (): swaps two names if not in order.
  10.  
  11. void Swap (string& x, string& y)
  12. { string temp;
  13. temp=x;
  14. x=y;
  15. y=temp;
  16. }
  17.  
  18. // Sort(): function that calls the names from the main program and
  19. // sorts them alphabetically.
  20.  
  21. void Sort (string full[])
  22. {int small_pos;
  23. string small;
  24. for (int k=0; k<17; k++)
  25. {small =full[k];
  26. for (int n=k; n<17; n++)
  27. if (full[n]<=small)
  28. {small_pos=n;
  29. small=full[n];
  30. }
  31. Swap (full[k], full[small_pos]);
  32.  
  33. }
  34. }
  35.  
  36. int main(int argc, char *argv[])
  37. {
  38.  
  39.  
  40. ifstream infile;
  41. ofstream outfile;
  42. int ID;
  43. infile.open("c:\\students.txt");
  44. outfile.open("c:\\grades.txt");
  45. string lastN[17], firstN[17], fullname[17];
  46. int scores[17][10];
  47. float ave[17];
  48. char grade[17];
  49. cout.setf(ios::fixed);
  50. cout.setf(ios::showpoint);
  51.  
  52. for(int count=0; count<17; count++)
  53. {
  54. float sum=0; // the cout statements inside this for
  55. infile>>ID; // loop are just to see it on the screen
  56. infile>>lastN[count]; // before sorting. I also calculate
  57. infile>>firstN[count]; // average and grades inside this loop.
  58. fullname[count]=lastN[count]+","+firstN[count];
  59. cout.precision(3);
  60. cout << showpoint;
  61. cout.width(20);
  62. cout<<left<<fullname[count]<<" ";
  63. for(int n=0; n<10; n++)
  64. {infile>>scores[count][n];
  65.  
  66. cout.precision(3);
  67. cout << showpoint;
  68. cout.width(3);
  69. cout<<right<<scores[count][n]<<" ";
  70. sum+=scores[count][n];
  71.  
  72. }
  73. ave[count]=sum/10;
  74. cout.precision(1);
  75. cout << showpoint;
  76. cout.width(3);
  77. cout<<ave[count]<<" ";
  78. if (ave[count]>=90.0)
  79. {grade[count]='A';
  80. cout<<grade[count];
  81. }
  82. else
  83. { if (ave[count]>=80.0)
  84. {grade[count]='B';
  85. cout<<grade[count];
  86. }
  87. else
  88. { if (ave[count]>=70.0)
  89. {grade[count]='C';
  90. cout<<grade[count];
  91. }
  92. else
  93. { if (ave[count]>=60.0)
  94. {grade[count]='D';
  95. cout<<grade[count];
  96. }
  97. else
  98. {grade[count]='F';
  99. cout<<grade[count];
  100. }
  101. }
  102. }
  103. }
  104. cout<<endl;
  105. sum=0;
  106. } // END OF LOOP FOR READING
  107.  
  108.  
  109. cout<<endl<<endl;
  110.  
  111. Sort (fullname); // CALLING MY SORT FUNCTION
  112.  
  113.  
  114. // FORMATTING, " STILL WORKING ON THE LAST 16 STUDENTS"
  115.  
  116. outfile.precision(3);
  117. outfile << showpoint;
  118. outfile.width(20);
  119.  
  120. // I kept track of the number the student was before
  121. // sorting so I could then put the rigth scores, average
  122. // and grades in the right person!
  123.  
  124. outfile<<left<<fullname[0]<<" "; // FIRST STUDENT
  125. for (int s=0; s<10; s++)
  126. { outfile.precision(3);
  127. outfile<< showpoint;
  128. outfile.width(3);
  129. outfile<<right<<scores[15][s]<<" ";
  130. }
  131. outfile<< showpoint;
  132. outfile.width(1);
  133. outfile<<ave[15]<<" "<<grade[15]<<endl;
  134.  
  135. outfile.precision(3);
  136. outfile << showpoint;
  137. outfile.width(20);
  138.  
  139.  
  140. outfile<<left<<fullname[1]<<" "; // SECOND STUDENT
  141. for (int s=0; s<10; s++)
  142. { outfile.precision(3);
  143. outfile<< showpoint;
  144. outfile.width(3);
  145.  
  146. outfile<<right<<scores[5][s]<<" ";
  147. }
  148.  
  149. outfile<< showpoint;
  150. outfile.width(1);
  151.  
  152. outfile<<ave[5]<<" "<<grade[5]<<endl;
  153.  
  154.  
  155. outfile.precision(3);
  156. outfile << showpoint;
  157. outfile.width(20);
  158.  
  159.  
  160.  
  161. outfile<<left<<fullname[2]<<" "; // THIRD STUDENT
  162. for (int s=0; s<10; s++)
  163. { outfile.precision(3);
  164. outfile<< showpoint;
  165. outfile.width(3);
  166.  
  167. outfile<<right<<scores[9][s]<<" ";
  168. }
  169. outfile<< showpoint;
  170. outfile.width(1);
  171.  
  172. outfile<<ave[9]<<" "<<grade[9]<<endl;
  173.  
  174.  
  175. outfile.precision(3);
  176. outfile << showpoint;
  177. outfile.width(20);
  178.  
  179.  
  180. outfile<<left<<fullname[3]<<" "; // FOURTH STUDENT
  181. for (int s=0; s<10; s++)
  182. { outfile.precision(3);
  183. outfile<< showpoint;
  184. outfile.width(3);
  185.  
  186. outfile<<right<<scores[14][s]<<" ";
  187. }
  188. outfile<< showpoint;
  189. outfile.width(1);
  190. outfile<<ave[14]<<" "<<grade[14]<<endl;
  191.  
  192.  
  193. outfile.precision(3);
  194. outfile << showpoint;
  195. outfile.width(20);
  196.  
  197. outfile<<left<<fullname[4]<<" "; // FIFTH STUDENT
  198. for (int s=0; s<10; s++)
  199. { outfile.precision(3);
  200. outfile<< showpoint;
  201. outfile.width(3);
  202.  
  203. outfile<<right<<scores[2][s]<<" ";
  204. }
  205. outfile<<ave[2]<<" "<<grade[2]<<endl;
  206.  
  207. outfile<<left<<fullname[5]<<" "; // SIXTH STUDENT
  208. for (int s=0; s<10; s++)
  209. { outfile.precision(3);
  210. outfile<< showpoint;
  211. outfile.width(3);
  212. outfile<<scores[7][s]<<" ";
  213. }
  214. outfile<<ave[7]<<" "<<grade[7]<<endl;
  215.  
  216. outfile<<fullname[6]<<" "; // SEVENTH STUDENT
  217. for (int s=0; s<10; s++)
  218. { outfile.precision(3);
  219. outfile<< showpoint;
  220. outfile.width(3);
  221.  
  222. outfile<<scores[11][s]<<" ";
  223. }
  224. outfile<<ave[11]<<" "<<grade[11]<<endl;
  225.  
  226. outfile<<fullname[7]<<" "; // EIGTH STUDENT
  227. for (int s=0; s<10; s++)
  228. { outfile.precision(3);
  229. outfile<< showpoint;
  230. outfile.width(3);
  231.  
  232. outfile<<scores[12][s]<<" ";
  233. }
  234. outfile<<ave[12]<<" "<<grade[12]<<endl;
  235.  
  236. outfile<<fullname[8]<<" "; // NINTH STUDENT
  237. for (int s=0; s<10; s++)
  238. { outfile.precision(3);
  239. outfile<< showpoint;
  240. outfile.width(3);
  241.  
  242. outfile<<scores[10][s]<<" ";
  243. }
  244. outfile<<ave[10]<<" "<<grade[10]<<endl;
  245.  
  246. outfile<<fullname[9]<<" "; // TENTH STUDENT
  247. for (int s=0; s<10; s++)
  248. { outfile.precision(3);
  249. outfile<< showpoint;
  250. outfile.width(3);
  251.  
  252. outfile<<scores[0][s]<<" ";
  253. }
  254. outfile<<ave[0]<<" "<<grade[0]<<endl;
  255.  
  256. outfile<<fullname[10]<<" "; // ELEVENTH STUDENT
  257. for (int s=0; s<10; s++)
  258. { outfile.precision(3);
  259. outfile<< showpoint;
  260. outfile.width(3);
  261.  
  262. outfile<<scores[4][s]<<" ";
  263. }
  264. outfile<<ave[4]<<" "<<grade[4]<<endl;
  265.  
  266. outfile<<fullname[11]<<" "; // STUDENT 12
  267. for (int s=0; s<10; s++)
  268. { outfile.precision(3);
  269. outfile<< showpoint;
  270. outfile.width(3);
  271.  
  272. outfile<<scores[16][s]<<" ";
  273. }
  274. outfile<<ave[16]<<" "<<grade[16]<<endl;
  275.  
  276. outfile<<fullname[12]<<" "; // STUDENT 13
  277. for (int s=0; s<10; s++)
  278. { outfile.precision(3);
  279. outfile<< showpoint;
  280. outfile.width(3);
  281.  
  282. outfile<<scores[6][s]<<" ";
  283. }
  284. outfile<<ave[6]<<" "<<grade[6]<<endl;
  285.  
  286. outfile<<fullname[13]<<" "; // STUDENT 14
  287. for (int s=0; s<10; s++)
  288. { outfile.precision(3);
  289. outfile<< showpoint;
  290. outfile.width(3);
  291.  
  292. outfile<<scores[13][s]<<" ";
  293. }
  294. outfile<<ave[13]<<" "<<grade[13]<<endl;
  295.  
  296. outfile<<fullname[14]<<" "; // STUDENT 15
  297. for (int s=0; s<10; s++)
  298. { outfile.precision(3);
  299. outfile<< showpoint;
  300. outfile.width(3);
  301.  
  302. outfile<<scores[3][s]<<" ";
  303. }
  304. outfile<<ave[3]<<" "<<grade[3]<<endl;
  305.  
  306. outfile<<fullname[15]<<" "; // STUDENT 16
  307. for (int s=0; s<10; s++)
  308. { outfile.precision(3);
  309. outfile<< showpoint;
  310. outfile.width(3);
  311.  
  312. outfile<<scores[8][s]<<" ";
  313. }
  314. outfile<<ave[8]<<" "<<grade[8]<<endl;
  315.  
  316. outfile<<fullname[16]<<" "; // STUDENT 17
  317. for (int s=0; s<10; s++)
  318. { outfile.precision(3);
  319. outfile<< showpoint;
  320. outfile.width(3);
  321.  
  322. outfile<<scores[1][s]<<" ";
  323. }
  324. outfile<<ave[1]<<" "<<grade[1]<<endl;
  325.  
  326.  
  327.  
  328.  
  329. system("PAUSE");
  330. return EXIT_SUCCESS;
  331. }
<< moderator edit: added [code][/code] tags >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
angel22 is offline Offline
23 posts
since Jul 2005
Aug 4th, 2005
0

Re: Is it wrong to have a very long program if i did not find another way?

It's up to your professor what s/he will accept. You'll be a better judge than any of us.

Looking at your code quickly my biggest concern is I'm not sure how you are keeping track of the original indexes of each last name within your program sp you can match the correct last name with the correct original indexes holding the other data after the sorting.

Here's an example of what I might do it if I had to use multiple arrays.
C++ Syntax (Toggle Plain Text)
  1. //say my test file contains
  2. Sally Smith A
  3. Tom Jones B
  4.  
  5. //my program flow might be something like:
  6.  
  7.  
  8. //declare variables
  9. string fNames[2];
  10. string lNames[2];
  11. char grades[2];
  12. int i;
  13. ifstream fin("myFile.txt");
  14.  
  15. //step 1: read into variables
  16. for(i = 0; i < 2; ++i)
  17. {
  18. fin >> fNames[i];
  19. fin >> lNames[i];
  20. fin >> grades[i];
  21. }
  22.  
  23.  
  24. //step 2: sort arrays based on putting lNames in alphabetical order
  25. i = 0;
  26. if(lNames[i] > lNames[i + 1])
  27. {
  28. swap(lNames, i); //swap lNames
  29. swap(fNames, i); //swap fNames
  30. swap(grades, i); //swap grades
  31. //now I don't have to keep track of indexes because I swap appropriate values in each array based on the indexes in lName that need to be swapped.
  32. }
  33.  
  34. //step 3: display sorted arrays
  35. for(i = 0; i < 2; ++i)
  36. cout << lNames[i] << ", " << fNames[i] << ' ' << grades[i] << endl;
  37.  
  38.  
  39. //where the swap functions were defined as something like this:
  40. void swap(string name[], int i)
  41. {
  42. string temp;
  43. temp = name[i];
  44. name[i] = name[i + 1];
  45. name[i + 1] = temp;
  46. }
  47. //and swap() was overloaded to handle int (and double and char, if desired) arrays in addition to string arrays

To specialize these general techniques to your case you would need to:
1) expand the number of arrays used to include all the exam scores and maybe the average and grade (depending on when you calculate those results)
2) expand the sorting protocol to allow you to sort more than just a 2 student array (you've already done one example of that in your posted code)
3) swap appropriate indexes in all arrays used rather than just three arrays like I did
4) format the output as you desire (which you seem to have a handle on already)

If you know about multidimensional arrays and how to convert strings to ints and visa versa, then you might be able to do this all in a single two dimensional array of strings (and just swap rows within the array based on last name) rather than multiple individual arrays.

But, when you learn about struct/classes, come back and redo this project using them. It will be impressive how much easier it is to accomplish a given task when you have the correct tools.

Restricted as you are by your current status on the learning curve, this will be a tedious process no matter how you set it up. Good luck.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Aug 4th, 2005
0

Re: Is it wrong to have a very long program if i did not find another way?

Quote ...
Is it wrong to have a very long program if i did not find another way?
It is if you should have found another way.

Why do you have your offsets in some crazy order? (15, 5, 9, 14, ...)
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Setcolor
Next Thread in C++ Forum Timeline: Checking for duplicates in a orderedered linked list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC