0

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.

2
  • What language do you use? Commented Oct 13, 2016 at 7:16
  • C language poss Commented Oct 13, 2016 at 7:28

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hi thank you for your answer. But I need it in C language.
this is helpful. Thank you
@Steele Glad to help :)
How to do Matrix Transposition in C language?Program will ask the user for the number of rows and columns and the data for the matrix. The output will be the transpose of the matrix.
@Steele You have to store it in an array. And then using the keys you can transpose it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.