Professional Documents
Culture Documents
Daa 9
Daa 9
PART 1
#include <stdio.h>
#include <limits.h>
#include <math.h>
}
}
int main()
{
int B[] = {10, 15, 16, 9, 25, 6, 5, 18, 5, 21};
int S[] = {24, 13, 8, 12, 9, 21, 7, 21, 6, 14};
int n = sizeof(B) / sizeof(B[0]);
printf("\n");
printf("n = %d \n", n);
printf("Buying Prices:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", B[i]);
}
printf("\n");
printf("Selling Prices:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", S[i]);
}
printf("\n");
int C = 1000;
// Initial capital
printf("\n");
printf("Method 1\n");
printf("Max Profit: %d", singletrans1(B, S, n, C));
printf("\n");
printf("\n");
printf("Method 2\n");
singletrans2(B, S, n, C);
printf("\n");
printf("\n");
return 0;
}
Output:
n = 10
Buying Prices:
10 15 16 9 25 6 5 18 5 21
Selling Prices:
24 13 8 12 9 21 7 21 6 14
Method 1
Max Profit: 3200
Method 2
Buying date: 6
Selling date: 7
Max Profit: 3200
Part 2
#include <stdio.h>
#include <stdlib.h>
if (sell_date != -1)
{
int profit = max_possible_buy * (max_sell_price -
B[i]);
if (profit > 0)
{
struct Transaction t;
t.buy_date = i;
t.sell_date = sell_date;
t.buy_rate = B[i];
t.sell_rate = S[sell_date];
t.profit = profit;
transactions[num_transactions] = t;
num_transactions++;
capital += profit;
}
}
}
printf("***Multiple transactions\n");
printf("Initial Capital = %d\n", C);
int total_profit = 0;
int sell_date = -1;
total_profit += transactions[i].profit;
sell_date = transactions[i].sell_date;
}
int main()
{
int n, C;
printf("Enter n = ");
scanf("%d", &n);
printf("Enter C = ");
scanf("%d", &C);
singletrans(B, S, n, C);
singletrans2(B, S, n, C);
multitrans(B, S, n, C);
return 0;
}
Output:
cd "c:\Users\HP\daa lab\" ; if ($?) { gcc daa9_part2.c -o daa9_part2 } ; if
($?) { .\daa9_part2 }
Enter n = 10
Buying prices: 400
500
500
555
555
785
855
87
5
56
Selling prices: 455
455
855
988
5
55
5
5
5
666
Enter C = 120