Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Got most the way through this code but am having issues on implementing step 5.

ID: 3912196 • Letter: G

Question

Got most the way through this code but am having issues on implementing step 5. If you could please help edit my code so that it meets the following requirements. This is coding in C language.

1.)Read the daily closing prices of your Stock from the file “prices.txt” into an Array called “prices”. Remember: the first price is the most recent and the last price in this file is the oldest. So the first element in your array will be the most recent price (today’s or yesterday’s price).

2.)Write a function to find minimum closing price for an array of doubles

3.)Write a function to find the maximum price for an array of doubles.

4.)Write a function that will try out all possible profitable trades and store the results (profits) of all those trades in an “output” array called “profits”.

5.)Write a function to save the “profits” array into a file called “profits.txt”

6.)Write a function (or use an existing function) that will find the maximum profit from the array “profits”.

7.)Write a function to print the “profits” array and the maximum profit on the screen.

THIS IS MY CODE:

************************************************************

void banner();

//This function introduces the application to the user and informs them what it does.

void readPrices(char* fileName, int pricesCapacity, double* pPrices, int* pricesSize);

//Reads the daily closing prices of your Stock from the file “prices.txt” into an Array pointed to by “pPrices”

//(test function should call this array prices[]).

// Remember: the first price is the most recent and the last price in this file is the oldest. So the first element in

// your array will be the most recent price (today’s or yesterday’s price).

//fileName is a string and is an input parameter, but is optional. You can hard code

//the “prices.txt” file name within this function if you wish. Providing it makes the function more reusable.

//pricesCapacity is also an input parameter that informs the function of maximum allocated capacity for the prices[] array.

//void

//pPrices and pricesSize are output parameters. pricesSize is used to return the number of valid entries made in “prices[]”.

double getMinPrice(double* pPrices, int size);

//Credits: Dai Tran. Scans the array for the lowest value and returns the the minimum price as a double.

//Alternative:

//void getMinPrice(double* pPrices, int size, double* pMinPrice);

//void getMaxPrice(double* pPrices, int size, double* pMaxPrice); //Credits: Daniel Bennett. Scans the array for the

//highest value and passes it into double maxPrice

//Alternative:

double getMaxPrice(double* pPrices, int size);

//Scans the array for the highest value and returns the the maximum price as a double.

void profitableTrades(double* pPrices, int pricesSize, double* pProfits, int* pProfitsSize);

//Finds all possible profitable trades and store the results (profits) of all those

//trades in an “output” array called “profits”.

//It buys once and sell once,

//It cannot sell before it buys

//Once it buys, it only sells with the maximum possible sell price after that.

//pPrices and pricesSize are input parameters.

//pProfits and profitsSize are output parameters.

//We do not need profitsCapacity here, because we are guaranteed that size of profits[] is less than that of prices[].

void save(char* filename, double* pProfits, int profitsSize);

//Saves the “profits” array into a file called “profits.txt”

//pProfits and profitsSize are input parameters.

//fileName is a string and is an input parameter, but is optional. You can hard code the “prices.txt” file name within this function if you wish. Providing it makes the function more reusable.

#include <stdio.h>

#define MAX_PRICES_SIZE 30

void banner()

{

printf("The program allows analysis of the IAE Stock ");

printf("========================================================= ");

}

void readPrices(char* fileName, int pricesCapacity, double* pPrices, int* pricesSize)

{

FILE* pFile =0;

double price;

int i=0;

pFile = fopen("prices.txt","r");

if(pFile==NULL) {

printf("File did not open ");

return;

}

while(fscanf(pFile, "%lf",&price)!=EOF) {

pPrices[*pricesSize]=price;

(*pricesSize)++;

}

if(pFile) fclose(pFile);

}

double getMaxPrice(double* pPrices, int size)

{

int i;

double maxPrice;

if(size) maxPrice = pPrices[0];

for(i=1; i<size; i++) {

if(maxPrice<pPrices[i]) maxPrice=pPrices[i];

}

return maxPrice;

}

void profitableTrades(double* pPrices, int pricesSize, double* pProfits, int* pProfitsSize)

{

int i;

double currentSellMaxPrice;

double currentBuyPrice;

double currentMaxProfit;

*pProfitsSize = 0;

for(i=pricesSize-1; i>=0; i--) {

//printf("prices[%d]=%0.2lf ",i,pPrices[i]);

currentBuyPrice = pPrices[i];

currentSellMaxPrice = getMaxPrice(pPrices,i);

currentMaxProfit = currentSellMaxPrice - currentBuyPrice;

if(currentMaxProfit>0) {

pProfits[pricesSize]= currentMaxProfit;

(*pProfitsSize)++;

}

}

}//EO void profitableTrades(double* pPrices, int pricesSize, double* pProfits, int* pProfitsSize)

