943,015 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 297
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 2nd, 2010
0

Grading exams project

Expand Post »
this is the exact instructions:

"The program will grade a series of exams and then print a grade report for students in a course.

Input: An instructor has a class of students each of whom takes a multiple-choice exam with 10 questions. For each student in the class, there is one line in the input file. The line contains the answers that student gave for the exam. The input file named "grade_data.txt" will have the following format:

line 1: the key for the exam (e.g.)

bccbbadbca

lines 2-n:

a set of answers. You know you are done when you get to a line with no data.
Note: You will not know in advance how many exams you have to grade and you don't need to store the exam answers in your program.

Processing: The program is to read the input file and grade each exam and print out the score for that exam.

Output: Here is an example of how the output might appear. You will write the report to an output file named "grade_report.txt"

student 1 - 8
student 2 - 10
student 3 - 1
etc.

Final Report
------------

10 - 4
9 - 2
8 - 3
.
.
1 - 3
0 - 0

high score - 10

low score - 1

mean score - 6.25"


ive done a lot of it so far but i cant figure out how to do a few things. I cant figure out how to list how many people got question 1 right, question 2 right, etc. and im having trouble finding the highest/lowest/ and mean scores.

i didnt use any arrays on this project, and im not sure if it would be better to use an array for this kind of thing or if i can still make it work without any arrays.

