Implement Program for “Making Change” using Dynamic Programming.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int min(int a,int b)
{
            if(a<b)
            {
                        return a;
            }
            else
            {
                        return b;
            }
}

void main()
{
            int ncoins,change,i,j,**c;
            int denom[10];
            clrscr();
            printf("enter no.of coins:");
            scanf("%d",&ncoins);
            printf("enter the value of ncoins:\n");
            for(i=0;i<ncoins;i++)
            {
                        scanf("%d",&denom[i]);
            }
            printf("enter the amount to get the change:");
            scanf("%d",&change);

            *c=(int *)malloc(ncoins*sizeof(int));
            for(i=0;i<change;i++)
            {
                        *(c+i)=(int *)malloc((change+1)*sizeof(int));
            }
            for(i=0;i<ncoins;i++)
            {
                        c[i][0]=0;
            }

            for(i=0;i<ncoins;i++)
            {
                        for(j=1;j<=change;j++)
                        {

                                    if(i==0 && j<denom[0])
                                    {
                                                c[i][j]=infinite;
                                    }
                                    else if(i==0)
                                    {
                                                if(c[i][j-denom[0]]==infinite)
                                                {
                                                            c[i][j]=infinite;
                                                }
                                                else
                                                {
                                                            c[i][j]=1+c[i][j-denom[0]];
                                                }
                                    }
                                    else if(j<denom[i])
                                    {
                                                c[i][j]=c[i-1][j];
                                    }
                                    else
                                    {
                                                c[i][j]=min(c[i-1][j],1+c[i][j-denom[i]]);
                                    }
                        }
            }
            printf("Table:\n");
            for(i=0;i<ncoins;i++)
            {
                        for(j=1;j<=change;j++)
                        {
                                    printf("\t%d",c[i][j]);
                        }
                        printf("\n");
            }

            getch();
}

Comments

Popular posts from this blog

Write user defined functions for the following sorting methods and compare their performance by time measurement with random data and Sorted data.

Implement functions to print nth Fibonacci number using iteration and recursive method.

Implement a function of sequential search and count the steps executed by function on various inputs for best case and worst case. Also write complexity in each case and draw a comparative chart.