pa03.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Do not modify this file.
  3. */
  4. #include "pa03.h"
  5. #include <libgen.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define TRUE 1
  10. #define FALSE 0
  11. void printUsage(char * exec_path)
  12. {
  13. const char * exec = basename(exec_path);
  14. printf("\n"
  15. " Usage: %s <inputfile>\n"
  16. "\n"
  17. " Reads file <inputfile> (of integers), sorts them and prints them.\n"
  18. " Then, returns the ordinal position of the first number (unsorted) in the sorted list,\n"
  19. " and the ordinal position of the number 1 greater than that (if it exists).\n"
  20. "\n",
  21. exec);
  22. }
  23. const char * oridinalSuffix(int ind)
  24. {
  25. if(ind % 10 == 1 && ind != 11)
  26. return "st";
  27. if(ind % 10 == 2 && ind != 12)
  28. return "nd";
  29. return "th";
  30. }
  31. void printSearchResult(int key, int ind, int okay)
  32. {
  33. printf("Integer '%d' ", key);
  34. if(ind >= 0)
  35. printf("in the %d%s position in the sorted array", ind, oridinalSuffix(ind));
  36. else
  37. printf("not found");
  38. if(okay)
  39. printf(" -- pass.\n");
  40. else
  41. printf(" -- FAIL.\n");
  42. }
  43. int isSortedAscending(int * arr, int len)
  44. {
  45. int i;
  46. for(i = 1; i < len; ++i)
  47. if(arr[i] < arr[i-1])
  48. return FALSE;
  49. return TRUE;
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. if (argc != 2) {
  54. printUsage(argv[0]);
  55. return EXIT_FAILURE;
  56. }
  57. // Parse command-line arguments
  58. if (argc == 2 && (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0))
  59. {
  60. printUsage(argv[0]);
  61. return EXIT_SUCCESS;
  62. }
  63. // Read and sort integers, then find 'ind'
  64. const char * filename = argv[1];
  65. int length = 0;
  66. int * integers = readIntegers(filename, &length);
  67. if(integers == NULL || length <= 0)
  68. {
  69. fprintf(stderr, "Failed to read integer-file '%s', aborting\n", filename);
  70. return EXIT_FAILURE;
  71. }
  72. // This is what we will search for
  73. int key = integers[0];
  74. int key2 = key + 1;
  75. // -- Print the unsorted array
  76. printf("Unsorted array:");
  77. int i;
  78. for(i = 0; i < length; ++i)
  79. {
  80. printf(" % 3d", integers[i]);
  81. }
  82. printf("\n");
  83. // -- Sort (and search)
  84. sort(integers, length);
  85. int ind = search(integers, length, key);
  86. int ind2 = search(integers, length, key2);
  87. // -- Use a linear search to find the correct ind2
  88. int ind2_res = -1;
  89. for(i = 0; i < length && ind2_res == -1; ++i)
  90. if(integers[i] == key2)
  91. ind2_res = i;
  92. // -- Did we do everything correctly?
  93. int is_sorted = isSortedAscending(integers, length);
  94. int key1_okay = ind >= 0 && ind < length && integers[ind] == key;
  95. int key2_okay = ind2 == ind2_res;
  96. // -- Print results
  97. printf("Sorted array: ");
  98. for(i = 0; i < length; ++i)
  99. {
  100. printf(" % 3d", integers[i]);
  101. }
  102. if(is_sorted)
  103. printf(" -- correct.\n");
  104. else
  105. printf(" -- FAIL (not sorted).\n");
  106. printSearchResult(key, ind, key1_okay);
  107. printSearchResult(key2, ind2, key2_okay);
  108. // -- Clean up
  109. free(integers);
  110. if(is_sorted && key1_okay && key2_okay)
  111. return EXIT_SUCCESS;
  112. return EXIT_FAILURE;
  113. }