this is the code i have so far:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. ifstream infile("grade_data.txt");
  7. string grades, key;
  8. string t1;
  9. int temp;
  10.  
  11. int main()
  12. {
  13. if (!infile)
  14. {
  15. cout << "Error opening file\n\n";
  16. system("pause");
  17. }
  18. else
  19. cout << "File Successfully Opened\n\n";
  20.  
  21.  
  22. infile >> key;
  23.  
  24. string a1 = key.substr(0,1);
  25. string a2 = key.substr(1,1);
  26. string a3 = key.substr(2,1);
  27. string a4 = key.substr(3,1);
  28. string a5 = key.substr(4,1);
  29. string a6 = key.substr(5,1);
  30. string a7 = key.substr(6,1);
  31. string a8 = key.substr(7,1);
  32. string a9 = key.substr(8,1);
  33. string a10 = key.substr(9,1);
  34.  
  35.  
  36. cout<<endl;
  37.  
  38. infile >> grades;
  39. while (!infile.eof())
  40. {
  41. temp++;
  42.  
  43.  
  44. string b1 = grades.substr(0,1);
  45. string b2 = grades.substr(1,1);
  46. string b3 = grades.substr(2,1);
  47. string b4 = grades.substr(3,1);
  48. string b5 = grades.substr(4,1);
  49. string b6 = grades.substr(5,1);
  50. string b7 = grades.substr(6,1);
  51. string b8 = grades.substr(7,1);
  52. string b9 = grades.substr(8,1);
  53. string b10 = grades.substr(9,1);
  54.  
  55. int results=0;
  56.  
  57. if (a1==b1)
  58. results++;
  59. if (a2==b2)
  60. results++;
  61. if (a3==b3)
  62. results++;
  63. if (a4==b4)
  64. results++;
  65. if (a5==b5)
  66. results++;
  67. if (a6==b6)
  68. results++;
  69. if (a7==b7)
  70. results++;
  71. if (a8==b8)
  72. results++;
  73. if (a9==b9)
  74. results++;
  75. if (a10==b10)
  76. results++;
  77.  
  78.  
  79. int g;
  80. g=results*10;
  81. cout<<"Student "<<temp<<": Made: "<<results<<" Missed: "<<10-results<<" Grade: "<<g<<"%\n";
  82. infile >> grades;
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89. cout<<endl;
  90. system("pause");
  91. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jelinky is offline Offline
18 posts
since Apr 2010
Sep 3rd, 2010
0
Re: Grading exams project
Too many string here. A keys string and a grades string are sufficient. No need for all the substrings. Just do this:

C++ Syntax (Toggle Plain Text)
  1. if(key[i] == grades[i]
  2. {
  3. // right answer
  4. }
  5. else
  6. {
  7. // wrong answer.
  8. }

You'll need at least an array of integers to keep track of how many people scored 4 points, 5 points, 6 points, etc. I don't see a need for any other arrays. Your two strings are arrays anyway.

So something like this for variables:

C++ Syntax (Toggle Plain Text)
  1. const int NUM_QUESTIONS = 10;
  2. string key;
  3. string grades;
  4. int scores[NUM_QUESTIONS + 1];
  5. int results;

Get rid of a1 through a10 and b1 through b10 and all substr statements. They're unnecessary. Rename temp to something more descriptive and make sure to initialize it, or get rid of it.

You should also use a loop instead of all the repeated statements:

C++ Syntax (Toggle Plain Text)
  1. for(int i = 0; i < NUM_QUESTIONS; i++)
  2. {
  3. if(key[i] == grades[i]
  4. {
  5. // right answer
  6. }
  7. else
  8. {
  9. // wrong answer.
  10. }
  11. }

[EDIT]
Perhaps rename the "grades" variable to "answers" to be more descriptive?
Last edited by VernonDozier; Sep 3rd, 2010 at 12:09 am.
Featured Poster
Reputation Points: 2606
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,364 posts
since Jan 2008
Sep 3rd, 2010
0
Re: Grading exams project
ok, heres my revised version. its working but its not finished.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. ifstream infile("grade_data.txt");
  7. string answers, key;
  8. string t1;
  9. int num_grades;
  10.  
  11. const int NUM_QUESTIONS = 10;
  12. int scores[NUM_QUESTIONS + 1];
  13. int results=0;
  14. int wrong=0;
  15.  
  16.  
  17. int main()
  18. {
  19. if (!infile)
  20. {
  21. cout << "Error opening file\n\n";
  22. system("pause");
  23. }
  24. else
  25. cout << "File Successfully Opened\n\n";
  26.  
  27.  
  28. infile >> key;
  29.  
  30. cout<<" KEY\n";
  31. for (int i=0; i < key.length(); i++)
  32. cout<<" | "<<key[i]<<" | \n";
  33.  
  34.  
  35. cout<<endl;
  36.  
  37. infile >> answers;
  38. while (!infile.eof())
  39. {
  40. num_grades++;
  41.  
  42. for (int a=0; a < answers.length(); a++)
  43. cout<<" | "<<answers[a]<<" | \n";
  44.  
  45.  
  46. for(int b = 0; b < NUM_QUESTIONS; b++)
  47. {
  48. if(key[b] == answers[b])
  49. {
  50. results++;
  51. }
  52. else
  53. {
  54. wrong++;
  55. }
  56.  
  57. //cout << scores[NUM_QUESTIONS + 1] << "\n"; for visual purposes
  58. }
  59.  
  60.  
  61. int percent;
  62. percent=results*10;
  63. cout<<"Student "<<num_grades<<": Made: "<<results<<" Missed: "<<wrong<<" Grade: "<<percent<<"%\n";
  64. infile >> answers;
  65.  
  66. results=0;
  67. wrong=0;
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74. cout<<endl;
  75. system("pause");
  76. }

i still need to make a table showing how many people go question 1-10 right. and i need to sort the grades in highest/lowest and average.

im going to continue working on it now, but help is appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jelinky is offline Offline
18 posts
since Apr 2010
Sep 3rd, 2010
-1
Re: Grading exams project
Have another look at this part of VernonDozier's post
Quote originally posted by VernonDozier ...
C++ Syntax (Toggle Plain Text)
  1. const int NUM_QUESTIONS = 10;
  2. string key;
  3. string grades;
  4. int scores[NUM_QUESTIONS + 1];
  5. int results;
You'll want something similar to Line 4. You will then have to create some code to manipulate the array. Think of each element of the array as a counter. If someone scores a 5, you increment element 5; score of 8, increment 8; and so on.
Last edited by Fbody; Sep 3rd, 2010 at 3:06 pm.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Sep 3rd, 2010
0
Re: Grading exams project
can you explain a little more about line 4?...

ive tried using a numbered subscript for scores but i cant get it working.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jelinky is offline Offline
18 posts
since Apr 2010
Sep 3rd, 2010
0
Re: Grading exams project
You don't HAVE to use an array to hold all the scores, as arrays are just groups of individual variables of like type stored in contiguous memory. However, it is certainly neater to use arrays. The grades are arranged by how many answers were done correctly out of the 10 questions. Therefore the range of the grades could be anywhere from zero to 10, or 11 possible grades. Since the grades are ints they could act as indexes to the related element in the array. So array[5] would be number of people who got 5 answers correct and array[7] would be how many people got 7 answers correct, etc. If you initialize all the elements in the grade array to zero, then you can increment the appropriate element by one each time you get done grading a single students answers.

When you are done reading the file you can do the statistical analysis on the grade array. The highest and lowest grades are the index values of the highest and lowest non-zero elements of the grade array. The total number of test is the sum total of all the elements in the array. The average, well, you should be able to figure out the rest of it by yourself.

The table of student scores should be written to file when that students answers have been graded since you can't pull an individual students results out of the array.
Last edited by Lerner; Sep 3rd, 2010 at 6:20 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Sep 3rd, 2010
0
Re: Grading exams project
your talking about using scores[i] right? i cant get it working, its always 0
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jelinky is offline Offline
18 posts
since Apr 2010
Sep 3rd, 2010
0
Re: Grading exams project
Yeah, you use scores as the array to hold the grades. But, I don't see where you initialize the elements of scores to zero in the code posted.

In the code posted you could use the variable called results as the index to the element of scores you want to increment after grading the student answers.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Sep 3rd, 2010
0
Re: Grading exams project
From your earlier post:

Suppose you have 6 students. They have the following scores:

5, 7, 4, 5, 8, 5

By the time you've scored all the tests, your scores[] array should contain these values.

C++ Syntax (Toggle Plain Text)
  1. scores[0] = 0
  2. scores[1] = 0
  3. scores[2] = 0
  4. scores[3] = 0
  5. scores[4] = 1
  6. scores[5] = 3
  7. scores[6] = 0
  8. scores[7] = 1
  9. scores[8] = 1
  10. scores[9] = 0
  11. scores[10] = 0


Note that if you add the scores[] array, the sum is 6, which is exactly the number of students, and which is the exact number of times that you go through the big while loop.

That means that you must:
  1. Initialize all elements of scores[] to 0 BEFORE the while loop.
  2. Increment(i.e. add 1) to one of the elements of scores[] each time you go through the while loop.


Now, which element of scores[] should you increment? Well, say a student scored 4. What does scores[4] represent? The number students who scored exactly 4.

So there would be a line in there like this:

C++ Syntax (Toggle Plain Text)
  1. scores[4]++;

But actually not bcause the 4 is not hard-coded but instead is a variable. So this:

C++ Syntax (Toggle Plain Text)
  1. scores[someVariableName]++;

Now go through your code below and decide what the real variable is and replace someVariableName with the real variable and stick it at the bottom of the while loop.

And again, initialize everything to 0 BEFORE the while loop. Here's your code, formatted.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. ifstream infile("grade_data.txt");
  7. string answers, key;
  8. string t1;
  9. int num_grades;
  10.  
  11. const int NUM_QUESTIONS = 10;
  12. int scores[NUM_QUESTIONS + 1];
  13. int results=0;
  14. int wrong=0;
  15.  
  16.  
  17. int main()
  18. {
  19. if (!infile)
  20. {
  21. cout << "Error opening file\n\n";
  22. system("pause");
  23. }
  24. else
  25. cout << "File Successfully Opened\n\n";
  26.  
  27.  
  28. infile >> key;
  29.  
  30. cout<<" KEY\n";
  31. for (int i=0; i < key.length(); i++)
  32. cout<<" | "<<key[i]<<" | \n";
  33.  
  34.  
  35. cout<<endl;
  36.  
  37. infile >> answers;
  38. while (!infile.eof())
  39. {
  40. num_grades++;
  41.  
  42. for (int a=0; a < answers.length(); a++)
  43. cout<<" | "<<answers[a]<<" | \n";
  44.  
  45.  
  46. for(int b = 0; b < NUM_QUESTIONS; b++)
  47. {
  48. if(key[b] == answers[b])
  49. {
  50. results++;
  51. }
  52. else
  53. {
  54. wrong++;
  55. }
  56.  
  57. //cout << scores[NUM_QUESTIONS + 1] << "\n"; for visual purposes
  58. }
  59.  
  60.  
  61. int percent;
  62. percent=results*10;
  63. cout<<"Student "<<num_grades<<": Made: "<<results<<" Missed: "<<wrong<<" Grade: "<<percent<<"%\n";
  64. infile >> answers;
  65.  
  66. results=0;
  67. wrong=0;
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74. cout<<endl;
  75. system("pause");
  76. }
Last edited by VernonDozier; Sep 3rd, 2010 at 8:37 pm.
Featured Poster
Reputation Points: 2606
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,364 posts
since Jan 2008
Sep 4th, 2010
0
Re: Grading exams project
C++ Syntax (Toggle Plain Text)
  1. scores[0] = 0
  2. scores[1] = 0
  3. scores[2] = 0
  4. scores[3] = 0
  5. scores[4] = 1
  6. scores[5] = 3
  7. scores[6] = 0
  8. scores[7] = 1
  9. scores[8] = 1
  10. scores[9] = 0
  11. scores[10] = 0

@VernonDozier thats what im trying to get but i dont see where i can enter that to make it work.

this is the code i have now:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. ifstream infile("grade_data.txt");
  8. string answers, key;
  9. int num_grades;
  10.  
  11. const int NUM_QUESTIONS = 10;
  12. double scores[NUM_QUESTIONS + 1];
  13. double results=0.00;
  14. int wrong=0;
  15. double total=0.00;
  16. double average=0.00;
  17. double percent=0.00;
  18. double temp=0.00;
  19. int b;
  20.  
  21. int main()
  22. {
  23. if (!infile)
  24. {
  25. cout << "Error opening file\n\n";
  26. system("pause");
  27. }
  28. else
  29. cout << "File Successfully Opened\n\n";
  30.  
  31.  
  32. infile >> key;
  33.  
  34. cout<<" KEY\n";
  35. for (int i=0; i < key.length(); i++)
  36. cout<<" | "<<key[i]<<" | \n";
  37.  
  38.  
  39. cout<<endl;
  40.  
  41. infile >> answers;
  42. while (!infile.eof())
  43. {
  44. num_grades++;
  45.  
  46. for (int a=0; a < answers.length(); a++)
  47. cout<<" | "<<answers[a]<<" | \n";
  48.  
  49.  
  50. for(b = 0; b < NUM_QUESTIONS; b++)
  51. {
  52.  
  53. if(key[b] == answers[b])
  54. {
  55. results++;
  56. cout<<"CORRECT ANSWERS: "<<fixed<<setprecision(0)<<b+1<<"\n"; //which questions they are getting right.
  57. }
  58. else
  59. wrong++;
  60.  
  61. //cout << scores[NUM_QUESTIONS + 1] << "\n";
  62.  
  63.  
  64. }
  65.  
  66. percent=results*10;
  67. cout<<"Student "<<num_grades<<": Made: "<<fixed<<setprecision(0)<<results<<" Missed: "<<wrong<<" Grade: "<<percent<<"%\n";
  68.  
  69. for (int c=0; c<1;c++)
  70. total+=results;
  71. average=(total/num_grades);
  72.  
  73. infile >> answers;
  74.  
  75. results=0;
  76. wrong=0;
  77.  
  78. }
  79.  
  80. cout<<"\n\nThe average of "<<num_grades<<" tests is: "<<fixed<<setprecision(2)<<average<<endl;
  81.  
  82. cout<<"\n\n";
  83. system("pause");
  84. }

thanks for being patient and helping me, i dont normally have this much trouble :/
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jelinky is offline Offline
18 posts
since Apr 2010

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: datatype info over socket; dynamic initialize?
Next Thread in C++ Forum Timeline: C++ Doubts???





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


Follow us on Twitter


© 2011 DaniWeb® LLC