fork download
  1. #include <stdio.h>
  2. int main ()
  3. {
  4. /* variable definition: */
  5. int sentinel, count, sum;
  6. double avg;
  7. /* Initialize */
  8. sentinel = 0;
  9. count = 0;
  10. sum = 0;
  11. avg = 0.0;
  12. // Loop through to input values
  13. while (sentinel != -1)
  14. {
  15. printf("Enter all positive Integers you want to average, enter -1 when complete\n");
  16. scanf("%d", &sentinel);
  17. if (sentinel != -1) {
  18. sum = sum + sentinel;
  19. count = count + 1;
  20. }
  21. }
  22. // Calculate avg. Need to type cast since two integers will yield an integer
  23. avg = (double) sum/count;
  24. printf("average is %.3f\n " , avg );
  25. return 0;
  26. }
Success #stdin #stdout 0s 2160KB
stdin
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 -1
stdout
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
Enter all positive Integers you want to average, enter -1 when complete
average is 20.000