Click here to Skip to main content
15,885,676 members
Please Sign up or sign in to vote.
1.00/5 (4 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.
Posted
Comments
Rajesh Anuhya 26-Feb-13 0:02am    
Question Not Clear??
--RA
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.
—SA

Solution A,

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

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

then: 

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

C#
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
 
you can use the scores i.e. {67, 54, 98} to put a for loop something like below

C#
var _average = 0;
for(int i = 0; i < scores.Count(); i++)
{
   _average += ((scores[i] * weights[i]))

}
_average = (_average/ (weights.Sum()) * 100;


Read out below articles to know how the formula actually works and different ways of calculating the same:
article, specified formula
 
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