Professional Documents
Culture Documents
Daa 8
Daa 8
Part 1
#include <stdio.h>
#include <stdbool.h>
int sum = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
int S = sum;
int dp[n][2 * S + 1];
// Dynamic programming table
// Initialize the table with false values
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= 2 * S; j++)
{
dp[i][j] = false;
}
}
dp[0][A[0] + S] = true;
dp[0][-A[0] + S] = true;
int main()
{
int A[] = {7, 12, 1, 9, 5};
int n = 5;
int T = 8;
if (realizable(A, n, T))
{
printf("T is realizable.\n");
} else {
printf("T is not realizable.\n");
}
return 0;
}
Output:
T is realizable.
PART 2
#include <stdio.h>
#include <stdbool.h>
int S = sum;
bool dp[n][2 * 10000 + 1];
int main() {
int A[] = {7, 12, 1, 9, 5};
int n = 5;
int T = 8;
if (realizableAndPrint(A, n, T)) {
printf("T is realizable.\n");
} else {
printf("T is not realizable.\n");
}
return 0;
}
Output:
PS C:\Users\HP\daa lab> cd "c:\Users\HP\daa lab\" ; if ($?) {
gcc daa8_par
cd "c:\Users\HP\daa lab\" ; if ($?) { gcc daa8_part2.c -o
daa8_part2 } ; if ($?) { .\daa8_part2 }
T is realizable.
Part:3
#include <stdio.h>
#include <stdbool.h>
int S = sum;
bool dp[n][2 * 10000 + 1];
int main() {
int A[] = {7, 12, 1, 9, 5};
int n = 5;
int T = 8;
showAllRealizations(A, n, T);
return 0;
}
Output:
All realizations of T:
+5+9-1-12+7
-5+9-1+12-7