-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use pointer for dynamic memory allocation
- Loading branch information
Showing
6 changed files
with
154 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,53 @@ | ||
//Fibonacci Series using Dynamic Programming | ||
//Fibonacci Series using Dynamic Programming | ||
|
||
/* Author: Moinak Banerjee(moinak878) | ||
Date : 1 October ,2019 | ||
*/ | ||
|
||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
int fib(int n) | ||
{ | ||
//Out of Range checking | ||
if(n<0){ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,70 @@ | ||
//sorting of array list using bubble sort | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
/*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; 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]); | ||
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; | ||
} | ||
|
Oops, something went wrong.