I’m trying to modify the code below to allow the user to enter any number of pairs and calculate the average. In other words, the user could enter any number of pairs as long as the weight is positive.
I’m trying to modify the code below to allow the user to enter any
number of pairs and calculate the average. In other words, the user could enter any number of pairs as long as the weight is positive. (like prompt the user for how many they want to enter or use a sentinel value to trigger when the user has completed entering values). In the line of code “account = account + 1;” should I change it to count? Also, In the line of code “scanf (“%d”, & response);” I receive an error and don’t know how to fix it.
// C Code
//This program will calculate the weighted average of N numbers
#include <stdio.h>
int main () {
/* variable definition: */
int count, answer = 1;
double avg, value, weight, sum, sumw;
/* Initialize */
count = 0;
sum = 0;
sumw = 0;
avg = 0.0;
// Loop through to input values
while (answer==1) {
do{
printf(“Enter a value and its weight:”);
//use %If for double, %f for float
scanf(“%lf %lf”, &value, &weight);
}while (weight==0);
sumw = sumw + weight;
sum = sum + value * weight;
count = count +1;
printf(“Enter 0 if you do not want to include another value, enter 1 if you want to include a new value”);
scanf (“%d”, &response);
}
// Calculate avg if sumw is not 0
avg = sum / sumw;
printf(“average is %lf\n ” , avg );
return 0;
} // end main