Skip to content

Instantly share code, notes, and snippets.

@khardix
Created December 8, 2016 15:12
Show Gist options
  • Save khardix/291cab842796d4cfdf846b19a4c0014f to your computer and use it in GitHub Desktop.
Save khardix/291cab842796d4cfdf846b19a4c0014f to your computer and use it in GitHub Desktop.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int main(void) {
const size_t ARRAY_LEN = 256;
int data[ARRAY_LEN]; memset(data, 0, ARRAY_LEN*sizeof(int));
int input = 0, max = INT_MIN, min = INT_MAX;
int ec = 0, separator = 0;
// Never read more data than you can store!
for (size_t i = 0; i < ARRAY_LEN; ++i)
{
/* Load next input */
ec = scanf("%i", &input);
if (ec == EOF) break; // no more input
if (ec != 1) { // conversion error
fprintf(stderr, "Conversion error on input %zi!\n", i);
return EXIT_FAILURE;
}
data[i] = input;
/* Min/Max */
if (input < min) min = input;
if (input > max) max = input;
/* Check separator for EOL */
separator = getchar();
if (separator == EOF || ((char) separator) == '\n') break; // end of input
if (!isspace(separator)) { // unsupported input
fprintf(stderr, "Unsupported character after input %zi: %c\n", i, (char) separator);
return EXIT_FAILURE;
}
}
printf("Minimum: %i\nMaximum: %i\n", min, max);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment