Question
PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks from your mystocks.txt file. You can use this information
PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA:
1.)Read your stocks from your mystocks.txt file. You can use this information for the simulator.
2.)The stock price simulator will return the current price of the stock. The current prices is the prices of the requested ticker + a random number.
3.)We will assume that the stock price remains constant after the price check till your trade gets executed. Therefore, a buy or a sell order placed will be assumed to be executed at the price returned at the time of price check (called quote).
4.)The application will be able to facilitate purchase and sale of a stock.
5.)The application will be user oriented and will keep a record of the five stocks the user owns, allow the user to buy more shares, and sell shares of only those stocks.
6.)The application will also maintain the balance of money the user has deposited from which to buy stocks. Initial balance can be $20,000. When the user buys shares, the cost is taken out of this balance. When the user sells, the proceeds are returned to this balance.
7.)The application should save all the information in a file called portfolio.txt when the application closes it saves. It loads the information from that same file when the application starts if the file exists. File does not exist, the application will assume that the user has never traded before.
8.)Test your functions to make sure that you can make at least 20 trades
*************************************MY TEST CODE **************************************************
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#define RANGE 10
#define MAX_MY_STOCKS 5
#define CAPACITY 5
typedef struct {
char ticker[10];
double price;
} StockPrice;
typedef struct {
char ticker[10];
double shares;
} Stock;
typedef struct {
Stock myStocks[MAX_MY_STOCKS]; // MAX_MY_STOCKS is five.
double balance;
} Portfolio;
void initializeSimulator(char* fileName, StockPrice* pMyStocks, int* pMyStocksSize);
StockPrice* findStockPrice(StockPrice* pMyStocks, int myStocksSize, char* ticker);
double priceSimulator(StockPrice* pMyStocks, int myStocksSize, char* ticker);
void initializePortfolio(char* fileName, Portfolio* myPortfolio, double balance, Stock* myStock)
{
FILE* pFile = 0;
pFile = fopen(fileName, "r");
int capacity = 0, throwAway = 0;
if (pFile == NULL) {
printf("File did not open ");
return;
}
while (fscanf(pFile, "%s %lf", myStock[(capacity)].ticker, throwAway) != EOF) {
myStock[(capacity)].shares = 0;
(capacity)++;
if (capacity >= CAPACITY) return;
}
if (pFile) fclose(pFile);
}
void initializeSimulator(char* fileName, StockPrice* pMyStocks, int* pMyStocksSize) {
FILE* pFile = 0;
pFile = fopen(fileName, "r");
*pMyStocksSize = 0;
if (pFile == NULL) {
printf("File did not open ");
return;
}
while (fscanf(pFile, "%s %lf", pMyStocks[(*pMyStocksSize)].ticker, &(pMyStocks[(*pMyStocksSize)].price)) != EOF) {
(*pMyStocksSize)++;
if (*pMyStocksSize >= CAPACITY) return;
}
if (pFile) fclose(pFile);
}
double priceSimulatorInternal(double price) {
double perturbation = 1 / price;
double multiplier;
multiplier = ((double)rand()) / RAND_MAX*perturbation;
multiplier -= perturbation / 2;
multiplier += 1;
return multiplier*price;
}
//This function finds the stock corresponding to the ticker symbol provided and
//return the pointer to that StockPrice struct.
StockPrice* findStockPrice(StockPrice* pMyStocks, int myStocksSize, char* ticker) {
int i;
for (i = 0; i if (!strcmp(pMyStocks[i].ticker, ticker)) { return &pMyStocks[i]; } } return 0; } //This function allows user to obtain the current price of a specific stock by //typing in the ticker. The new price is also saved in the myStocks for that stock. //This function can only be called after a call to initalizeSimulator() specified above. double priceSimulator(StockPrice* pMyStocks, int myStocksSize, char* ticker) { StockPrice* pStockPrice; double newPrice; pStockPrice = findStockPrice(pMyStocks, myStocksSize, ticker); newPrice = priceSimulatorInternal(pStockPrice->price); pStockPrice->price = newPrice; return newPrice; } void printStockPrices(StockPrice* pMyStocks, int myStocksSize) { int i; printf(" myStocks[] "); for (i = 0; i { printf("Ticker=%s; Price=%lf ", pMyStocks[i].ticker, pMyStocks[i].price); } } void test() { int i; double p = 103.45; StockPrice* pStockPrice; StockPrice myStocks[MAX_MY_STOCKS]; int myStocksSize; Stock myStock[MAX_MY_STOCKS]; Portfolio* myPortfolio[MAX_MY_STOCKS]; double newPrice, balance =0; srand(time(0)); //Test for initializeSimulator() initializeSimulator("mystocks.txt", myStocks, &myStocksSize, MAX_MY_STOCKS); for (i = 0; i printf("Ticker=%s; Price=%lf ", myStocks[i].ticker, myStocks[i].price); } //Test for initaialize a portfolio() initializePortfolio("mystocks.txt", myPortfolio, balance, myStock); for (int i = 0; i { printf("Ticker=%s; Shares=%lf ", myStock[i].ticker, myStock[i].shares); } char again = 'y'; do { int answer; printf("Welcome to my stock market simulation program "); printf("Please enter a number to select a menu option "); printf("1.) View a single stock price "); printf("2.) View all of the stock prices "); printf("3.) View a portfolio "); printf("4.) Buy a stock "); printf("5.) Sell a stock "); printf("6.) Update "); printf("7.) Quit "); scanf("%d", &answer); switch (answer) { case 1: { char myTicker[4]; char *tick = &myTicker; printf("Please enter the ticker of the stock you would look to see:"); scanf(" %s", &myTicker); printf("%s", myTicker); printf(" Testing findStockPrice() "); pStockPrice = findStockPrice(myStocks, myStocksSize, tick); if (pStockPrice) { printf("Ticker=%s; Price=%lf ", pStockPrice->ticker, pStockPrice->price); } else { printf("Ticker Not found "); } break; } case 2: { printStockPrices(myStocks, myStocksSize); break; } case 3: { } case 4: { } case 5: { } case 6: { } case 7: { again = 'n'; break; } } } while (again == 'y'); system("pause"); } int main(void) { test(); return 0; } /* //Testing findStockPrice() printf(" Testing findStockPrice() "); pStockPrice = findStockPrice(myStocks, myStocksSize,"AEIS"); if(pStockPrice) { printf("Ticker=%s; Price=%lf ",pStockPrice->ticker,pStockPrice->price); }else{ printf("Ticker Not found "); } //Testing priceSimulator() printf(" Testing priceSimulator() "); for( i=0;i<6;i++) { //multiplier = ((double) rand())/RAND_MAX*pertubation -pertubation/2+1; //price = priceSimulatorInternal(p); newPrice = priceSimulator(myStocks,myStocksSize, "ATVI"); printf(" -->%lf ",newPrice); printStockPrices(myStocks,myStocksSize); } */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started