6 Replies - 943 Views - Last Post: 06 February 2009 - 07:44 PM Rate Topic: -----

#1 JigJag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 09

Help with Student Exam program

Post icon  Posted 06 February 2009 - 01:21 PM

Hey can someone help me. I need to finish my program ASAP and am having trouble with it. The program reads in student answers from a "txt" file and outputs their score in an output file. The exam is composed of 10 questions(multiple choice= a,b,c,d). The problem I have is with my average function which prints out different averages every time I compile the program. This is my output file:
student 1 - 6
student 2 - 1
student 3 - 6
student 4 - 2
student 5 - 3
student 6 - 10
student 7 - 5
student 8 - 10

The Average Score is: -2.18754


So, as you can see that the average is not correct and every time I compile the program the average goes down(-3, -6, etc.)

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Student Exam program

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Help with Student Exam program

Posted 06 February 2009 - 02:02 PM

do you initialise the variable used to hold the sum to 0 before starting to add the marks to it? , e.g.
int main()
{
	   int sum=0;


local variables defined inside a block are not initialised unless you explicitly do so - they take whatever value is in the memory location

This post has been edited by horace: 06 February 2009 - 02:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 JigJag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 09

Re: Help with Student Exam program

Posted 06 February 2009 - 02:45 PM

Yes I do initialize the elements in the array to 0 but this is the code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

double Average (int []);

int main(){
	
	// open files 
//ifstream inFile;
ifstream inFile("grade_data.txt");
if(!inFile) {
			cout << " Error: couldn't open file";
			cin.get();
			return -1;
			}
ofstream outputFile("grade_report.txt");

// read the answer key
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[] ) {
	   double total =0;
	   int numStudents =0;
	   for (int i =0; i < 10; ++i){
			numStudents += ary[i];
		   total+= ary[i] * i;
		   
	   }
 return (total / numStudents);
}  


Was This Post Helpful? 0
  • +
  • -

#4 headways_millennia  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 03-February 09

Re: Help with Student Exam program

Posted 06 February 2009 - 03:26 PM

Hi JigJag, just looking at your Average function there appear to be a few problems.

First, you already know the number of students is 10. So there is no reason to try and determine this in the body of the for loop. i.e. the steps of your loop are identical to the number of students, and therefore you can safely remove the variable numStudents and its initialization.

Also, why this:

total+= ary[i] * i;


instead of,

total += ary[i];


When,

return (total / 10.0);


makes sense and will give you an appropriate average.

This post has been edited by headways_millennia: 06 February 2009 - 03:28 PM

Was This Post Helpful? 1
  • +
  • -

#5 JigJag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 09

Re: Help with Student Exam program

Posted 06 February 2009 - 03:52 PM

I tried your suggestion headways_millenia but my average keeps increasing/decreasing as I compile the program. Here are some examples:

Compiled once(output file grade_report contains):

student 1 - 6
student 2 - 1
student 3 - 6
student 4 - 2
student 5 - 3
student 6 - 10
student 7 - 5
student 8 - 10

The Average Score is: 3.14045e+008


Compiled program 2nd time(output file contains):

student 1 - 6
student 2 - 1
student 3 - 6
student 4 - 2
student 5 - 3
student 6 - 10
student 7 - 5
student 8 - 10

The Average Score is: 6.00368e+008


So, this is what I am getting and it goes on etc.. with different numbers for the average.
Was This Post Helpful? 0
  • +
  • -

#6 headways_millennia  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 03-February 09

Re: Help with Student Exam program

Posted 06 February 2009 - 04:46 PM

My suggestion works if you want to find the average score of a group of students. I don't know if the Average function is all that needs to change in your program.

This post has been edited by headways_millennia: 06 February 2009 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#7 JigJag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 09

Re: Help with Student Exam program

Posted 06 February 2009 - 07:44 PM

Thanks. I adjusted the program a little bit and figured it out. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1