I have a problem with my program and it has to do with my finding average function. The program goes like this:
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.dat" 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. 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. 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.dat"

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

So, when I try to calculate the mean of the scores the average gives me random numbers each time I open the output file. Here is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

double avgScore (int []);

int main(){

// open files
ifstream inFile;
ofstream outputFile;

string anskey;
getline( inFile, anskey );

int scores[11]; // index range is 0..10
int thisScore;
int count = 0;
string line;
while( getline( inFile, line ) && line != "" )
{
++count;
// compares answer key to each students score
thisScore =0; // initialize loop to 0
for( int i =0; i<10; ++i )
if( line[i] == anskey[i] )
++thisScore;

// updates score counter
scores[thisScore]++;

// outputs scores to "grade_report.txt" file
outputFile << "student " << count << " - " << thisScore << endl;

}
inFile.close();

outputFile << "\nThe Average Score is: "
<< avgScore( scores) << endl;

system("pause");
return 0;
}

double avgScore (int ary[] ) {
double total =0;
int numStudents =0;
for (int i =0; i < 11; i++){
numStudents += ary[i];
total+= ary[i]*i;
}
}

Can someone help me?

## All 9 Replies

Remember that each variable needs to be given a default value before you try to output it or use it in a calculation. Also remember that each element of an array is a unique variable. Therefore, each element of an array needs to be given some default value before you try to ouptut it or increment/decrement it's value. Apply this rule to your program and rerun to see if it clears up the problem.

Could you be more specific? I don't really know what you mean. In my function "numStudents" and "total" are given the start value of 0. Should the variable "ary" be given a start value or something? This is my function:

double Average (int ary[] ) {
double total =0;
int numStudents =0;
for (int i =0; i < 11; i++){
numStudents += ary[i];
total+= ary[i] * i;
}
return (total / numStudents);
}

int scores[11]; // index range is 0..10
int thisScore;

between the above two lines add the following loop:

for(int i = 0; i < 11; ++i)
cout << scores << endl;

compile and run.

So, like this:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

double Average (int []);

int main(){

// open files
//ifstream inFile;
if(!inFile) {
cout << " Error: couldn't open file";
cin.get();
return -1;
}

string anskey;
getline( inFile, anskey );

int scores[10];// index range is 0..10
for(int i = 0; i < 10; ++i)
cout << scores[i] << endl;
int thisScore;
int count = 0;
string line;
while( getline( inFile, line ) && line != "" )
{
++count;
// compares answer key to each students score
thisScore = 0; // initialize loop to 0
for( int i = 0; i<10; ++i )
if( line[i] == anskey[i] )
++thisScore;

// updates score counter
scores[thisScore]++;

// outputs scores to "grade_report.txt" file
outputFile << "student " << count << " - " << thisScore << endl;

}
inFile.close();

outputFile << "\nThe Average Score is: "
<< Average ( scores) << endl;

system("pause");
return 0;
}

double Average (int ary[] ) {
double total =0;
int numStudents =0;
for (int i =0; i < 10; ++i){
numStudents += ary[i];
total+= ary[i] * i;

}
return (total / numStudents);
}

I was playing with the different numbers for the fields(8,9,10,11) but I still get decreasing/increasing averages every time I open the output file

double Average (int ary[] , int numStudents)
{
double total = 0;

for (int i =0; i < numStudents; ++i)
{
total += ary[i];
}

return (total / numStudents);
}

h3xc0de, I tried your method but I get these errors:

too few arguments to function `double Average(int*, int)'; error: at this point in file

This is the code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

double Average (int [],int);

int main(){

// open files
//ifstream inFile;
if(!inFile) {
cout << " Error: couldn't open file";
cin.get();
return -1;
}

string anskey;
getline( inFile, anskey );

int scores[10];// index range is 0..10
for(int i = 0; i < 10; ++i)
cout << scores[i] << endl;
int thisScore;
int count = 0;
string line;
while( getline( inFile, line ) && line != "" )
{
++count;
// compares answer key to each students score
thisScore = 0; // initialize loop to 0
for( int i = 0; i<10; ++i )
if( line[i] == anskey[i] )
++thisScore;

// updates score counter
scores[thisScore]++;

// outputs scores to "grade_report.txt" file
outputFile << "student " << count << " - " << thisScore << endl;

}
inFile.close();

outputFile << "\nThe Average Score is: "
<< Average ( scores) << endl;

system("pause");
return 0;
}
// finds the average for total student scores
double Average (int ary[], int numStudents ) {
double total =0;
for (int i =0; i < numStudents; ++i){
total+= ary[i];

}
return (total / numStudents);
}

You need to pass another argument in when you are calling the Average function.

outputFile << "\nThe Average Score is: " << Average (scores, 10) << endl;

run your initial code with my two line addition. add a cin.get() to pause the program after my two lines. The point of doing this to see what the value of the individual elements of the array are at that point. Once you see what the values are you can then correct them to a more appropriate default value before you try to increment the values with the following line.

scores[thisScore]++;

When you've figured out what's wrong and you've corrected it, then remove those 2 (or 3) lines.

Thanks, for the suggestion Lerner I figured out what the problem was and thanks everyone else.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.