| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- /*
- * Do not modify this file.
- */
- #include "pa03.h"
- #include <libgen.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define TRUE 1
- #define FALSE 0
- void printUsage(char * exec_path)
- {
- const char * exec = basename(exec_path);
- printf("\n"
- " Usage: %s <inputfile>\n"
- "\n"
- " Reads file <inputfile> (of integers), sorts them and prints them.\n"
- " Then, returns the ordinal position of the first number (unsorted) in the sorted list,\n"
- " and the ordinal position of the number 1 greater than that (if it exists).\n"
- "\n",
- exec);
- }
- const char * oridinalSuffix(int ind)
- {
- if(ind % 10 == 1 && ind != 11)
- return "st";
- if(ind % 10 == 2 && ind != 12)
- return "nd";
- return "th";
- }
- void printSearchResult(int key, int ind, int okay)
- {
- printf("Integer '%d' ", key);
- if(ind >= 0)
- printf("in the %d%s position in the sorted array", ind, oridinalSuffix(ind));
- else
- printf("not found");
- if(okay)
- printf(" -- pass.\n");
- else
- printf(" -- FAIL.\n");
- }
- int isSortedAscending(int * arr, int len)
- {
- int i;
- for(i = 1; i < len; ++i)
- if(arr[i] < arr[i-1])
- return FALSE;
- return TRUE;
- }
- int main(int argc, char *argv[])
- {
- if (argc != 2) {
- printUsage(argv[0]);
- return EXIT_FAILURE;
- }
- // Parse command-line arguments
- if (argc == 2 && (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0))
- {
- printUsage(argv[0]);
- return EXIT_SUCCESS;
- }
-
- // Read and sort integers, then find 'ind'
- const char * filename = argv[1];
- int length = 0;
- int * integers = readIntegers(filename, &length);
- if(integers == NULL || length <= 0)
- {
- fprintf(stderr, "Failed to read integer-file '%s', aborting\n", filename);
- return EXIT_FAILURE;
- }
- // This is what we will search for
- int key = integers[0];
- int key2 = key + 1;
- // -- Print the unsorted array
- printf("Unsorted array:");
- int i;
- for(i = 0; i < length; ++i)
- {
- printf(" % 3d", integers[i]);
- }
- printf("\n");
-
- // -- Sort (and search)
- sort(integers, length);
- int ind = search(integers, length, key);
- int ind2 = search(integers, length, key2);
- // -- Use a linear search to find the correct ind2
- int ind2_res = -1;
- for(i = 0; i < length && ind2_res == -1; ++i)
- if(integers[i] == key2)
- ind2_res = i;
- // -- Did we do everything correctly?
- int is_sorted = isSortedAscending(integers, length);
- int key1_okay = ind >= 0 && ind < length && integers[ind] == key;
- int key2_okay = ind2 == ind2_res;
- // -- Print results
- printf("Sorted array: ");
- for(i = 0; i < length; ++i)
- {
- printf(" % 3d", integers[i]);
- }
- if(is_sorted)
- printf(" -- correct.\n");
- else
- printf(" -- FAIL (not sorted).\n");
- printSearchResult(key, ind, key1_okay);
- printSearchResult(key2, ind2, key2_okay);
- // -- Clean up
- free(integers);
- if(is_sorted && key1_okay && key2_okay)
- return EXIT_SUCCESS;
- return EXIT_FAILURE;
- }
|