Professional Documents
Culture Documents
Experiment 8: Faculty of Engineering
Experiment 8: Faculty of Engineering
Computer Engineering
3150703-Analysis & Design of Algorithms-Lab Manual
Experiment 8
Step-1: For i ← 0 to n
SET c[i][0] = 0
Repeat step-2 to step-4 for the remaining matrix
values Step-2: For i ← 0 to n do
Step-3: For i ← 0 to n
do Step-4: If 𝑖 = 1 then
SET c[i][j] = 1 + c[1][j − d1]
Step-5: Else If 𝑗 < 𝑑𝑖 then
SET c[i][j] = c[i − 1][j]
Step-6: Else
SET c[i][j] = min (c[i − 1][j],1 + c[i][j − di])
Step-7: Return c[n, N]
Program:
#include <stdio.h>
void main()
{
int i, j, n, x, a[n];
printf("Enter number of coins:
"); scanf("%d", &n);
printf("Enter maximum value: ");
scanf("%d", &x);
printf("Enter value of each coin in sorted order:
"); for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
else
{
if (m[i - 1][j] == -1 && m[i][j - a[i]] == -
1) m[i][j] = -1;
else
{
if ((1 + m[i][j - a[i]]) <= m[i - 1][j])
m[i][j] = 1 + (m[i][j - a[i]]);
else
m[i][j] = m[i - 1][j];
}
}
}
}
printf("\n\n\t");
for (j = 0; j < x + 1; j++)
{
printf("%d\t", j);
}
printf("\n");
for (i = 0; i < n; i++)
{
printf("%d\t", a[i]);
Output:
Time
complexity: Time Complexity = O(A^d), where d is the number of coins given