Question
Stocks.c main need help creating utils.h, utils.c for these two functions below. add a function called int get num tokens(const char str[], char delim); This
Stocks.c main
need help creating utils.h, utils.c for these two functions below.
add a function called int get num tokens(const char str[], char delim); This is a utility function that counts the number of tokens in a string given a delimiter.
add a function called void get tokens array(const char str[], float array[], float size, char delim); This is a utility function that breaks up the string tokens for a given delimiter and assigns the tokens to an array of floats
Code is below
#include
int main(int argc, char* argv[]) { int size, i; char order; //user has enter nothing in command prompt float *array; //program name will always be 1st argument, followed by other arguments. So minimum valud of argc is 1 if(argc < 2) { //from Proj 3 greet and read data // greet and get the stock values print_greeting(); printf("How many stocks prices would you like to analyze? "); scanf("%d", &size); // read the data array = (float *)malloc(size*sizeof(float)); read_array(array, size); } //user has enter only one argument else if(argc== 2) { if(strchr(argv[1], ',') == NULL) // if the argument is a single number, i.e there are no commas in the single argument { size = atoi(argv[1]); // read the data array = (float *)malloc(size*sizeof(float)); read_array(array, size); } else { //find the number of entries using , as delimiter char *token; char *nums = strdup(argv[1]);//make a copy because it will get modified after using strtok token = strtok(argv[1], ","); size = 0; while(token != NULL) { size++; //printf("%s ", token); token = strtok(NULL, ","); } //now allocate enough memory and convert the values and store in array array = (float*)malloc(size * sizeof(float)); //use the saved copy of argument numbers token = strtok(nums, ","); for(int i = 0; i < size; i++) { array[i] = strtod(token, NULL); token = strtok(NULL, ","); } } } else { size = argc - 1; //less 1 to ignore program name argument array = (float*)malloc(size * sizeof(float));
for(int i = 0; i < size; i++) array[i] = strtod(argv[i+1], NULL); } // same as from project 3 //get the stats float mean = get_average(array, size); float variance = get_variance(array, size); float min = get_min(array, size); float max = get_max(array, size); float median = get_median(array, size); // show the results print_results(array, size, median, min, max, mean, variance); return 0; }
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