Must compile on Visual studio 2015 Language C Sprint LTE 8:33 AM 100% Goals (Req
ID: 3911529 • Letter: M
Question
Must compile on Visual studio 2015 Language C Sprint LTE 8:33 AM 100% Goals (Requirements): In this project you will calculate the maximum profit you could have made by trading a single stock over a period of one month. You will assume that you bought rounded stock units (usually in hundreds, or tens) worth between $3000 and $5000 at the best possible"closing AFootnote " price during the period and sold it at the best possible "closing" price. You will assume that you are permitted to buy your units just once during this period and then sell it just once. You have to buy them before you can sell them (long trading only, no shorts) Example I could have bought 70 units of FSLR on 2018/06/11 for $51.82/unit for a total price of $3627.40, and sold it on 2018/06/14 $53.71/unit for a total proceeds of $3759.70 for a profit of $132.30. I did it just be inspection. This may not be the maximum profit I could have made. You can use the power of C language earn better profits. Even though in this example, I state the dates of the trade, you do not have to specify when you bought and when you sold. All you need to find is what price you paid at what price you sold, and calculate the profit you madeExplanation / Answer
---------------------------------------------------------
Source Code::
#include <stdio.h>
#include <stdlib.h>
void banner();
double getMinPrice(double* pStockPrices, int size);
double getMaxPrice(double* pStockPrices, int size);
int main(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
size_t read;
double prices[100];
int size = 0;
fp = fopen("prices.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
prices[size] = strtod(line, NULL);
printf("%f ", prices[size]);
size ++;
}
printf("Size of array is %d", size);
fclose(fp);
if (line)
free(line);
banner();
printf(" Minimum closing price: %f", getMinPrice(&prices, size));
printf(" Maximum closing price: %f", getMaxPrice(&prices, size));
exit(EXIT_SUCCESS);
}
void banner() {
printf(" ----------------------------------------------------------------");
printf(" This will calculate the maximum profit you could have made by trading a single stock over a period of one month.");
printf(" ----------------------------------------------------------------");
}
double getMinPrice(double* pStockPrices, int size) {
double small = 0;
for (int x=0; x<size;x++)
{
if(x==0)
small = pStockPrices[x];
else {
if(small > pStockPrices[x])
small = pStockPrices[x];
}
}
return small;
}
double getMaxPrice(double* pStockPrices, int size) {
double big = 0;
for (int x=0; x<size;x++)
{
if(x==0)
big = pStockPrices[x];
else {
if(big < pStockPrices[x])
big = pStockPrices[x];
}
}
return big;
}
--------------------------------------------
prices.txt
52.36
53.05
52.66
52.13
50.98
51.91
52.64
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.