mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
use pointer for dynamic memory allocation
This commit is contained in:
parent
2bd049c73a
commit
bf1c367a62
@ -1,49 +1,53 @@
|
|||||||
//Fibonacci Series using Dynamic Programming
|
//Fibonacci Series using Dynamic Programming
|
||||||
|
|
||||||
/* Author: Moinak Banerjee(moinak878)
|
/* Author: Moinak Banerjee(moinak878)
|
||||||
Date : 1 October ,2019
|
Date : 1 October ,2019
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include<stdio.h>
|
#include <stdio.h>
|
||||||
#include<stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int fib(int n)
|
int fib(int n)
|
||||||
{
|
{
|
||||||
//Out of Range checking
|
//Out of Range checking
|
||||||
if(n<0){
|
if (n < 0)
|
||||||
|
{
|
||||||
printf("\nNo Such term !\n");
|
printf("\nNo Such term !\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
//declaring array to store fibonacci numbers -- memoization
|
//declaring array to store fibonacci numbers -- memoization
|
||||||
int f[n+2]; // one extra to handle edge case, n = 0
|
int *f = (int *)malloc((n + 2) * sizeof(int)); // one extra to handle edge case, n = 0
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* let 0th and 1st number of the series be 0 and 1*/
|
/* let 0th and 1st number of the series be 0 and 1*/
|
||||||
f[0] = 0;
|
f[0] = 0;
|
||||||
f[1] = 1;
|
f[1] = 1;
|
||||||
|
|
||||||
for (i = 2; i <= n; i++)
|
for (i = 2; i <= n; i++)
|
||||||
{
|
{
|
||||||
// Adding the previous 2 terms to make the 3rd term
|
// Adding the previous 2 terms to make the 3rd term
|
||||||
f[i] = f[i-1] + f[i-2];
|
f[i] = f[i - 1] + f[i - 2];
|
||||||
}
|
}
|
||||||
|
|
||||||
return f[n];
|
int out = f[n];
|
||||||
}
|
free(f);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
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)
|
if (argc == 2)
|
||||||
number = atoi(argv[1]);
|
number = atoi(argv[1]);
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
printf("Enter the value of n(n starts from 0 ): ");
|
printf("Enter the value of n(n starts from 0 ): ");
|
||||||
scanf("%d", &number);
|
scanf("%d", &number);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("The nth term is : %d \n", fib(number));
|
printf("The nth term is : %d \n", fib(number));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
int main(int argc, char **argv)
|
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;
|
float mean;
|
||||||
|
|
||||||
if (argc == 2)
|
if (argc == 2)
|
||||||
@ -18,6 +18,7 @@ int main(int argc, char **argv)
|
|||||||
fprintf(stderr, "Maximum %d!\n", MAX_LEN);
|
fprintf(stderr, "Maximum %d!\n", MAX_LEN);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
a = (int *)malloc(n * sizeof(int));
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Random Numbers Generated are : ");
|
printf("Random Numbers Generated are : ");
|
||||||
@ -35,5 +36,6 @@ int main(int argc, char **argv)
|
|||||||
printf("\nMean :");
|
printf("\nMean :");
|
||||||
printf("%f", mean);
|
printf("%f", mean);
|
||||||
|
|
||||||
|
free(a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
int linearsearch(int *arr, int size, int val)
|
int linearsearch(int *arr, int size, int val)
|
||||||
{
|
{
|
||||||
@ -17,7 +18,7 @@ int main()
|
|||||||
printf("Enter the size of the array:\n");
|
printf("Enter the size of the array:\n");
|
||||||
scanf("%d", &n); //Taking input for the size of Array
|
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);
|
printf("Enter the contents for an array of size %d:\n", n);
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
scanf("%d", &a[i]); // accepts the values of array elements until the loop terminates//
|
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);
|
printf("Value %d is in the array.\n", v);
|
||||||
else
|
else
|
||||||
printf("Value %d is not in the array.\n", v);
|
printf("Value %d is not in the array.\n", v);
|
||||||
|
|
||||||
|
free(a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,16 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
/*Displays the array, passed to this method*/
|
/*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++){
|
int i;
|
||||||
printf("%d ", arr[i]);
|
for (i = 0; i < n; i++)
|
||||||
}
|
{
|
||||||
|
printf("%d ", arr[i]);
|
||||||
printf("\n");
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*This is where the sorting of the array takes place
|
/*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;
|
int i, j, max, sum;
|
||||||
unsigned char *beads;
|
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++)
|
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);
|
beads = calloc(1, max * len);
|
||||||
|
|
||||||
/* mark the beads */
|
/* mark the beads */
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
for (j = 0; j < a[i]; j++)
|
for (j = 0; j < a[i]; j++)
|
||||||
BEAD(i, j) = 1;
|
BEAD(i, j) = 1;
|
||||||
|
|
||||||
for (j = 0; j < max; j++) {
|
for (j = 0; j < max; j++)
|
||||||
|
{
|
||||||
/* count how many beads are on each post */
|
/* 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);
|
sum += BEAD(i, j);
|
||||||
BEAD(i, j) = 0;
|
BEAD(i, j) = 0;
|
||||||
}
|
}
|
||||||
/* mark bottom sum beads */
|
/* 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 (i = 0; i < len; i++)
|
||||||
for (j = 0; j < max && BEAD(i, j); j++);
|
{
|
||||||
|
for (j = 0; j < max && BEAD(i, j); j++)
|
||||||
|
;
|
||||||
a[i] = j;
|
a[i] = j;
|
||||||
}
|
}
|
||||||
free(beads);
|
free(beads);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char * argv[]) {
|
int main(int argc, const char *argv[])
|
||||||
int n;
|
{
|
||||||
printf("Enter size of array:\n");
|
int n;
|
||||||
scanf("%d", &n); // E.g. 8 1 2 3
|
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;
|
printf("Enter the elements of the array\n");
|
||||||
int arr[n];
|
int i;
|
||||||
for(i = 0; i < n; i++){
|
int *arr = (int *)malloc(n * sizeof(int));
|
||||||
scanf("%d", &arr[i] );
|
for (i = 0; i < n; i++)
|
||||||
}
|
{
|
||||||
|
scanf("%d", &arr[i]);
|
||||||
printf("Original array: ");
|
}
|
||||||
display(arr, n);
|
|
||||||
|
printf("Original array: ");
|
||||||
bead_sort(arr, n);
|
display(arr, n);
|
||||||
|
|
||||||
printf("Sorted array: ");
|
bead_sort(arr, n);
|
||||||
display(arr, n);
|
|
||||||
|
printf("Sorted array: ");
|
||||||
return 0;
|
display(arr, n);
|
||||||
|
|
||||||
|
free(arr);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,62 +1,70 @@
|
|||||||
//sorting of array list using bubble sort
|
//sorting of array list using bubble sort
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
/*Displays the array, passed to this method*/
|
/*Displays the array, passed to this method*/
|
||||||
void display(int arr[], int n){
|
void display(int *arr, int n)
|
||||||
|
{
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < n; i++){
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*Swap function to swap two values*/
|
/*Swap function to swap two values*/
|
||||||
void swap(int *first, int *second){
|
void swap(int *first, int *second)
|
||||||
|
{
|
||||||
|
|
||||||
int temp = *first;
|
int temp = *first;
|
||||||
*first = *second;
|
*first = *second;
|
||||||
*second = temp;
|
*second = temp;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*This is where the sorting of the array takes place
|
/*This is where the sorting of the array takes place
|
||||||
arr[] --- Array to be sorted
|
arr[] --- Array to be sorted
|
||||||
size --- Array Size
|
size --- Array Size
|
||||||
*/
|
*/
|
||||||
void bubbleSort(int arr[], int size){
|
void bubbleSort(int *arr, int size)
|
||||||
|
{
|
||||||
for(int i=0; i<size-1; i++) {
|
|
||||||
for(int j=0; j<size-1-i; j++) {
|
for (int i = 0; i < size - 1; i++)
|
||||||
if(arr[j]>arr[j+1]) {
|
{
|
||||||
swap(&arr[j], &arr[j+1]);
|
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;
|
int n;
|
||||||
printf("Enter size of array:\n");
|
printf("Enter size of array:\n");
|
||||||
scanf("%d", &n); // E.g. 8
|
scanf("%d", &n); // E.g. 8
|
||||||
|
|
||||||
printf("Enter the elements of the array\n");
|
printf("Enter the elements of the array\n");
|
||||||
int i;
|
int i;
|
||||||
int arr[n];
|
int *arr = (int *)malloc(n * sizeof(int));
|
||||||
for(i = 0; i < n; i++){
|
for (i = 0; i < n; i++)
|
||||||
scanf("%d", &arr[i] );
|
{
|
||||||
|
scanf("%d", &arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Original array: ");
|
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);
|
bubbleSort(arr, n);
|
||||||
|
|
||||||
printf("Sorted array: ");
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,35 +3,37 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
// Displays the array, passed to this method
|
// Displays the array, passed to this method
|
||||||
void display(int arr[], int n){
|
void display(int *arr, int n)
|
||||||
|
{
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < n; i++){
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Swap function to swap two values
|
// Swap function to swap two values
|
||||||
void swap(int *first, int *second){
|
void swap(int *first, int *second)
|
||||||
|
{
|
||||||
|
|
||||||
int temp = *first;
|
int temp = *first;
|
||||||
*first = *second;
|
*first = *second;
|
||||||
*second = temp;
|
*second = temp;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function sort the array using Cycle sort
|
// Function sort the array using Cycle sort
|
||||||
void cycleSort(int arr[], int n)
|
void cycleSort(int *arr, int n)
|
||||||
{
|
{
|
||||||
// count number of memory writes
|
// count number of memory writes
|
||||||
int writes = 0;
|
int writes = 0;
|
||||||
|
|
||||||
// traverse array elements and put it to on
|
// traverse array elements and put it to on
|
||||||
// the right place
|
// 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
|
// initialize item as starting point
|
||||||
int item = arr[cycle_start];
|
int item = arr[cycle_start];
|
||||||
|
|
||||||
@ -51,13 +53,15 @@ void cycleSort(int arr[], int n)
|
|||||||
pos += 1;
|
pos += 1;
|
||||||
|
|
||||||
// put the item to it's right position
|
// put the item to it's right position
|
||||||
if (pos != cycle_start) {
|
if (pos != cycle_start)
|
||||||
|
{
|
||||||
swap(&item, &arr[pos]);
|
swap(&item, &arr[pos]);
|
||||||
writes++;
|
writes++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rotate rest of the cycle
|
// Rotate rest of the cycle
|
||||||
while (pos != cycle_start) {
|
while (pos != cycle_start)
|
||||||
|
{
|
||||||
pos = cycle_start;
|
pos = cycle_start;
|
||||||
|
|
||||||
// Find position where we put the element
|
// Find position where we put the element
|
||||||
@ -70,29 +74,29 @@ void cycleSort(int arr[], int n)
|
|||||||
pos += 1;
|
pos += 1;
|
||||||
|
|
||||||
// put the item to it's right position
|
// put the item to it's right position
|
||||||
if (item != arr[pos]) {
|
if (item != arr[pos])
|
||||||
|
{
|
||||||
swap(&item, &arr[pos]);
|
swap(&item, &arr[pos]);
|
||||||
writes++;
|
writes++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Driver program to test above function
|
// Driver program to test above function
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int n; // Size of array elements
|
int n; // Size of array elements
|
||||||
|
|
||||||
printf("Enter size of array:\n");
|
printf("Enter size of array:\n");
|
||||||
scanf("%d", &n); // E.g. 8
|
scanf("%d", &n); // E.g. 8
|
||||||
|
|
||||||
printf("Enter the elements of the array\n");
|
printf("Enter the elements of the array\n");
|
||||||
int i;
|
int i;
|
||||||
int arr[n];
|
int *arr = (int *)malloc(n * sizeof(int));
|
||||||
for(i = 0; i < n; i++){
|
for (i = 0; i < n; i++)
|
||||||
scanf("%d", &arr[i] );
|
{
|
||||||
|
scanf("%d", &arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Original array: ");
|
printf("Original array: ");
|
||||||
@ -102,5 +106,6 @@ int main()
|
|||||||
printf("Sorted array: ");
|
printf("Sorted array: ");
|
||||||
display(arr, n);
|
display(arr, n);
|
||||||
|
|
||||||
|
free(arr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user