Question

C programming course. Cannot use global variables. This is the assignment: In this project you will...

C programming course. Cannot use global variables.

This is the assignment:

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” 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).

I need help coding these two functions:

double getMinPrice(double* pPrices, int size);

scans the array for the lowest value and returns the the minimum price as a double.

double getMaxPrice(double* pPrices, int size);

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

I have created a .txt in my project folder containing these values: (closing price of the stock CRON)

7.7

7.7

7.42

7.05

6.8

7.78

8.11

7.63

8.34

8.52

9.71

10.72

10.65

11.52

11.74

9.86

9.05

9.6

9.34

9.7

9.77

10.22

10.69

10.72

11.32

This is my code so far. Please show me how to creat those two functions. the max and min. they have to be verbaitum.

#define _CRT_SECURE_NO_WARNINGS

#define _NUMB_OF_STOCKS 30

#include <stdio.h>

#include <string.h>

void banner();

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

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

double getMaxPrice(double* pPrices, int size);

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

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

void print(double* pProfits, int profitsSize);*/

void banner() {

   printf("Welcome to Stock Trading \n");

   printf("xxxxxxxxxxxxxxxxxxxxxxxx\n\n");

}

void readPrices() {

   char prices[_NUMB_OF_STOCKS];

   char* stockPrices = "prices.txt";

   long charsRead = 0;

   FILE* pFile = 0;

   pFile = fopen(stockPrices, "r");

   if (pFile) {

      while (fgets(prices, _NUMB_OF_STOCKS, pFile) != NULL) {

          charsRead = strlen(prices);

          printf("Closing price: %s\n", prices);

        }

   }else {

      printf("Error opening %s file for read\n", stockPrices);

   }

if(pFile) fclose(pFile);

}

double getMinPrice(double* pPrices, int size) {

}

void test() {

  

   banner();

   readPrices();

   // getMinPrice();

   // getMaxPrice();

   // profitableTrades();

   // save();

   // print();

}

int main() {

   test();

   return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#define _CRT_SECURE_NO_WARNINGS
#define _NUMB_OF_STOCKS 30
#include <stdio.h>
#include <string.h>

void banner();
void readPrices();

double getMinPrice(double* pPrices, int size);
double getMaxPrice(double* pPrices, int size);
/*
void profitableTrades(double* pPrices, int pricesSize, double* pProfits, int* pProfitsSize);
void save(char* filename, double* pProfits, int profitsSize);
void print(double* pProfits, int profitsSize);
*/
void banner()
{
   printf("Welcome to Stock Trading \n");
   printf("xxxxxxxxxxxxxxxxxxxxxxxx\n\n");

}

void readPrices()
{

   char prices[_NUMB_OF_STOCKS];
   char* stockPrices = "data.txt";
   long charsRead = 0;
   
   FILE* pFile = 0;
   
   pFile = fopen(stockPrices, "r");
   
   if (pFile)
   {
      while (fgets(prices, _NUMB_OF_STOCKS, pFile) != NULL) 
      {
         charsRead = strlen(prices);    
         printf("Closing price: %s\n", prices);
      }
   
   }
   else
   {
      printf("Error opening %s file for read\n", stockPrices);
   }

   if(pFile)
      fclose(pFile);

}

double getMinPrice(double* pPrices, int size) {
   
   int i, min = pPrices[0];

   for(i = 1; i < size; i++)
   {
      if(min > pPrices[i])
         min = pPrices[i];
   }

   return min;
}

double getMaxPrice(double* pPrices, int size) {
   
   int i, max = pPrices[0];

   for(i = 1; i < size; i++)
   {
      if(max < pPrices[i])
         max = pPrices[i];
   }

   return max;
}


void test() {

   banner();

   readPrices();

   //test double array for testing getMInPrice and getMaxPrice function

   double price[] = { 34.56, 56.34, 12.34, 89.43, 67.234, 343.3, 535.5 };

   printf("\nMin : %lf", getMinPrice(price, 7));
   
   printf("\nMax : %lf", getMaxPrice(price, 7));
   
   //     profitableTrades();
   
   //     save();
   
   //     print();

}

int main() {

   test();

   return 0;

}

Select C:\Users\TusharDesktopne.profit.ee Closing price: 7.63 Closing price: 8.34 Closing price: 8.52 Closing price: 9.71 Closing price: 10.72 Closing price: 10.65 Closing price: 11.52 Closing price: 11.74 Closing price: 9.86 Closing price: 9.05 Closing price: 9.6 Closing price: 9.34 Closing price: 9.7 Closing price: 9.77 Closing price: 10.22 Closing price: 10.69 Closing price: 10.72 Closing price: 11.32 Min 12.88888e Max 535.888ee Process exited after 0.1257 seconds with return value 0 Press any key to continue . . .

Note: The required functions are implemented and tested with the test array. For any query or optimization drop a comment.
Add a comment
Know the answer?
Add Answer to:
C programming course. Cannot use global variables. This is the assignment: In this project you will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Pease help ASAP C programming course. Cannot use global variables. This is the assignment: In this...

    Pease help ASAP C programming course. Cannot use global variables. This is the assignment: 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” price during the period and sold it at the best possible “closing” price. You will assume that you are...

  • PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks...

    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...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(...

    Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(char *s) {    /* this is here so code compiles */ return 0; } /* array version */ /* concantenate t to the end of s; s must be big enough */ void str_cat(char s[], char t[]) {    int i, j;    i = j = 0;    while (s[i] != '\0')    /* find end of s */        i++;    while ((s[i++] = t[j++]) != '\0') /* copy t */        ;...

  • C PROGRAM The following is code prints the current activation record number, the memory address of...

    C PROGRAM The following is code prints the current activation record number, the memory address of the current array, followed by the estimated size of the current activation record as a distance between the current array address and the array address from the previous activation record. I need it to run until a segmentation fault occurs and also it must print the estimated size of the runtime stack as a product of the size of current activation record and the...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • Can you help with this C programming question. I have provided the skeleton code below along...

    Can you help with this C programming question. I have provided the skeleton code below along with the Stack/Data/Process Class for you to see/reference. Along with the Stack/Data type definition.   **SKELTON CODE** #include #include #include Stack* concat_stack(Stack *s1, Stack *s2) { //your code here return NULL; } **STACK CLASS FOR YOU TO REFERENCE** #include #include #include #include Stack* create_stack(int stack_capacity) { Stack *s = (Stack*) malloc(sizeof(Stack)); if (stack_capacity < 1) { fprintf(stderr, "Error(create_stack): invalid capacity, set to 10\n"); s->capacity =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT