Skip to content

Commit

Permalink
use pointer for dynamic memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
kvedala committed Apr 24, 2020
1 parent 2bd049c commit bf1c367
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 122 deletions.
68 changes: 36 additions & 32 deletions misc/Fibonacci_DP.c
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;
}
4 changes: 3 additions & 1 deletion numerical_methods/MEAN.C
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 : ");
Expand All @@ -35,5 +36,6 @@ int main(int argc, char **argv)
printf("\nMean :");
printf("%f", mean);

free(a);
return 0;
}
5 changes: 4 additions & 1 deletion searching/Linear_Search.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>

int linearsearch(int *arr, int size, int val)
{
Expand All @@ -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//
Expand All @@ -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;
}
94 changes: 52 additions & 42 deletions sorting/Bead_Sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
#include <stdlib.h>

/*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
Expand All @@ -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;
}
60 changes: 34 additions & 26 deletions sorting/Bubble_Sort.c
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;
}

Loading

0 comments on commit bf1c367

Please sign in to comment.