Suppose I have a set of weighted grades for a subject in high school. Like this: 8.4 (weight: 3), 7.2 (weight: 1), 3.4 (weight: 3).
When I loop over that set like this:
totalCount: Double
totalWeight: Integer
for grade in grades {
totalCount += grade * weight
totalWeight += weight
}
average = totalCount / totalWeight
That gives me the current average. Which is fine, but I want to calculate what grade I should score for a given weight to precisely get an average of 5.50.
That means I only want to calculate what grade I should score, as the user chooses the weight.
That means I should use this equalization: 5.50 = ((grade1 * weight1) + (grade2 * weight2) .... + (gradeToBeCalculated * givenWeight)).
I'm not entirely sure how to do this in programming code, has somebody got examples for me?
The equation above is a linear equation in many variables, as only the weights are fixed and all the grades can vary.
If you have n variables, you need to have n independent equations to get a unique solution. The equation you have will probably have infinite solutions. However, as the range for grades is probably limited, it may have a unique solution or no solution for particular values of the weights.
EDIT
The question asks for how to get an overall average of 5.50 when one grade and one weight is added.
For this:
$5.50$ $=$ $($$total count$(existing) $+$ $new grade$ $*$ $new weight$$)$ $/$ $($ $existing total weight$ $+$ $new weight$ $)$
Thus,
$new grade$ $=$ $($ $5.50$ $*$ $($ $existing total weight$ $+$ $new weight$ $)$ $-$ $total count$ $)$ $/$ $new weight$