Updating an array

Hello, everyone. I'm wondering if anyone could help me with a program I'm working on. Here's the 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. You will also keep track of how many students earned each score (0-10) and print a report after the grading.

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

My problem is with the bold part. I know that I need to store each grade into a separate array, then print it out when that's done, but I can't seem to get the storing function to work.

Here's my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int gradeExam(string key, string exam);
int updateScores(int grade, int index);

int main()
{
   string key;
   string exam;

   int grade;
   int index = 0;

   ifstream infile;
   infile.open ("grade_data.txt");

   ofstream outfile;
   outfile.open ("grade_report.txt");
   
   infile >> key >> exam;

   while (infile >> exam)
   {
      ++index;
	  grade = gradeExam(key, exam);
	  updateScores(grade, index);

      outfile << "Student" << index << " - " << grade << endl;
	 	  
   }

   return 0;
}

int gradeExam(string key, string exam)
{
   int grade;
   grade = 0;

   for (int i = 0; i < 10; i++)
   {
      if (key[i] == exam[i])
         grade = grade + 1;
	  else
	     grade += 0;
	}


   return grade;
}

int updateScores(int grade, int index)
{
   int i;
   int scores[11];

   for (i = 0; i <= 11; i++)
      scores[i] = 0;

   for (i = 0; i <= 11; i++)
   {
      if (grade == scores[i])
         scores[i] += 1;
      else
    	 scores[i] += 0;

      cout << scores[i] << endl;
   }


   return 0;
}


Input file contains:
bccbbadbca
bccbbadbca
bccdbadbca
accdbadbca
bccbbadbcd
bccbbadbad
dddddddbca
No one can figure this out either? ._.
For starters, you are passing 'index' into updateScores(), but that parameter is never accessed in the function.

More importantly, the scores array is re-declared and re-initialized each time you call updateScores().
your update scores function is completely useless just delete it. You don't need to store the individual grades. Just the number of students who got the grade. All you need to do is declare an array that can hold 10 integers and initialize it to zero eg. int scores[11] = {0}; then after you calculate the grade you increment the proper element of the array eg scores[grade]++; so if grade is 10 then the tenth element of the array will now be 1.

infile >> key >> exam; should be infile >> key;

lines 48 and 49 should be deleted they don't do anything.

line 44 should be more like for (int i = 0; i < key.size(); i++)

you should also be checking to make sure you file opened properly, and you need to call close on the file.
Last edited on
Topic archived. No new replies allowed.