This blog is under construction

Monday 8 July 2013

C program to calculate weighted arithmetic mean

Write a C program to calculate weighted arithmetic mean.


  #include <stdio.h>

  int main() {
        int subject[5][2], totWeight = 0, totwx = 0, i;
        float wmean;

        /* get the weight and marks from the user */
        printf("Enter the Mark and weight for each subject:\n");
        for (i = 0; i < 5; i++) {
                printf("Subject %d: ", i + 1);
                scanf("%d%d", &subject[i][0], &subject[i][1]);
        }

        /* weighted arithmetic mean calculation */
        for (i = 0; i < 5; i++) {
                totwx = totwx + (subject[i][0] * subject[i][1]);
                totWeight =  totWeight + subject[i][1];
        }

        wmean = (1.0 * totwx) / totWeight;

        /* print the output */
        printf("\nWeighted Arithmetic Mean: %.2f\n", wmean);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the Mark and weight for each subject:
  Subject 1: 40 5
  Subject 2: 50 2
  Subject 3: 60 4
  Subject 4: 80 3
  Subject 5: 45 1
  
  Weighted Arithmetic Mean: 55.00






See Also:

No comments:

Post a Comment