accumulation function for vectors. please help, this is pretty time se

I am having really silly trouble but I can't seem to fix it

Page 1 of 1

3 Replies - 1750 Views - Last Post: 11 June 2009 - 06:11 AM Rate Topic: -----

#1 pigmitten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-October 08

accumulation function for vectors. please help, this is pretty time se

Post icon  Posted 10 June 2009 - 01:22 PM

hi, so I'm doing a class average function for a vector that is storing grades.
my prof wants me to use the accumulation function to determine the average. this is what I submitted:


double GradeBook::determineClassAverage()   //self explanatory
{
	double gradeTotal = 0;  
	for ( int i = 0; i < grades.size(); i++) {  
		gradeTotal++;  
	}
	
	if (!grades.empty()){
		return accumulate(grades.begin(), grades.end(), 0)/ gradeTotal;
	}
	
	return 0;
}	



his comment was this:
" Why do you need this? You're calculating a value you already have available."

I assume he means that instead of gradeTotal, I need to use grades.size(), making:

return accumulate(grades.begin(), grades.end(), 0 ) / grades.size();


howEVER, when I do this, it rounds my averages to the nearest int instead of giving me a double. how do I fix this?
to the best of my knowledge, gradeTotal should equal grades.size() so I don't understand why I am getting two different return values.


anyway,
my test code is this:

void testClassAverage()
	{
		string s1 = "Gradebook 1";
		
		vector<int> gradeVector1;

		GradeBook gradebook1(s1, gradeVector1);
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, gradebook1.determineClassAverage(), 0.001);
		
		gradebook1.addGrade( 75 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);
				
		gradebook1.addGrade( 75);
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);
		
		gradebook1.addGrade( 75 ); 
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);		
		
		gradebook1.addGrade( 74 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 74.75, gradebook1.determineClassAverage(), 0.001);
		
		//this is where my code goes beserk. well, not beserk. but if i use grades.size() the equality assertion returns false & tells me the average is 74, not 74.75
	
		gradebook1.addGrade( 76 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);
		
		gradebook1.addGrade( 80 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.833, gradebook1.determineClassAverage(), 0.001);
		
		gradebook1.addGrade( 89 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 77.714, gradebook1.determineClassAverage(), 0.001);
		
		gradebook1.addGrade( 95 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 79.875, gradebook1.determineClassAverage(), 0.001);
	}
	






much thanks,
pig

Is This A Good Question/Topic? 0
  • +

Replies To: accumulation function for vectors. please help, this is pretty time se

#2 pigmitten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-October 08

Re: accumulation function for vectors. please help, this is pretty time se

Post icon  Posted 10 June 2009 - 01:22 PM

hi, so I'm doing a class average function for a vector that is storing grades.
my prof wants me to use the accumulation function to determine the average. this is what I submitted:


double GradeBook::determineClassAverage()   //self explanatory
{
	double gradeTotal = 0;  
	for ( int i = 0; i < grades.size(); i++) {  
		gradeTotal++;  
	}
	
	if (!grades.empty()){
		return accumulate(grades.begin(), grades.end(), 0)/ gradeTotal;
	}
	
	return 0;
}	



his comment was this:
" Why do you need this? You're calculating a value you already have available."

I assume he means that instead of gradeTotal, I need to use grades.size(), making:

return accumulate(grades.begin(), grades.end(), 0 ) / grades.size();


howEVER, when I do this, it rounds my averages to the nearest int instead of giving me a double. how do I fix this?
to the best of my knowledge, gradeTotal should equal grades.size() so I don't understand why I am getting two different return values.


anyway,
my test code is this:

void testClassAverage()
	{
		string s1 = "Gradebook 1";
		
		vector<int> gradeVector1;

		GradeBook gradebook1(s1, gradeVector1);
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, gradebook1.determineClassAverage(), 0.001);
		
		gradebook1.addGrade( 75 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);
				
		gradebook1.addGrade( 75);
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);
		
		gradebook1.addGrade( 75 ); 
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);		
		
		gradebook1.addGrade( 74 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 74.75, gradebook1.determineClassAverage(), 0.001);
		
		//this is where my code goes beserk. well, not beserk. but if i use grades.size() the equality assertion returns false & tells me the average is 74, not 74.75
	
		gradebook1.addGrade( 76 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);
		
		gradebook1.addGrade( 80 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.833, gradebook1.determineClassAverage(), 0.001);
		
		gradebook1.addGrade( 89 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 77.714, gradebook1.determineClassAverage(), 0.001);
		
		gradebook1.addGrade( 95 );
		
		CPPUNIT_ASSERT_DOUBLES_EQUAL( 79.875, gradebook1.determineClassAverage(), 0.001);
	}
	






much thanks,
pig
Was This Post Helpful? 0
  • +
  • -

#3 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: accumulation function for vectors. please help, this is pretty time se

Posted 10 June 2009 - 01:45 PM

Try typecasting it:

return accumulate(grades.begin(), grades.end(), 0 ) / (double)grades.size();


Was This Post Helpful? 0
  • +
  • -

#4 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: accumulation function for vectors. please help, this is pretty time se

Posted 11 June 2009 - 06:11 AM

Doubles behave differently in division than int.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1