In C language. Hi how can I compute general weighted average using grades and units? for example, input grade = 1.25, 3.0, 1.0 input units = 3, 3, 3
then the output will be 1.25*3 +3.0*3 + 1.0 *3 then divided by 9 which is the total units.
In C language. Hi how can I compute general weighted average using grades and units? for example, input grade = 1.25, 3.0, 1.0 input units = 3, 3, 3
then the output will be 1.25*3 +3.0*3 + 1.0 *3 then divided by 9 which is the total units.
In PHP here is the solution. Since language is not mentioned:
$grade = [1.25, 3.0, 1.0];
$unit = [3, 3, 3];
$count = 0;
foreach($grade as $key => $val){
$count += $val * $unit[$key];
}
$divide = $count/array_sum($unit);
print_r($divide);
Here in C. I don't know much about C but hope it helps.
#include <stdio.h>
float grade[] = {1.25, 3.0, 1.0};
float units[] = {3, 3, 3};
float sum = 0.0;
float count = 0.0;
int i = 0;
float division = 0;
int main()
{
for(i = 0; i < sizeof(grade)/sizeof(grade[0]); ++i){
count += grade[i] * units[i];
sum += units[i];
}
division = count / sum;
printf("%f",division);
return 0;
}