diff --git a/Conversions/decimal _to_binary.c b/Conversions/decimal _to_binary.c index faa78e39..e27d18f7 100644 --- a/Conversions/decimal _to_binary.c +++ b/Conversions/decimal _to_binary.c @@ -1,25 +1,66 @@ -#include +#include +#include + +#define MAXBITS 100 + int main() { - int n,re,a[10000],j; - printf("\nenter the no "); - scanf("%d",&n); + + // input of the user + int inputNumber; + + // for the remainder + int re; + + // contains the bits 0/1 + int bits[MAXBITS]; + + // for the loops + int j; int i=0; - while(n>0) + + printf("\t\tConverter decimal --> binary\n\n"); + + // reads a decimal number from the user. + printf("\nenter a positive integer number: "); + scanf("%d",&inputNumber); + + // make sure the input number is a positive integer. + if (inputNumber < 0) + { + printf("only positive integers >= 0\n"); + return 1; + } + + // actual processing + while(inputNumber>0) { - re=n%2; - a[i]=re; - n=n/2; + // computes the remainder by modulo 2 + re = inputNumber % 2; + + // computes the quotient of division by 2 + inputNumber = inputNumber / 2; + + bits[i] = re; i++; + } - int k; - k=i-1; + printf("\n the number in binary is: "); - for(j=k;j>=0;j--) + + // iterates backwards over all bits + for(j=i-1; j>=0; j--) { - printf("%d",a[j]); + printf("%d",bits[j]); } - return(0); + + // for the case the input number is 0 + if (i == 0) + { + printf("0"); + } + + return 0; } diff --git a/Data Structures/Array/CArray.c b/Data Structures/Array/CArray.c new file mode 100644 index 00000000..07b15b15 --- /dev/null +++ b/Data Structures/Array/CArray.c @@ -0,0 +1,253 @@ +/* + * CArray.c + * + * Author: Leonardo Vencovsky + * Created on 19/03/2018 + * + * Modified by: Leonardo Vencovsky + * Last modified: 19/03/2018 + * + * Array Implementations in C + * + * Compiled in Visual Studio 2017 + * + */ + + /* + Return Codes + + -1 - Array Erased + 0 - Success + 1 - Invalid Position + 2 - Position already initialized (use update function) + 3 - Position not initialized (use insert function) + 4 - Position already empty + 5 - Array is full + + */ + +#include +#include +#include "CArray.h" + +void swap(CArray *array, int position1, int position2); + +CArray * getCArray(int size) +{ + CArray *array = (CArray *) malloc(sizeof(CArray)); + array->array = (int *) malloc(sizeof(int) * size); + array->size = size; + int i; + for (i = 0; i < size; i++) { + array->array[i] = 0; + } + return array; +} + +int insertValueCArray(CArray *array, int position, int value) +{ + if (position >= 0 && position < array->size) { + if (array->array[position] == 0) { + array->array[position] = value; + return 0; // OK + } + else return 2; // Position already initialized (use update function) + } + return 1; // Invalid position +} + +int removeValueCArray(CArray *array, int position) +{ + if (position >= 0 && position < array->size) { + if (array->array[position] != 0) { + array->array[position] = 0; + } + else return 4; // Position already empty + } + return 1; // Invalid position +} + +int pushValueCArray(CArray *array, int value) +{ + int i; + int ok = 0; + for (i = 0; i < array->size; i++) { + if (array->array[i] == 0) { + array->array[i] = value; + ok = 1; + break; + } + } + if (ok == 1) return 0; + else return 5; // Array is full +} + +int updateValueCArray(CArray *array, int position, int value) +{ + if (position >= 0 && position < array->size) { + if (array->array[position] != 0) { + + } + else return 3; // Position not initialized (use insert function) + } + return 1; // Invalid Position +} + +int eraseCArray(CArray *array) +{ + int i; + for (i = 0; i < array->size; i++) { + array->array[i] = 0; + } + return 0; +} + +int switchValuesCArray(CArray *array, int position1, int position2) +{ + if (position1 >= 0 && position1 < array->size + && position2 >= 0 && position2 < array->size) { + int temp = array->array[position1]; + array->array[position1] = array->array[position2]; + array->array[position2] = temp; + } + return 1; // Invalid Position +} + +int reverseCArray(CArray *array) +{ + int i; + for (i = 0; i < array->size / 2; i++) { + swap(array, i, array->size - i - 1); + } + return 0; +} + +int displayCArray(CArray *array) +{ + int i; + printf("\nC ARRAY\n"); + for (i = 0; i < array->size; i++) { + printf("%d ", array->array[i]); + } + printf("\n"); + return 0; +} + +int blenderCArray(CArray *array) +{ + srand(time(NULL) * array->size); + int i; + int total = array->size * 100; + for (i = 0; i < total; i++) { + swap(array, rand() % array->size, rand() % array->size); + } + return 0; +} + +CArray * getCopyCArray(CArray *arr) +{ + CArray *array = (CArray *)malloc(sizeof(CArray)); + array->array = (int *)malloc(sizeof(int) * arr->size); + array->size = arr->size; + int i; + for (i = 0; i < arr->size; i++) { + array->array[i] = arr->array[i]; + } + return array; +} + +void swap(CArray *array, int position1, int position2) +{ + int temp = array->array[position1]; + array->array[position1] = array->array[position2]; + array->array[position2] = temp; +} + +int bubbleSortCArray(CArray *array) +{ + int i, j; + for (i = 0; i < array->size - 1; i++) { + for (j = 0; j < array->size - i - 1; j++) { + if (array->array[j] > array->array[j + 1]) { + swap(array, j, j + 1); + } + } + } + return 0; +} + +int selectionSortCArray(CArray *array) +{ + int i, j, min; + for (i = 0; i < array->size - 1; i++) { + min = i; + for (j = i + 1; j < array->size; j++) + if (array->array[j] < array->array[min]) min = j; + swap(array, min, i); + } + return 0; +} + +int insertionSortCArray(CArray *array) +{ + int i, j, num; + for (i = 1; i < array->size; i++) { + num = array->array[i]; + j = i - 1; + while (j >= 0 && array->array[j] > num) + { + array->array[j + 1] = array->array[j]; + j--; + } + array->array[j + 1] = num; + } + return 0; +} + +int valueOcurranceCArray(CArray *array, int value) +{ + int i, total = 0; + for (i = 0; i < array->size; i++) { + if (array->array[i] == value) total++; + } + return total; +} + +CArray * valuePositionsCArray(CArray *array, int value) +{ + int i, j = 0; + int total = valueOcurranceCArray(array, value); + CArray *resultArray = getCArray(total); + for (i = 0; i < array->size; i++) { + if (array->array[i] == value) { + // Hopefully this won't overflow + resultArray->array[j] = i; + j++; + } + } + return resultArray; +} + +int findMinCArray(CArray *array) +{ + int i; + int min = array->array[0]; + for (i = 1; i < array->size; i++) { + if (array->array[i] < min) { + min = array->array[i]; + } + } + return min; +} + +int findMaxCArray(CArray *array) +{ + int i; + int max = array->array[0]; + for (i = 1; i < array->size; i++) { + if (array->array[i] > max) { + max = array->array[i]; + } + } + return max; +} \ No newline at end of file diff --git a/Data Structures/Array/CArray.h b/Data Structures/Array/CArray.h new file mode 100644 index 00000000..2e31997f --- /dev/null +++ b/Data Structures/Array/CArray.h @@ -0,0 +1,76 @@ +/* + * CArray.h + * + * Author: Leonardo Vencovsky + * Created on 18/03/2018 + * + * Modified by: Leonardo Vencovsky + * Last modified: 19/03/2018 + * + * Header for Array in C + * + * Compiled in Visual Studio 2017 + * + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + + typedef struct CArray { + int *array; + int size; + } CArray; + + // +-------------------------------------+ + // | Returns array | + // +-------------------------------------+ + CArray * getCArray(int size); + CArray * getCopyCArray(CArray *array); + + // +-------------------------------------+ + // | Input / Output | + // +-------------------------------------+ + int insertValueCArray(CArray *array, int position, int value); + int removeValueCArray(CArray *array, int position); + int pushValueCArray(CArray *array, int value); + int updateValueCArray(CArray *array, int position, int value); + + // +-------------------------------------+ + // | Erase | + // +-------------------------------------+ + int eraseCArray(CArray *array); + + // +-------------------------------------+ + // | Switching | + // +-------------------------------------+ + int switchValuesCArray(CArray *array, int position1, int position2); + int reverseCArray(CArray *array); + + // +-------------------------------------+ + // | Sorting | + // +-------------------------------------+ + int bubbleSortCArray(CArray *array); + int selectionSortCArray(CArray *array); + int insertionSortCArray(CArray *array); + int blenderCArray(CArray *array); + + // +-------------------------------------+ + // | Searching | + // +-------------------------------------+ + int valueOcurranceCArray(CArray *array, int value); + CArray * valuePositionsCArray(CArray *array, int value); + int findMaxCArray(CArray *array); + int findMinCArray(CArray *array); + + // +-------------------------------------+ + // | Display | + // +-------------------------------------+ + int displayCArray(CArray *array); + + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/Data Structures/Array/CArrayTests.c b/Data Structures/Array/CArrayTests.c new file mode 100644 index 00000000..3743fd31 --- /dev/null +++ b/Data Structures/Array/CArrayTests.c @@ -0,0 +1,152 @@ +/* + * CArrayTests.c + * + * Author: Leonardo Vencovsky + * Created on 19/03/2018 + * + * Modified by: Leonardo Vencovsky + * Last modified: 19/03/2018 + * + * Test Cases for Array Implementations in C + * + * Compiled in Visual Studio 2017 + * + */ + +#include +#include +#include +#include "CArray.h" + +int CArrayTests() +{ + printf("\n"); + printf(" +-------------------------------------+\n"); + printf(" | |\n"); + printf(" | C Array |\n"); + printf(" | |\n"); + printf(" +-------------------------------------+\n"); + printf("\n"); + + CArray *array = getCArray(10); + + int i; + for (i = 0; i < array->size; i++) { + insertValueCArray(array, i, i+1); + } + + displayCArray(array); + printf("\nCode: %d\n", pushValueCArray(array, 11)); // 5 + + for (i = 0; i < array->size; i++) { + removeValueCArray(array, i); + } + + displayCArray(array); + + printf("\nCode: %d", removeValueCArray(array, -1)); // 1 + printf("\nCode: %d\n", insertValueCArray(array, -1, 1)); // 1 + + // Erase + for (i = 0; i < array->size; i++) { + insertValueCArray(array, i, i + 1); + } + eraseCArray(array); + displayCArray(array); // Should give all 0s + + // Switching + CArray *arr = getCArray(13); + for (i = 0; i < arr->size; i++) { + insertValueCArray(arr, i, i + 1); + } + displayCArray(arr); + for (i = 0; i < arr->size / 2; i++) { + switchValuesCArray(arr, i, arr->size - i - 1); + } + + displayCArray(arr); + + // Or simply... + reverseCArray(arr); + + displayCArray(arr); + + // Sorting + srand(time(NULL)); + CArray *barray = getCArray(20); + for (i = 0; i < barray->size; i++) { + insertValueCArray(barray, i, rand()); + } + CArray *carray = getCopyCArray(barray); + CArray *darray = getCopyCArray(barray); + printf("\nNot sorted Array:"); + displayCArray(barray); + + printf("\nBubble Sort:"); + clock_t begin1 = clock(); + // Timing bubble sort + bubbleSortCArray(barray); + clock_t end1 = clock(); + double time_spent1 = (double)(end1 - begin1) / CLOCKS_PER_SEC; + displayCArray(barray); + + printf("\nSelection Sort:"); + clock_t begin2 = clock(); + // Timing selection sort + selectionSortCArray(carray); + clock_t end2 = clock(); + double time_spent2 = (double)(end2 - begin2) / CLOCKS_PER_SEC; + displayCArray(carray); + + printf("\nInsertion Sort:"); + clock_t begin3 = clock(); + // Timing insertion sort + insertionSortCArray(darray); + clock_t end3 = clock(); + double time_spent3 = (double)(end3 - begin3) / CLOCKS_PER_SEC; + displayCArray(carray); + + // Descending order + reverseCArray(barray); + // displayCArray(barray); + +// printf("\nBlender:"); +// blenderCArray(barray); +// displayCArray(barray); + + printf("\nTotal time spent for bubble sort: %lf seconds", time_spent1); + printf("\nTotal time spent for selection sort: %lf seconds", time_spent2); + printf("\nTotal time spent for insertion sort: %lf seconds", time_spent3); + + // Searching + CArray *aarray = getCArray(1000); + for (i = 0; i < aarray->size; i++) { + insertValueCArray(aarray, i, rand() % 100); + } + + int j = 24; + printf("\nOccurrences of the number %d in the array: %d", j, + valueOcurranceCArray(aarray, j)); + printf("\nAnd its positions:\n"); + CArray *positions = valuePositionsCArray(aarray, j); + displayCArray(positions); + // This should all give value of j + printf("\nAll %d s", j); + for (i = 0; i < positions->size; i++) { + printf("\nPosition %d has a value of %d", + positions->array[i], aarray->array[positions->array[i]]); + } + printf("\nThe list has a minimum value of %d and a maximum value of %d", + findMinCArray(aarray), findMaxCArray(aarray)); + insertionSortCArray(aarray); + // displayCArray(aarray); + + free(arr); + free(array); + free(aarray); + free(barray); + free(carray); + free(darray); + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/Data Structures/Array/README.md b/Data Structures/Array/README.md new file mode 100644 index 00000000..bb037413 --- /dev/null +++ b/Data Structures/Array/README.md @@ -0,0 +1,18 @@ +# Array + +Simple array of integers. With I/O functions, Sort Functions and Search Functions. + +## Structure + +```C +typedef struct CArray { + int *array; + int size; + } CArray; +``` + +## Files + +* CArray.c - Array Implementations +* CArray.h - Import for Usage +* CArrayTests.c - Usage Examples and tests