fork download
  1. #include <stdio.h>
  2.  
  3. int acc(int x) {
  4. static int sum = 0;
  5. static int count = 0;
  6. int y = 0;
  7.  
  8. if (x == -1) {
  9. sum = 0;
  10. count = 0;
  11. y = 0;
  12. } else if (x == -2) {
  13. y = count;
  14. } else if (x == 0) {
  15. y = sum;
  16. } else if (x > 0) {
  17. sum += x;
  18. count++;
  19. y = sum;
  20. }
  21. return y;
  22. }
  23.  
  24. int main() {
  25. int score, num, i;
  26. printf("数字の個数を入力してください:");
  27. scanf("%d", &num);
  28. printf("%d\n", num);
  29.  
  30. for (i = 0; i < num; i++) {
  31. printf("数字を入力してください:");
  32. scanf("%d", &score);
  33. printf("%d\n", score);
  34. acc(score);
  35. }
  36.  
  37. int total = acc(0);
  38. int count = acc(-2);
  39.  
  40. printf("合計値は%dです。\n", total);
  41.  
  42. if (count > 0) {
  43. double average = (double)total / count;
  44. printf("平均値は%.1fです。\n", average);
  45. } else {
  46. printf("平均が計算できません。\n");
  47. }
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0.01s 5288KB
stdin
3 4 2 5
stdout
数字の個数を入力してください:3
数字を入力してください:4
数字を入力してください:2
数字を入力してください:5
合計値は11です。
平均値は3.7です。