Posts

Showing posts with the label Design and Analysis of Algorithms

Design and Analysis of Algorithms - 10

Image
Implement Program for “Making Change” using Dynamic Programming.   Code: #include<stdlib.h>   struct change_entry {     unsigned int count;     int coin;     struct change_entry *prev; }; typedef struct change_entry change_entry;   unsigned int make_change(const unsigned int *coins, size_t len, unsigned int value,         unsigned int **solution) {     unsigned int i, j;     change_entry **table;     unsigned int result;         table = malloc((len + 1) * sizeof(change_entry *));     for (i = 0; i <= len; i++)     {         table[i] = malloc((value + 1) * sizeof(change_entry));     }       for (i = 0; i <= len; i++)     {   ...

Design and Analysis of Algorithms - 9

Image
Implement Program for “Making Change” using Greedy design technique.   Code: #include<stdio.h> #include<stdlib.h>   void change(int *denom,int nDenom,int change); void main() {     int coin[10];     int i,n,amount=0;     printf("Enter amount:");     scanf("%d",&amount);     printf("Enter number of coins for exchange:");     scanf("%d",&n);     printf("\nEnter value of coins:\n");     for(i=0;i<n;i++)         {             scanf("%d",&coin[i]);         }          change(coin,n,amount); }   void change(int *denom,int nDenom,int change) {     int a,i=0,j,k,sum,ncoin=0,*count;     count=(int*)malloc(nDenom*(sizeof(int)));    ...

Design and Analysis of Algorithms - 8

Image
Implement Program for fractional knapsack using Greedy design technique. Code: #include <stdio.h>   void main() {     int capacity, no_items, cur_weight, item;     int used[10];     float total_profit;     int i;     int weight[10];     int value[10];       printf("Enter the capacity of knapsack:\n");     scanf("%d", &capacity);       printf("Enter the number of items:\n");     scanf("%d", &no_items);       printf("Enter the weight and value of %d item:\n", no_items);     for (i = 0; i < no_items; i++)     {         printf("Weight[%d]:", i);         scanf("%d", &weight[i]);         printf("Value[%d]:", i)...

Design and Analysis of Algorithms - 7

Image
Implement program of Counting Sort. Code: #include <stdio.h> #include <stdlib.h> #include <time.h>   int count1=0; void count_s(int arr1[],int n,int max);   int main() {     int n,i;     float t1,t2;     printf("Enter the length of array: ");     scanf("%d",&n);     int arr[n],ar[n];     for(i=0;i<n;i++)     {         ar[i]=i;     }     for(i=0;i<n;i++)     {         arr[i] = rand()%n;         printf("%d ",arr[i]);     }     int max=0;     for(i=0;i<n;i++)     {         if(arr[i]>max)         {   ...