Hello,
So I was trying to make a program that would take 4 inputs from the user and return the sum and average of those numbers. It's just the average part of my function that's acting weird; it's taking the sum of the four inputs and multiplying it by 0.8125 instead of dividing it by the number of inputs. I once entered four 4s and got an average of 13 and a sum of 16 when it should have been an average of 4 and a sum of 16. Here's the code:
Code:
#include <stdio.h>
void avg_sum(float, float, float, float, float *, float *);
int main(void)
{
float average, sum; /* the average and sum of the inputs from the user */
float input_1, input_2, input_3, input_4; /* the four inputs from the user */
printf("Please enter the first of the floating point numbers: ");
if (scanf("%f", &input_1) != 1)
{ printf("There was a problem reading the first number\n");
return 1;
}
printf("Please enter the second of the floating point numbers: ");
if (scanf("%f", &input_2) != 1)
{
printf("There was a problem reading the second nubmer\n");
return 1;
}
printf("Please enter the third of the floating point numbers: ");
if (scanf("%f", &input_3) != 1)
{
printf("There was a problem reading the third number\n");
return 1;
}
printf("Please enter the fourth of the floating point numbers: ");
if (scanf("%f", &input_4) !=1)
{
printf("There was a problem reading the fourth number\n");
return 1;
}
avg_sum(input_2, input_2, input_3, input_4, &average, &sum);
printf("The average and sum of these four floating point numbers are %f and %f\n", average, sum);
return 0;
}
void avg_sum(float input_1, float input_2, float input_3, float input_4, float *average_ptr, float *sum_ptr)
{
*average_ptr = input_1 + input_2 + input_3 + input_4/4;
*sum_ptr = input_1 + input_2 + input_3 + input_4;
return;
}
Thanks. I'm new here to the forum and C in general.