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

Page 1 of 1

## 3 Replies - 2103 Views - Last Post: 11 June 2009 - 06:11 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=109364&amp;s=66ec8688932a897b21dacc5bb7cad07d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pigmitten

• New D.I.C Head

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

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

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++) {
}

}

return 0;
}

" 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:

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";

CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);

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

CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.833, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 77.714, gradebook1.determineClassAverage(), 0.001);

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

• New D.I.C Head

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

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

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++) {
}

}

return 0;
}

" 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:

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";

CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);

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

CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.0, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 75.833, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 77.714, gradebook1.determineClassAverage(), 0.001);

CPPUNIT_ASSERT_DOUBLES_EQUAL( 79.875, gradebook1.determineClassAverage(), 0.001);
}

much thanks,
pig

### #3 BlakeJustBlake

• D.I.C Regular

Reputation: 26
• 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:

### #4 Kanvus

• D.I.C Regular

Reputation: 42
• 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.