Andrew is a stock trader who trades in N
selected stocks. He has calculated the relative
stock price changes in the N stocks from the
previous day stock prices. Now, his lucky
number is K, so he wishes to invest in the
particular stock that has the Kth smallest
relative stock value.
Write an c program for Andrew to find the Kth
smallest stock price out of the selected N
stocks.
Answers
Program is given below.
Explanation:
#include <stdio.h>
int main()
{
int number_of_stocks, previous_day_stocks_price[50],current_day_stocks_price[50],c,d,i,K;
float relative_stocks_price[50],temp=0.0f,a,b;
printf("Enter the number of stocks: ");
scanf("%d",&number_of_stocks);
printf("Enter the previous day stocks prices of %d stocks.\n",number_of_stocks);
for(i=1;i<=number_of_stocks;i++)
{
scanf("%d",&previous_day_stocks_price[i]);
}
printf("Enter the current day stocks prices of %d stocks. \n",number_of_stocks);
for(i=1;i<=number_of_stocks;i++)
{
scanf("%d",¤t_day_stocks_price[i]);
}
printf("Relative stock values of %d stocks is: ",number_of_stocks);
for(i=1;i<=number_of_stocks;i++)
{
a=current_day_stocks_price[i];
b=previous_day_stocks_price[i];
relative_stocks_price[i]=((a-b)/b)*100;
printf("\n%.2f%",relative_stocks_price[i]);
}
//Sorting the relative stocks price
for (c = 0 ; c < number_of_stocks - 1; c++)
{
for (d = 0 ; d < number_of_stocks - c - 1; d++)
{
if (relative_stocks_price[d] > relative_stocks_price[d+1])
{
temp = relative_stocks_price[d+1];
relative_stocks_price[d+1] = relative_stocks_price[d];
relative_stocks_price[d] = temp;
}
}
}
printf("\nThe sorted relative stock values are: \n");
for(i=1;i<=number_of_stocks;i++)
{
printf("%.2f \t",relative_stocks_price[i]);
}
printf("\n \nEnter your lucky number K: ");
scanf("%d",&K);
printf("The %d smallest relative stock value is %.2f.",K,relative_stocks_price[K]);
return 0;
}
Refer the attached image for the output.
# Python linear time solution for stock span problem