Reputation: 71
i inputed the first number:14,the second number:63,the third number:78,the fourth number:45,the fifth number:21 and i got 44.00 instead of 44.20.Where did i go wrong?
#include<stdio.h>
int main() {
int i,numbers[5],total=0,count=0;
float average ;
printf("\nInput the first number:");
scanf("%d", &numbers[0]);
printf("\nInput the second number:");
scanf("%d", &numbers[1]);
printf("\nInput the third number:");
scanf("%d", &numbers[2]);
printf("\nInput the fourth number:");
scanf("%d", &numbers[3]);
printf("\nInput the fifth number:");
scanf("%d", &numbers[4]);
for (i = 0; i < 5; i++) {
if (numbers[i] > 0) {
count++;
total += numbers[i];
}
}
average = total / count;
printf("\n The number of positive numbers:%d\n", count);
printf("\n The average of all positive value is %.2f\n", average);
return 0;
}
Upvotes: 0
Views: 3747
Reputation: 154055
total / count
is an integer division. The remainder of the quotient is lost before the assignment to the float
object average
.
Instead, perform floating point division.
In general, code should avoid casts. Use them sparingly. Casting can readily hide issues too.
Consider gentler means like introducing a floating point constant in the computation.
1.0 * total / count
Here the is also little need to the smallish float
versus C's more work-horse floating point type double
. Various large int
values will not save exactly in a float
.
Note that printf("%.2f\n", average);
converts average
to a double before passing it to printf()
.
Watch out for / 0
printf("\n The number of positive numbers:%d\n", count);
if (count) {
double average = 1.0 * total / count;
printf("\n The average of all positive value is %.2f\n", average);
} else {
printf("\n No positive values\n");
}
Upvotes: 0
Reputation: 84579
While there is nothing wrong with repetitive lines of code, it's certainly not literature. If you are faced with doing the same thing 5 times and the only thing changing is an index or two in the output -- you should be thinking loop. Here you can expand the loop you have to include the prompting for input as well as handling the summing of positive numbers, and it can all be done without having to store the input values in an array.
All you need to maintain within the loop is the sum
of the positive values. The after you exit the loop you can compute the average based on the number of positive values input.
Putting that altogether, you could do something similar to:
#include <stdio.h>
#define NNUM 5 /* if you need a constant, #define one (or more) */
int main (void) {
int n = 0, /* no. of positive value counter */
sum = 0, /* sum of all positive values */
val; /* each input value */
double avg; /* floating-point number to hold average */
for (int i = 0; i < NNUM; i++) { /* loop NNUM times */
printf ("enter no. %d: ", i+1); /* prompt for input */
if (scanf ("%d", &val) != 1) { /* validate EVERY user input */
fputs ("error: invalid integer input.\n", stderr);
return 1;
}
if (val > 0) { /* check if val is positive */
sum += val; /* add val to sum */
n++; /* increment pos value count */
}
}
avg = (double)sum / n; /* compute average (note cast) */
printf ("\nThe number of positive numbers : %d\n"
"The average of all positive value is : %.2f\n", n, avg);
}
Example Use/Output
$ ./bin/avgposnum
enter no. 1: 14
enter no. 2: 63
enter no. 3: 78
enter no. 4: 45
enter no. 5: 21
The number of positive numbers : 5
The average of all positive value is : 44.20
or with a few negative values:
$ ./bin/avgposnum
enter no. 1: -10
enter no. 2: 10
enter no. 3: 0
enter no. 4: 20
enter no. 5: -20
The number of positive numbers : 2
The average of all positive value is : 15.00
Look things over and let me know if you have further questions.
Upvotes: 2
Reputation:
Here's a simpler code.
#include <stdio.h>
int main()
{
int n=5,a,total=0,count=0;
float average;
while(n--){
scanf("%d",&a);
if(a>0){
printf("%d\n",a);
total+=a;
count++;
}
}
printf("%d %d\n",total,count);
average=(float)total/count;
printf("%f",average);
}
Both average and count are an integer. So their division is also an integer. Use cast operator
average=(float)total/count
or
average=total/(float)
Upvotes: 0
Reputation: 582
In your average = total / count;
, you are making integer to integer division, which will return an integer result.
Use the cast operator to temporarily use one of your variables as a float.
Like this:
average = (float) total / count;
Upvotes: 1