void save(char* filename, double* pProfits, int* profitsSize)

{

{

FILE* pFile =0;

double profits;

int i=0;

pFile = fopen("profits.txt","w");

if(pFile==NULL) {

printf("File did not open ");

return;

}

while(fscanf(pFile, "%lf",&profits)!=EOF) {

pProfits[*profitsSize]=profits;

(*profitsSize)++;

}

if(pFile) fclose(pFile);

}

}

void print(double* pProfits, int profitsSize)

{

}

void test()

{

char* fileName = "prices.txt";

double prices [MAX_PRICES_SIZE];

int pricesSize=0;

double profits [MAX_PRICES_SIZE];

int profitsSize=0;

int i;

double maxPrice;

banner();

readPrices(fileName, MAX_PRICES_SIZE, prices, &pricesSize);

for (i=0; i<pricesSize; i++) {

printf("prices[%d]=%0.2lf ",i,prices[i]);

}

maxPrice = getMaxPrice(prices,pricesSize);

printf("Max price =%0.2lf ",maxPrice);

}

int main()

{

test();

return 0;

}

Explanation / Answer

If you have any doubts, please give me comment...

#include <stdio.h>

#define MAX_PRICES_SIZE 30

void banner()

{

printf("The program allows analysis of the IAE Stock ");

printf("========================================================= ");

}

void readPrices(char *fileName, int pricesCapacity, double *pPrices, int *pricesSize)

{

FILE *pFile = 0;

double price;

int i = 0;

pFile = fopen("prices.txt", "r");

if (pFile == NULL)

{

printf("File did not open ");

return;

}

while (fscanf(pFile, "%lf", &price) != EOF)

{

pPrices[*pricesSize] = price;

(*pricesSize)++;

}

if (pFile)

fclose(pFile);

}

double getMaxPrice(double *pPrices, int size)

{

int i;

double maxPrice;

if (size)

maxPrice = pPrices[0];

for (i = 1; i < size; i++)

{

if (maxPrice < pPrices[i])

maxPrice = pPrices[i];

}

return maxPrice;

}

void profitableTrades(double *pPrices, int pricesSize, double *pProfits, int *pProfitsSize)

{

int i;

double currentSellMaxPrice;

double currentBuyPrice;

double currentMaxProfit;

*pProfitsSize = 0;

for (i = pricesSize - 1; i >= 0; i--)

{

//printf("prices[%d]=%0.2lf ",i,pPrices[i]);

currentBuyPrice = pPrices[i];

currentSellMaxPrice = getMaxPrice(pPrices, i);

currentMaxProfit = currentSellMaxPrice - currentBuyPrice;

if (currentMaxProfit > 0)

{

pProfits[pricesSize] = currentMaxProfit;

(*pProfitsSize)++;

}

}

} //EO void profitableTrades(double* pPrices, int pricesSize, double* pProfits, int* pProfitsSize)

void save(char *filename, double *pProfits, int *profitsSize)

{

FILE *pFile = 0;

int i = 0;

pFile = fopen("profits.txt", "w");

if (pFile == NULL)

{

printf("File did not open ");

return;

}

for (i = 0; i < *profitsSize; i++)

{

fprintf(pFile, "%lf", pProfits[i]);

}

fclose(pFile);

}

void print(double *pProfits, int profitsSize)

{

int i=0;

for (i = 0; i < profitsSize; i++)

{

printf("%lf", pProfits[i]);

}

double maxProfit = getMaxPrice(pProfits, profitsSize);

printf("Maximum Profit: %lf ", maxProfit);

}

void test()

{

char *fileName = "prices.txt";

double prices[MAX_PRICES_SIZE];

int pricesSize = 0;

double profits[MAX_PRICES_SIZE];

int profitsSize = 0;

int i;

double maxPrice;

banner();

readPrices(fileName, MAX_PRICES_SIZE, prices, &pricesSize);

for (i = 0; i < pricesSize; i++)

{

printf("prices[%d]=%0.2lf ", i, prices[i]);

}

maxPrice = getMaxPrice(prices, pricesSize);

printf("Max price =%0.2lf ", maxPrice);

}

int main()

{

test();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote