Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Functional Specifications Write a function to read the daily closing prices of your Stock from the file prices.txt into an Array called prices. Remember: the

Functional Specifications

Write a function to 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 (todays or yesterdays price).

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

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

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.

You can buy once and sell once,

you cannot sell before you buy, and

once you buy, you only want to sell with the maximum possible sell price after that.

Hint: You will get a hint later when we get into implementation.

Write a function to save the profits array into a file called profits.txt

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

Write a function to print the profits array and the maximum profit on the screen.

Design

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 (todays or yesterdays price). fileName string is an input parameter.

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

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);

Scans the array for the lowest value and returns 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 maximum price as a double.

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

Determines 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, for a single trade. But compares all possible trades.

In a trade, you can sell only after buying.

After a stock is bought, you should consider all possible trades, but keep the profit for the trade that provides maximum profit.

Consider all possible buying opportunities.

Credits: Vicki Nguyen: There is no need to consider a minimum profit margin in order to sell. Can sell at profit>0.

pPrices and pricesSize are input parameters.

pProfits and pProfitsSize are output parameters.

We do not need profitsCapacity here, because we are guaranteed that the 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 String is an input parameter.

void print(double* pProfits, int profitsSize);

Prints the profits array and the maximum profit on the screen.

pProfits and profitsSize are input parameters.

MY CODE BELOW THE LINE

=======================================================================

#define _CRT_SECURE_NO_WARNINGS

#define MAX_SIZE 30

#include

#include

//Provide Function prototypes here

//Provide definition of your functions here

void banner() {

printf("WELCOME TO THE TRADING STATION! ");

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

}

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

FILE *pFile = 0;

double price;

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

if (pFile == NULL) {

printf("FILE NOT FOUND!");

return;

}

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

pPrices[*pricesSize] = price;

(*pricesSize)++;

}

if (pFile)

fclose(pFile);

}

double getMinPrice(double* pPrices, int size) {

int i;

double minPrice;

if (size) {

minPrice = pPrices[0];

}

for (i = 0; i

if (minPrice > pPrices[i])

minPrice = pPrices[i];

}

return minPrice;

}

double getMaxPrice(double *pPrices, int size) {

int i;

double maxPrice;

if (size) {

maxPrice = pPrices[0];

}

for (i = 1; i

if (maxPrice < pPrices[i])

maxPrice = pPrices[i];

}

return maxPrice;

}

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

int i;

double profitGain, sellingPrice, buyingPrice;

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

//Run through all elements of prices backwards assuming

// each entry is a buy.

//...

// or for(j=i+1;j>=0;j--){

// runs through each possible selling

// opportunity after the ith buy.

// and saves the profit from the

// most profitable trade in the

// profits[] array

//...

*pProfitsSize = 0;

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

buyingPrice = pPrices[i];

sellingPrice = getMaxPrice(pPrices, i);

profitGain = sellingPrice - buyingPrice;

if (profitGain > 0) {

pProfits[pricesSize] = profitGain;

(*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 NOT FOUND!! ");

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() {

double prices[MAX_SIZE], maxPrice, minPrice;

char *fileName = "prices.txt";

int pricesSize = 0, i;

banner();

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

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

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

minPrice = getMinPrice(prices, pricesSize);

printf("Minimum price =%0.2lf ", minPrice);

maxPrice = getMaxPrice(prices, pricesSize);

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

// need help saving the file

//save(); --- save the profit array into a text file

//print(fileName); print the profits array and max profit to user on the screen

//profitableTrades(fileName, MAX_SIZE, prices, &pricesSize);

}

int main()

{

test();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions