X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=exo1%2Fexo1.c;h=810bd9eea64545915da2dfa27ec3ff4f52384c3c;hb=HEAD;hp=34251200207f6ef3afc6bc66f9f8e307ca55918f;hpb=fdfaa04d75c620f423be04e5142daf662f787780;p=TD_C.git diff --git a/exo1/exo1.c b/exo1/exo1.c deleted file mode 100644 index 3425120..0000000 --- a/exo1/exo1.c +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include - -//FIXME: Comment the code !!! - -void promptValue(int* addr) { - scanf("%d", addr); -} - -// The efficiency of this swap alternative is debatable .. -void xorSwap (int *v1, int *v2) { - if (v1 != v2) { - *v1 ^= *v2; - *v2 ^= *v1; - *v1 ^= *v2; - } -} - -void swap(int* v1, int* v2) { - if (v1 != v2) { - int tmp = *v1; - *v1 = *v2; - *v2 = tmp; - } -} - -void displayArray(int* array, int count) { - for (int i = 0; i < count; i++) { - printf("Value in array at index[%d]= %d\n", i, array[i]); - } -} - -bool sortFirst(int* array, int length) { - bool rt = false; - // This loop could probably be replaced by a while loop with conditions - // on the array values permutation AND the iteration value, later ... - for (int i = 0; i < length-1; i++) { - if (array[i] > array[i+1]) { - swap(&array[i], &array[i+1]); - //xorSwap(&array[i], &array[i+1]); - if (!rt) { rt = true; }; - } - } - return rt; -} - -void sortArray(int* array, int length) { - bool rt; - do { - rt = sortFirst(array, length); - } while (rt); - -} - -int main() { - int tab_length = 10; - // GCC do not like variable sized array, even with the size variable properly initialized - int tab[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - for (int i = 0; i < tab_length; i++) { - printf("Enter integer value at array's index[%d]? ", i); - promptValue(&tab[i]); - } - - printf("\nView array content unsorted:\n"); - displayArray(tab, tab_length); - sortArray(tab, tab_length); - printf("\nNow, sorting the array...\n"); - printf("\nView array content sorted:\n"); - displayArray(tab, tab_length); - - return 0; -}