Implement Program for “Making Change” using Greedy design technique.


#include<stdio.h>
#include<conio.h>
int C[]={1,5,10,25,100};

void make_change(int n);
int bestsol(int,int);

void main()
{
            int n;
            clrscr();
            printf("\n Enter amount you want:");
 scanf("%d",&n);
            make_change(n);
getch();
}


void make_change(int n)
{

            int S[100],s=0,x,ind=0,i;
            for(i=0;i<= 4;i++)
            printf("%5d",C[i]);

while(s!=n)
 {
                         x=bestsol(s,n);
                         if(x==-1)
                        {}
                         else
                         {
                                    S[ind++]=x;
                                     s=s+x;
                        }
 }

 printf("\nMAKING CHANGE FOR %4d\n",n);
 for(i=0;i < ind;i++)
 {
             printf("\n%5d",S[i]);
 }
 printf("\n");
}

int bestsol(int s,int n)
{
 int i;
 for(i=4;i>-1;i--)
            {
                         if((s+C[i]) <= n)
                          return C[i] ;
            }
            return -1;
}

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.