diff --git a/misc/Fibonacci_DP.c b/misc/Fibonacci_DP.c index 52d99db1..c3c55782 100644 --- a/misc/Fibonacci_DP.c +++ b/misc/Fibonacci_DP.c @@ -1,49 +1,53 @@ -//Fibonacci Series using Dynamic Programming +//Fibonacci Series using Dynamic Programming /* Author: Moinak Banerjee(moinak878) Date : 1 October ,2019 */ -#include -#include - -int fib(int n) -{ - //Out of Range checking - if(n<0){ +#include +#include + +int fib(int n) +{ + //Out of Range checking + if (n < 0) + { printf("\nNo Such term !\n"); exit(0); } - //declaring array to store fibonacci numbers -- memoization - int f[n+2]; // one extra to handle edge case, n = 0 - int i; - - /* let 0th and 1st number of the series be 0 and 1*/ - f[0] = 0; - f[1] = 1; - - for (i = 2; i <= n; i++) - { - // Adding the previous 2 terms to make the 3rd term - f[i] = f[i-1] + f[i-2]; - } - - return f[n]; -} - + //declaring array to store fibonacci numbers -- memoization + int *f = (int *)malloc((n + 2) * sizeof(int)); // one extra to handle edge case, n = 0 + int i; + + /* let 0th and 1st number of the series be 0 and 1*/ + f[0] = 0; + f[1] = 1; + + for (i = 2; i <= n; i++) + { + // Adding the previous 2 terms to make the 3rd term + f[i] = f[i - 1] + f[i - 2]; + } + + int out = f[n]; + free(f); + return out; +} + int main(int argc, char *argv[]) { - int number; + int number; - //Asks for the number/position of term in Fibonnacci sequence + //Asks for the number/position of term in Fibonnacci sequence if (argc == 2) number = atoi(argv[1]); - else { + else + { printf("Enter the value of n(n starts from 0 ): "); scanf("%d", &number); } - - printf("The nth term is : %d \n", fib(number)); - - return 0; + + printf("The nth term is : %d \n", fib(number)); + + return 0; } \ No newline at end of file diff --git a/numerical_methods/MEAN.C b/numerical_methods/MEAN.C index 160c8b75..718abca0 100644 --- a/numerical_methods/MEAN.C +++ b/numerical_methods/MEAN.C @@ -7,7 +7,7 @@ int main(int argc, char **argv) { - int a[MAX_LEN], n = 10, i, j, temp, sum = 0; + int *a, n = 10, i, j, temp, sum = 0; float mean; if (argc == 2) @@ -18,6 +18,7 @@ int main(int argc, char **argv) fprintf(stderr, "Maximum %d!\n", MAX_LEN); return 1; } + a = (int *)malloc(n * sizeof(int)); } printf("Random Numbers Generated are : "); @@ -35,5 +36,6 @@ int main(int argc, char **argv) printf("\nMean :"); printf("%f", mean); + free(a); return 0; } diff --git a/searching/Linear_Search.c b/searching/Linear_Search.c index 5c85d697..3ee72471 100644 --- a/searching/Linear_Search.c +++ b/searching/Linear_Search.c @@ -1,4 +1,5 @@ #include +#include int linearsearch(int *arr, int size, int val) { @@ -17,7 +18,7 @@ int main() printf("Enter the size of the array:\n"); scanf("%d", &n); //Taking input for the size of Array - int a[n]; + int *a = (int *)malloc(n * sizeof(int)); printf("Enter the contents for an array of size %d:\n", n); for (i = 0; i < n; i++) scanf("%d", &a[i]); // accepts the values of array elements until the loop terminates// @@ -28,5 +29,7 @@ int main() printf("Value %d is in the array.\n", v); else printf("Value %d is not in the array.\n", v); + + free(a); return 0; } diff --git a/sorting/Bead_Sort.c b/sorting/Bead_Sort.c index 37e5e0b7..1c244f87 100644 --- a/sorting/Bead_Sort.c +++ b/sorting/Bead_Sort.c @@ -3,15 +3,16 @@ #include /*Displays the array, passed to this method*/ -void display(int arr[], int n){ - - int i; - for(i = 0; i < n; i++){ - printf("%d ", arr[i]); - } - - printf("\n"); - +void display(int *arr, int n) +{ + + int i; + for (i = 0; i < n; i++) + { + printf("%d ", arr[i]); + } + + printf("\n"); } /*This is where the sorting of the array takes place @@ -22,54 +23,63 @@ void bead_sort(int *a, int len) { int i, j, max, sum; unsigned char *beads; -# define BEAD(i, j) beads[i * max + j] - +#define BEAD(i, j) beads[i * max + j] + for (i = 1, max = a[0]; i < len; i++) - if (a[i] > max) max = a[i]; - + if (a[i] > max) + max = a[i]; + beads = calloc(1, max * len); - + /* mark the beads */ for (i = 0; i < len; i++) for (j = 0; j < a[i]; j++) BEAD(i, j) = 1; - - for (j = 0; j < max; j++) { + + for (j = 0; j < max; j++) + { /* count how many beads are on each post */ - for (sum = i = 0; i < len; i++) { + for (sum = i = 0; i < len; i++) + { sum += BEAD(i, j); BEAD(i, j) = 0; } /* mark bottom sum beads */ - for (i = len - sum; i < len; i++) BEAD(i, j) = 1; + for (i = len - sum; i < len; i++) + BEAD(i, j) = 1; } - - for (i = 0; i < len; i++) { - for (j = 0; j < max && BEAD(i, j); j++); + + for (i = 0; i < len; i++) + { + for (j = 0; j < max && BEAD(i, j); j++) + ; a[i] = j; } free(beads); } -int main(int argc, const char * argv[]) { - int n; - printf("Enter size of array:\n"); - scanf("%d", &n); // E.g. 8 1 2 3 - - printf("Enter the elements of the array\n"); - int i; - int arr[n]; - for(i = 0; i < n; i++){ - scanf("%d", &arr[i] ); - } - - printf("Original array: "); - display(arr, n); - - bead_sort(arr, n); - - printf("Sorted array: "); - display(arr, n); - - return 0; +int main(int argc, const char *argv[]) +{ + int n; + printf("Enter size of array:\n"); + scanf("%d", &n); // E.g. 8 1 2 3 + + printf("Enter the elements of the array\n"); + int i; + int *arr = (int *)malloc(n * sizeof(int)); + for (i = 0; i < n; i++) + { + scanf("%d", &arr[i]); + } + + printf("Original array: "); + display(arr, n); + + bead_sort(arr, n); + + printf("Sorted array: "); + display(arr, n); + + free(arr); + return 0; } diff --git a/sorting/Bubble_Sort.c b/sorting/Bubble_Sort.c index fb025988..7d51bc13 100644 --- a/sorting/Bubble_Sort.c +++ b/sorting/Bubble_Sort.c @@ -1,62 +1,70 @@ //sorting of array list using bubble sort #include +#include /*Displays the array, passed to this method*/ -void display(int arr[], int n){ - +void display(int *arr, int n) +{ + int i; - for(i = 0; i < n; i++){ + for (i = 0; i < n; i++) + { printf("%d ", arr[i]); } - + printf("\n"); - } /*Swap function to swap two values*/ -void swap(int *first, int *second){ - +void swap(int *first, int *second) +{ + int temp = *first; *first = *second; *second = temp; - } /*This is where the sorting of the array takes place arr[] --- Array to be sorted size --- Array Size */ -void bubbleSort(int arr[], int size){ - - for(int i=0; iarr[j+1]) { - swap(&arr[j], &arr[j+1]); +void bubbleSort(int *arr, int size) +{ + + for (int i = 0; i < size - 1; i++) + { + for (int j = 0; j < size - 1 - i; j++) + { + if (arr[j] > arr[j + 1]) + { + swap(&arr[j], &arr[j + 1]); } } } } -int main(int argc, const char * argv[]) { +int main(int argc, const char *argv[]) +{ int n; printf("Enter size of array:\n"); scanf("%d", &n); // E.g. 8 - + printf("Enter the elements of the array\n"); int i; - int arr[n]; - for(i = 0; i < n; i++){ - scanf("%d", &arr[i] ); + int *arr = (int *)malloc(n * sizeof(int)); + for (i = 0; i < n; i++) + { + scanf("%d", &arr[i]); } - + printf("Original array: "); - display(arr, n); // Original array : 10 11 9 8 4 7 3 8 - + display(arr, n); // Original array : 10 11 9 8 4 7 3 8 + bubbleSort(arr, n); - + printf("Sorted array: "); - display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 - + display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 + + free(arr); return 0; } - diff --git a/sorting/Cycle_Sort.c b/sorting/Cycle_Sort.c index 20464700..77cfcaa3 100644 --- a/sorting/Cycle_Sort.c +++ b/sorting/Cycle_Sort.c @@ -3,35 +3,37 @@ #include // Displays the array, passed to this method -void display(int arr[], int n){ - +void display(int *arr, int n) +{ + int i; - for(i = 0; i < n; i++){ + for (i = 0; i < n; i++) + { printf("%d ", arr[i]); } - + printf("\n"); - } // Swap function to swap two values -void swap(int *first, int *second){ - +void swap(int *first, int *second) +{ + int temp = *first; *first = *second; *second = temp; - } // Function sort the array using Cycle sort -void cycleSort(int arr[], int n) +void cycleSort(int *arr, int n) { // count number of memory writes int writes = 0; // traverse array elements and put it to on // the right place - for (int cycle_start = 0; cycle_start <= n - 2; cycle_start++) { + for (int cycle_start = 0; cycle_start <= n - 2; cycle_start++) + { // initialize item as starting point int item = arr[cycle_start]; @@ -51,13 +53,15 @@ void cycleSort(int arr[], int n) pos += 1; // put the item to it's right position - if (pos != cycle_start) { + if (pos != cycle_start) + { swap(&item, &arr[pos]); writes++; } // Rotate rest of the cycle - while (pos != cycle_start) { + while (pos != cycle_start) + { pos = cycle_start; // Find position where we put the element @@ -70,29 +74,29 @@ void cycleSort(int arr[], int n) pos += 1; // put the item to it's right position - if (item != arr[pos]) { + if (item != arr[pos]) + { swap(&item, &arr[pos]); writes++; } } } - } - // Driver program to test above function int main() { - int n; // Size of array elements + int n; // Size of array elements printf("Enter size of array:\n"); scanf("%d", &n); // E.g. 8 - + printf("Enter the elements of the array\n"); int i; - int arr[n]; - for(i = 0; i < n; i++){ - scanf("%d", &arr[i] ); + int *arr = (int *)malloc(n * sizeof(int)); + for (i = 0; i < n; i++) + { + scanf("%d", &arr[i]); } printf("Original array: "); @@ -102,5 +106,6 @@ int main() printf("Sorted array: "); display(arr, n); + free(arr); return 0; }