Suppose we want to compute a weighted average and we are working in a main progr
ID: 3678618 • Letter: S
Question
Suppose we want to compute a weighted average and we are working in a main program where we have the following declarations: [9+4+4 = double values [NUM_PTS);//values to be factored into average double weights [NUM_PTS];//weights of values where the ith element//of weights is the fraction between 0 and 1//the ith element of values counts int count;//number of categories and values For example, suppose values contains 1,2, and 4 and weights contains 0.3,0.5, and 0.2. Then the weighted average would be (1)(0.3) + (2)(0.S) + (4X0.2) - 0.3 + I + 0.8 = 2.1. a. Write a method that takes in both an array of values and an array of weights, along with the logical size of each and returns the weighted average.Explanation / Answer
#include<iostream>
#include<stdlib.h>
using namespace std;
const int numpts=20;
double values[numpts];
double weights[numpts];
double average();
int main()
{
double avg;
avg=average();
cout<<" average is "<<avg;
return 0;
}
double average()
{
int count=0;
double v,w,avg=0;
cout<<" enter value and weight 007 007 to stop ";
while(1)
{
cin>>v>>w;
if(v!=007 && w!=007)
{
values[count]=v;
weights[count]=w;
count++;
}
else
{
exit(0);
}
}
cout<<values[2]<<count;
for(int i=0;i<count;i++)
{
avg=avg+(values[i]*weights[i]);
}
return avg;
}