-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem14.c
More file actions
53 lines (48 loc) · 1.01 KB
/
problem14.c
File metadata and controls
53 lines (48 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
void getDaysNo(int *n)
{
while (1)
{
printf("Enter the no of days (2 - 100): ");
scanf("%d", n);
if (*n >= 2 && *n <= 100)
break;
printf("!! Invalid Input !!\n");
}
}
void getStockPrice(int n, int *stock)
{
printf("Enter the stock Prices: ");
for (int i = 0; i < n; i++)
{
scanf("%d", stock + i);
}
}
void printMaxProfit(int n, int *stock)
{
int minPrice = stock[0];
int maxProfit = 0;
for (int i = 1; i < n; i++)
{
if (stock[i] - minPrice >= 2)
{
int profit = stock[i] - minPrice;
if (profit > maxProfit)
maxProfit = profit;
}
if (stock[i] < minPrice)
minPrice = stock[i];
}
printf("Maximum Profit = %d\n", maxProfit);
}
int main()
{
int n;
getDaysNo(&n);
int *stock = (int *)malloc(n * sizeof(int));
getStockPrice(n, stock);
printMaxProfit(n, stock);
free(stock);
return 0;
}