Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Question Given that a class named Rational exists and given the vector decla

ID: 3860211 • Letter: C

Question

C++ Question

Given that a class named Rational exists and given the vector declaration: vector values: And given that the Rational class has a method for adding two Rational objects: Rational Rational:: add(const Rational & b) const: As well as a [pair of] method(s) for dividing a Rational object by an integer: Rational Rational:: divide(long n) const: Rational Rational:: divide(unsigned long n) const: Show the code that would be required to calculate the average of all the elements in the vector named values.

Explanation / Answer

The code below shows how to calculate the average given the specifications of Rational class and a vector of Rational as input. It adds each of the Rational objects starting from 1st and finally divides by the size of the vector which indicates the number of rational objects present.

Rational average(vector<Rational> values)
{
Rational ans = values[0];
for(int i = 1; i < values.size(); i++)
{
ans = ans.add(values[i]);
}

ans = ans.divide(values.size());

return ans;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote