fork download
  1.  
  2. /* input a list of elements into an array,
  3.  * and then print the smallest element.
  4.  * Here we use pointers to implement the smallest function. */
  5.  
  6. #include <stdio.h>
  7. #define TABLESIZE 12
  8.  
  9. int input(int [], const int);
  10. int *smallest(int *const, const int);
  11.  
  12. int main(void)
  13. {
  14. int size, *index;
  15. int table[TABLESIZE];
  16.  
  17. if ((size = input(table, TABLESIZE)) > TABLESIZE)
  18. printf("Too many elements, max is %d\n", TABLESIZE);
  19. else {
  20. index = (int *)smallest(table, size);
  21. printf("The smallest element is %d\n", *index);
  22. }
  23. }
  24.  
  25. int input(int table[], const int limit)
  26. {
  27. int size; /* expected number */
  28. int k; /* loop control */
  29.  
  30. printf("Enter number of data items: ");
  31. scanf("%d", &size);
  32.  
  33. if (size > limit) return (size);
  34.  
  35. for (k = 0; k < size; k++)
  36. scanf("%d", &table[k]);
  37. }
  38.  
  39. /* Computing the smallest element. */
  40. int *smallest(int *const array, const int limit)
  41. {
  42. int *index, *final, *subscript;
  43. int least;
  44.  
  45. index = array;
  46. final = array + limit;
  47. least = *index;
  48. for (subscript = array + 1; subscript < final; subscript++)
  49. if (*subscript < least)
  50. least = *(index=subscript);
  51.  
  52. return (index);
  53. }
  54.  
  55.  
Success #stdin #stdout 0s 9432KB
stdin
8
stdout
Enter number of data items: The smallest element is 0