Question: //This is a C program /*The issue i am having is the the array of prices is not printing and the results for max profits

//This is a C program

/*The issue i am having is the the array of prices is not printing and the results for max profits are also not printing correctly, it is also not printing to the profits.txt file*/

Please see why this isnt working and coment next to changes please, Thank you

//here is the prices.txt file

/*2.0 2.0163 2.0 2.17 2.1 2.6 2.25 1.69 1.44 1.436 1.4472 1.41 1.42 1.47 1.51 1.6482 1.66 1.7 1.65 1.7112 1.73 1.69 1.75 1.75 1.81 1.82 1.87 1.88 1.8999 1.9196 1.9 1.84 1.78 1.84 1.84 1.85 1.93 1.95 1.97 2.01 2.0 1.96 2.02 2.04 2.04 2.03 2.05 2.0 2.2 2.4 1.91 1.84 2.0 2.08 2.12 2.07 2.04 2.01 2.07 2.1099 2.0958 2.09 2.1199 2.14 2.24 2.3*/

#define _CRT_SECURE_NO_WARNINGS #include #define MAX_PRICES_SIZE 23 #define MAX_PROFITS_SIZE 50

typedef struct { int pricesCapacity; double prices[MAX_PRICES_SIZE]; int pricesSize; double minPrice; double maxPrice; } Prices;

typedef struct { int profitsCapacity; double profits[MAX_PROFITS_SIZE]; int profitsSize;

} Profits;

//Function Dec

void banner();//Intro function void readPrices(char* fileName, Prices* pPrices);//Reads file and puts into array double getMinPrice(Prices* pPrices);//Finds min price double getMaxPrice(Prices* pPrices);//Finds max price void profitableTrades(Prices* pPrices, Profits* pProfits);//Calculates the most profitable trade void save(char* fileName, Profits* pProfits);//Saves all the trades void print(Profits* pProfits);//Function finds the maximum profits and then prits them

//Functions

void banner() { printf("Hello, and welcome to the stock trading calculator "); printf("================================================== "); printf("This program reads the closing prices for the (CNET) "); printf("and then calculates the highest possible profift you could've made "); printf("if you would've bought 70 shares. "); }

void readPrices(char* fileName, Prices* pPrices) { FILE* pFile = 0; int i = 0; double price; //pPrices->pricesSize = 0;//may cause an issue

pFile = fopen(fileName, "r"); if (pFile) { while (fscanf(pFile, "%lf", &price) > 0 && i < pPrices->pricesCapacity) { pPrices->prices[i] = price; i++; } pPrices->pricesSize = i; }

else { printf("File did not open to read. So terminating the program "); }

if (pFile) fclose(pFile); }

double getMinPrice(Prices* pPrices) { int i = 0; pPrices->minPrice = 0; if (pPrices->pricesSize) pPrices->minPrice = pPrices->prices[0];

for (i = 1; i < pPrices->pricesSize; i++) {

if (pPrices->prices[i] < pPrices->minPrice) { pPrices->minPrice = pPrices->prices[i]; } }

return pPrices->minPrice; }

double getMaxPrice(Prices* pPrices) { int i = 0; pPrices->maxPrice = 0; if (pPrices->pricesSize) pPrices->maxPrice = pPrices->prices[0];

for (i = 1; i < pPrices->pricesSize; i++) {

if (pPrices->prices[i] > pPrices->maxPrice) { pPrices->maxPrice = pPrices->prices[i]; } }

return pPrices->maxPrice; }

void profitableTrades(Prices* pPrices, Profits* pProfits) { int i = 0; double maxProfit; double buyPrice; double sellPrice; pProfits->profitsSize = 0;

for (i = pProfits->profitsSize - 1; i >= 0; i--) { buyPrice = pProfits->profits[i]; sellPrice = getMaxPrice(pPrices, i); maxProfit = sellPrice - buyPrice;

if (maxProfit > 0) { pProfits->profitsSize = maxProfit; pProfits->profitsSize++; } } }

void save(char* fileName, Profits* pProfits) { int i = 0; FILE* pFile = 0; pFile = fopen(fileName, "w");

if (pFile == NULL) { printf("File did not open "); }

for (i = 0; i < pProfits->profitsSize; i++) { fprintf(pFile, "%.2lf ", pProfits->profits[i]); }

if (pFile)fclose(pFile);

}

void print(Profits* pProfits) { double maxProfit; int i = 0; maxProfit = pProfits->profits[i];

printf(" Here are all the possible profits: ");

for (i = 0; i < pProfits->profitsSize; i++) { printf("Profit[%d]:%.2lf ", i, pProfits->profits[i]);

if (pProfits->profits[i] > maxProfit) { maxProfit = pProfits->profits[i]; } }

printf(" "); printf("The maximum profit(per share) that you could have made was: %.2lf ", maxProfit); printf("The maximum profit for all 70 shares would have been: %.2lf ", maxProfit * 70); printf("Thank you for using my trading program. "); }

void test() { char* readFileName = "prices.txt"; char* writeFileName = "profits.txt"; //double prices[MAX_PRICES_SIZE]; //double profits[MAX_PROFITS_SIZE]; //int pricesSize = 0; //int profitsSize = 0; double maxPrice = 0; double minPrice = 0;

Prices prices; prices.pricesCapacity = MAX_PRICES_SIZE; Profits profits; profits.profitsCapacity = MAX_PROFITS_SIZE;

banner(); readPrices(readFileName, &prices);

maxPrice = getMaxPrice(&prices); minPrice = getMinPrice(&prices);

printf("The lowest closing price during this month was: %.2lf ", minPrice); printf("The highest closing price during this month was: %.2lf ", maxPrice);

profitableTrades(&prices, &profits); save(writeFileName, &profits); print(&profits);

}

int main() {

test();

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!