Click here to Skip to main content
15,665,942 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
what my weighted average grade if i have grades in three subject respectively 67,54,98 with weight of respectively 32,12,45. i know the formula =(67*32)+(54*12)+(98*45)/(32+12+45) but i dont know how its work if i use for loop.
Rajesh Anuhya 26-Feb-13 0:02am    
Question Not Clear??
Sergey Alexandrovich Kryukov 26-Feb-13 0:10am    
This is not a formula, this is a numeric example. If you have not idea of elementary algebra (you don't need to know any formulas, but understanding), you have nothing to do in programming.

1 solution

Solution A,

you can use two arrays. One for the numbers one for the corresponding weights.

public double[] _number = new double[] {67,54,98};
public double[] _weights = new double[] {32,12,45};


double _wavg = 0;

for(int i = 0; i < _number.Count(); i++)
   _wavg += ((_number[i] * _weight[i]) / (_weights.Sum())


Solution B,

You can use a predefined class to store your numbers end weights

public class WeightedNumber
   public double Number {get; set; }
   public double Weight{get; set; }


List<wightednumber> _numbers;</wightednumber>

The rest looks more or less the same
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900