0

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?

1 Answers1

0

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$

GoodDeeds
  • 11,185
  • 3
  • 22
  • 42
  • The weight per grade can actually vary from 1..n, but it's fixed as in: it is set at a fixed value by a teacher. The grades are also fixed by the teacher.

    The thing is, I want to add a grade to the equation as mentioned above, but I'm not sure how I should calculate what that grade should be with the weight given by the user.

    – Johann Behrens Jan 25 '16 at 09:31
  • So that means I calculate the given set of grades with weights, the user inputs a weight and I just need to calculate which grade that user should score on the next test to get a 5.50 – Johann Behrens Jan 25 '16 at 09:32
  • You mean the data is added one at a time and you want to know what grade you need for a given weight in a single test to make the overall value 5.50? – GoodDeeds Jan 25 '16 at 09:37
  • Precisely, yes. I want the grade of that single test calculated for the average to be at least 5.50. – Johann Behrens Jan 25 '16 at 09:41
  • I add one grade with the given weight and I want to know what value that grade should be in order for the average to be 5.50. – Johann Behrens Jan 25 '16 at 09:43
  • And any grade above this would only increase the average for a given weight – GoodDeeds Jan 25 '16 at 09:56
  • Amazing! I checked your solution and when I have a 5,7 with weight 3 already, when I use your formula for weight 1 I should score a 4,9. When I use that in the basic formula to calculate my new average, it says 5,5.

    Thank you so much, GoodDeeds!

    – Johann Behrens Jan 25 '16 at 10:03
  • By the way, how does the formula respond when the average is lower than 5.50? Will it also correctly produce the grade I need to score for my average to become a 5.50? – Johann Behrens Jan 25 '16 at 10:07
  • Yes it will. However, if it is impossible to score that high a grade (whatever you want), you will get it greater than the maximum value and if it is impossible to score that low a grade, you will get a negative answer. – GoodDeeds Jan 25 '16 at 10:08
  • Alright :) Then I can backup that I need the lowest value for a given grade and calculate again with the result from the first calculation, so being recursive.

    Thanks so much :D

    – Johann Behrens Jan 25 '16 at 10:11