471,076 Members | 1,256 Online

# How to calculate weighted average 2
Weihted Average

Write a C program to calculate and print the weighted average of a list of N
floating point number, using the formula

Xave = F1X1 + F2X2+...+ FnXn

where the F's are fractional weighting factors,

i.e. 0<=F1<1, and F1+F2+...+Fn=1

For example: F1=0.1 F2=0.2 F3=0.3 F4=0.4 for n=4 and the sum=1.0

(a) Assume that N=4, and that the N numbers are already available in an array.
Use as input the following array: float X={1.2,2.4,3.6,4.8};

(b) Prompt the user to enter the four weights from the keyboard. the program
should print an error messaage if the weights are out of range.

(c) For the result, the program should print the data, weights and the weighted
average.
Sep 30 '06 #1
1 19641 D_C
293 100+
Expand|Select|Wrap|Line Numbers
1. for(i = 0, i < 4, i++)
2. {
3.   do
4.   {
5.     // get floating point number input
6.   }  while // input is not within weight range
7.   F[i] = input;
8.   sum += input;
9. }
10. if(sum != 1.0)
11.   // throw a fit
12. else
13. {
14.   for(i = 0; i < 4, i++)
15.     xAve += (F[i]*X[i]);
16.   xAve /= 4;
17.   // output xAve
18. }
Sep 30 '06 #2

 reply views Thread by Rolan | last post: by 2 posts views Thread by Steve | last post: by 4 posts views Thread by gaga | last post: by 1 post views Thread by Sedigh | last post: by 3 posts views Thread by Salad | last post: by 3 posts views Thread by paeh | last post: by 7 posts views Thread by jamesnkk | last post: by reply views Thread by SuzK | last post: by reply views Thread by edmund_xue | last post: by reply views Thread by Trc0g | last post: by reply views Thread by anoble1 | last post: by reply views Thread by clicknium | last post: by reply views Thread by autodeveloper | last post: by reply views Thread by vishwasr | last post: by 6 posts views Thread by Videot7 | last post: by reply views Thread by Stoney L | last post: by reply views Thread by Twizzlers | last post: by 3 posts views Thread by OSWOM | last post: by