Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. // Important note: When reading input, something like 123abc is considered
  5. // a valid value, and will be read as 123. The non-numeric input will not be
  6. // detected immediately.
  7. // This is because of how the scanf function works, and can't be solved with
  8. // the current limitations (unless we read as a string and attempt to convert
  9. // it to a number manually, either through strtol or with our own function
  10. // (the atoi function can't be used, it's prone to the same problem)).
  11.  
  12. // Reads until newline (or end of file), discarding all it reads
  13. void skip_line(void)
  14. {
  15. int c;
  16. while ((c = getchar()) != '\n' && c != EOF)
  17. {
  18. }
  19. }
  20.  
  21. // Get the expected number of values to read
  22. int get_expected(void)
  23. {
  24. while (1)
  25. {
  26. printf("Please input the number of values to read: ");
  27. fflush(stdout); // Make sure the output is flushed and written to the console
  28.  
  29. int expected;
  30. int result = scanf("%d", &expected);
  31.  
  32. if (result == EOF)
  33. {
  34. return -1;
  35. }
  36. else if (result == 1 && expected >= 0)
  37. {
  38. // Got a valid number
  39. return expected;
  40. }
  41. else
  42. {
  43. // Either not a valid input, or a negative value
  44.  
  45. skip_line(); // Skip everything until the end of the line (or end of input)
  46.  
  47. printf("Not a valid number, it must be a positive number.\n");
  48. }
  49. }
  50. }
  51.  
  52. // Get a single value
  53. int get_value(void)
  54. {
  55. while (1)
  56. {
  57. printf("Enter a value: ");
  58. fflush(stdout); // Make sure the output is flushed and written to the console
  59.  
  60. int value;
  61. int result = scanf("%d", &value);
  62.  
  63. if (result == EOF)
  64. {
  65. return INT_MIN; // Lets hope the user doesn't input the minimum integer value
  66. }
  67. else if (result == 1)
  68. {
  69. // Got a valid value
  70. return value;
  71. }
  72. else
  73. {
  74. // Not a valid value
  75.  
  76. skip_line(); // Skip everything until the end of the line (or end of input)
  77.  
  78. printf("Not a valid value, please try again.\n");
  79. }
  80. }
  81. }
  82.  
  83. int main(void)
  84. {
  85. int expected = get_expected();
  86.  
  87. if (expected <= 0)
  88. {
  89. printf("No expected values, exiting\n");
  90. return 0;
  91. }
  92.  
  93. double sum = 0;
  94. int value = 0;
  95. int counter;
  96.  
  97. for (counter = 0; counter < expected && value != INT_MIN; ++counter)
  98. {
  99. value = get_value();
  100. if (value != INT_MIN)
  101. {
  102. sum += value;
  103. }
  104. }
  105.  
  106. double average = sum / counter;
  107. if (value == INT_MIN)
  108. {
  109. printf("Premature end of values, average = %f\n", average);
  110. }
  111. else
  112. {
  113. printf("Average = %.02f\n", average);
  114. }
  115.  
  116. return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement