This program is intended to read numbers into an array and to count how many positive, negative numbers there are and to also calculate the total for each category and calculate the average.
Please use these numbers as input:
-123.45
-234.56
576.1
-9.345
675.2
100
-10
1654.45
765.89
0 (sentinel value) prints output once this is entered
I hand calculated and used excel to verify that the negative total should be -377.3550 and the negative average should be -94.33875
BUT when i run my program my out puts are -377.35499477 -94.33874869
Can someone tell me what is causing the error?
Code:
#include <stdio.h>
int main (void)
{
float input[11];
int elements = 0;
double sum_positive = 0;
double sum_negative = 0;
double sum_overall = 0;
int positivecount = 0;
int negativecount = 0;
double positive_ave = 0;
double negative_ave = 0;
double overall_ave = 0;
int counter = 0;
puts("Enter a set of numbers of no more than 10 numbers:");
for(;;)
{
scanf("%f", &input[elements]);
if(input[elements] == 0)
{
break;
}
elements++;
if (elements > 10)
{
puts("Error! You have entered more than ten numbers\n");
break;
}
}
for (counter = 0; counter <= elements; counter++)
{
if(input[counter] > 0)
{
sum_positive += input[counter];
++positivecount;
}
if(input[counter] < 0)
{
sum_negative += input[counter];
++negativecount;
}
}
positive_ave = sum_positive / (double) positivecount;
negative_ave = sum_negative / (double) negativecount;
sum_overall = sum_positive + sum_negative;
overall_ave = sum_overall / elements;
if(elements <= 10)
{
puts("Input Read:");
for(counter = 0; counter <= elements; counter++)
{
if(input[counter] != 0)
{
printf("%10.4f\n",input[counter]);
}
}
puts("\nStatistics:");
puts("Desc\t\tNumber\t\t Total:\t Average:");
printf("Positive\t%6d\t\t%10.4f\t%10.4f\n", positivecount, sum_positive, positive_ave);
printf("Negative\t%6d\t\t%14.8f\t%14.8f\n", negativecount, sum_negative, negative_ave);
printf("Overall\t\t%6d\t\t%10.4f\t%10.4f\n", elements, sum_overall, overall_ave);
}
return 0;
}
THANKS IN ADVANCE