Question
This is C Programming: I need help creating a function that assignes the stocks values and dates to 2D arrays. We are using arrays, pointers,
This is C Programming: I need help creating a function that assignes the stocks values and dates to 2D arrays. We are using arrays, pointers, and structures:
// this function takes in a stats_array of structures containing statistics for each of the data // arrays in our file: open, high, low close, volume and prints a pretty-formatted tabled with the results // like the one shown in the project writeup void print_stats_table(const Stats *stats_array, int size);
{
}
void print_stats_table(const Stats *stats_array, int size) {
}
output:
HERE IS THE WHOLE PROJECT FOR YOU TO REVIEW. I'M TRYING TO BREAK IT UP BY BUILDING EACH FUNCTION FIRST AND THEN WORK ON THE MAIN.:
THIS IS OUR MAIN:
#include
#include
#include "io.h"
#include "stats.h"
#include "utils.h"
#include "common.h"
// this function counts the number of columns and valid lines in
// fname and assigns then to cols and rows, respectively
void get_num_lines(const char *fname, const char delim, int *rows, int *cols);
{
}
// this function assigns the stock values and volume to their respective arrays
// you need to make sure you're not parsing blank lines. use rows and cols as
// an extra safety measure to prevent you from accessing unallocated memory
void get_1d_array_values(const char *fname, float *open, float *close, float *high, float *low, float *volume, char delim, int rows, int cols);
{
}
// the function assigns the stock values and dates to 2d-arrays. stocks_array should have a size
// columns x rows. volume should be included in stocks_array and treated as a float since all the
// statistics done on the volume array will be of type float
void get_2d_array_values(const char *fname, float **stocks_array, char **date, char delim, int rows, int cols);
{
}
// this function takes in a stats_array of structures containing statistics for each of the data
// arrays in our file: open, high, low close, volume and prints a pretty-formatted tabled with the results
// like the one shown in the project writeup
void print_stats_table(const Stats *stats_array, int size);
{
}
int main(int argc, char* argv[])
{
int size, i;
char delim = ',';
// structure that holds the stats for an array
Stats stats,
*stats_array;
// individual arrays that hold the stock values
float *stocks, // stock values from options 1-4
*open, // open stock price from option 5
*close, // close stock price from option 5
*high, // high stock price from option 5
*low, // low stock price from option 5
*volume; // trade volume of stock from option 5
char **date; // array of strings to hold the date from option 5
float **stocks_array; // 2d array to hold the open, close, high, low , & volume stock prices from option 5
int option = 0, rows = 0, cols = 0;
FILE *fname;
if (argc == 1)
{
// greet and get the stock values
print_greeting();
printf("How many stocks prices would you like to analyze? ");
scanf("%d", &size);
stocks = (float *) malloc(size * sizeof(float));
if(stocks == NULL)
{
printf("Error! Cannot allocate memory for stocks - Exiting... ");
exit(0);
}
read_array(stocks, size);
option = 1
}
else if (argc == 2)
{
char *ptr = NULL;
ptr = strchr(argv[1], delim);
// does the string contain the delimiter???
if (ptr == NULL) // this is case $ ./stocks.x 4
{
size = atoi(argv[1]);
stocks = (float *) malloc(size * sizeof(float));
if(stocks == NULL)
{
printf("Error! Cannot allocate memory for stocks - Exiting... ");
exit(0);
}
read_array(stocks, size);
option = 1;
}
else // this is case $ ./stocks 1,2,3,4
{
//
size = get_num_tokens(argv[1], delim);
stocks = (float *) malloc(size * sizeof(float));
if(stocks == NULL)
{
printf("Error! Cannot allocate memory for stocks - Exiting... ");
exit(0);
}
get_tokens_array(argv[1], stocks, size, delim);
option = 1;
}
}
else if (argc > 2) // this is case $ ./stocks.x 1 2 3 4
{
// ignore the executable
size = argc - 1;
stocks = (float *) malloc(size * sizeof(float));
if(stocks == NULL)
{
printf("Error! Cannot allocate memory for stocks - Exiting... ");
exit(0);
}
// assign all the values but the executable to the array
for (int i = 0; i
stocks[i] = atof(argv[i+1]);
option = 1;
}
else if(arg > 3)//this is case $./stocks.x stocks.csv
{
option = 2;
}
if (option == 1)
{
// get stats and print results
get_stats(&stats, stocks, size);
print_results(&stats, stocks, size);
// free memory
free(stocks);
}
else (option == 2)
{
//get the stats for the 1-d arrays
get_1d_array_values(&stats, stocks, size);
//print the stats for the 1d arrays
print_stats_table(&stats, stocks, size);
//get the stats for the 2d arrays
get_2d_array_values(&stats, stocks, size);
//print the stats for the 2 d array
print_stats_table(&stats, stocks, size);
}
return 0;
}
void get_num_lines(const char *fname, char delim, int *rows, int *cols)
{
}
void get_1d_array_values(const char *fname, float *open, float *close, float *high, float *low, float *volume, char delim, int rows, int cols)
{
}
void get_2d_array_values(const char *fname, float **stocks_array, char **date, char delim, int rows, int cols)
{
}
void print_stats_table(const Stats *stats_array, int size)
{
}
THIS IS OUR STATS:
#include
// sorts the values of an array according to order void sort (float output[], int size, char order) { int i, j; float temp;
if (order == 'a' || order == 'A') { for ( i = 0; i output[j] ) { temp = output[i]; output[i] = output[j]; output[j] = temp; } } else if (order == 'd' || order == 'D') { for ( i = 0; i
}
// calculates the mean of the elements of an array float get_average(const float array[], int size) { int i; float sum = 0.0;
for (i = 0; i
sum /= size;
return sum; }
// calculates the variance of the emelemts of an array float get_variance(const float array[], int size) { int i; float variance = 0.0f;
float mean = 0.0f;
for (i = 0; i
mean /= size; variance = variance/size - mean*mean;
return variance;
}
// gets the median of an array after it sorts it float get_median(const float array[], int size) { int i; float temp_array[size]; // temp array tp be manipulated float median;
// copy oroginal array to the temp array for (i = 0; i
sort(temp_array, size, 'a');
if (size % 2 == 0) median = (temp_array[size/2] + temp_array[size/2-1])/2.0; else median = temp_array[size/2];
return median; }
// finds the maximum value of the elements of an array float get_max(const float array[], int size) { int i; float max = array[0];
for (i = 0; i = max) max = array[i];
return max; }
// finds the minimum value of the elements of an array float get_min(const float array[], int size) { int i; float min = array[0];
for (i = 0; i
return min;
}
OUR UTILS:
#include
char *token = strtok(temp, &delim);
while(token) { counter++; token = strtok(NULL, &delim); }
return counter; }
void get_tokens_array(const char str[], float array[], int size, char delim) { char temp[1024]; strcpy(temp, str); char *token = strtok(temp, &delim); int idx = 0; array[idx] = atof(token);
while(token && idx
}
void get_stats(Stats *stats, const float array[], int size) { // get the stats stats->mean = get_average(array, size); stats->stddev= sqrt(get_variance(array, size)); stats->min = get_min(array, size); stats->max = get_max(array, size); stats->median = get_median(array, size); }
OUR IO:
#include
// prompt the user for input and read the values into an array void read_array(float *array, int size) { int i = 0; for (i = 0; i
// say hi to the user void print_greeting(void) { printf("you put whatever greeting you want here "); }
// display array values void print_array(const float array[], int size) { int i = 0;
for (i = 0; i
printf(" ");
}
// print the stat results including input data void print_results(const Stats *stats, const float array[], int size) {
printf(" Say something here about the ouput "); print_array(array, size); print_stats(stats);
}
// print the stat results including input data void print_stats(const Stats *stats) {
printf(" Say something here about the ouput "); printf("%-10s $%.2f ", "min:", stats->min); printf("%-10s $%.2f ", "max:", stats->max); printf("%-10s $%.2f ", "mean:", stats->mean); printf("%-10s $%.2f ", "stddev:", stats->stddev); printf("%-10s $%.2f ", "median:", stats->median);
}
Open Close High Low Volume Min Max Average Median StdevStep 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