fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int sum = 0;
  5. double ave = 0;
  6. int a[] = {3, 5, 2, 4, 1, 9, 7, 6, 0, 8};
  7. int max = a[0], x = 0;
  8.  
  9. for (int i = 0; i < 10; i++) {
  10. sum = sum + a[i];
  11. }
  12. ave = (double)sum / 10;
  13.  
  14. for (int i = 1; i < 10; i++) {
  15. if (max < a[i]) {
  16. max = a[i];
  17. x = i;
  18. }
  19. }
  20.  
  21. printf("最大値は %d\n", max);
  22. printf("最大値の場所は a[%d]\n", x);
  23. printf("平均は %.2f です\n", ave);
  24.  
  25. int secondmax = -1;
  26. int y = -1;
  27. for (int i = 0; i < 10; i++) {
  28. if (a[i] != max) {
  29. if (secondmax == -1 || a[i] > secondmax) {
  30. secondmax = a[i];
  31. y = i;
  32. }
  33. }
  34. }
  35.  
  36. printf("2番目に大きい値は %d\n", secondmax);
  37. printf("その場所は a[%d]\n", y);
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
最大値は 9
最大値の場所は a[5]
平均は 4.50 です
2番目に大きい値は 8
その場所は a[9]