Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is for c programming: I have the following code where I need to parse the file name with no location correctly and make the

This is for c programming: I have the following code where I need to parse the file name with no location correctly and make the association from the csv file. I have enclosed the running code and the functions that we are using in this code. Please assist in trying to find the logic and looping mechanisms needed to parse the file name. Your assistance would be greatly appreciated:

Main code:

#include

#include

#include

#include "io.h"

#include "stats.h"

#include "utils.h"

#include "common.h"

int main(int argc, char* argv[])

{

//declare variable int size1 and size2

int size1 = 0, size2 = 0;

//char pointer to a pointer files_list and files_list2

//pointers point to the variables in address of file_list

char **files_list, **files_list2;

//used function to get files

//argv[] is the argument vector array that the CLA

//will be stored in array named argv

get_reg_files_list2(argv[1], &files_list, &size1);

//files_list is equal to get_reg_files_list

files_list2 = get_reg_files_list(argv[1], &size2);

//print size1

//print size2

printf("size1 = %d ", size1);

printf("size2 = %d ", size2);

print_strings(files_list, size1);

print_strings(files__list2, size2);

// int *array;

// get_num_files(argv[1], &size);

// array = (int *)malloc(size*sizeof(int));

// print_int_array(array, size);

// return EXIT_SUCCESS;

// char **files_list, **files_list2;

// get_reg_files_list2(argv[1], &files_list, &size);

// files_list2 = get_reg_files_list(argv[1], &i);

// printf("size = %d ", size);

// printf("size = %d ", i);

// print_strings(files_list, size);

// print_strings(files_list2, i);

// get_stock_values(argv[1], &stocks_array, &date, str_delim, &rows, &cols);

// print_strings(date, rows);

// print_2d_float_array(stocks_array, cols-1, rows);

char str_delim = ',';

int rows = 0, cols = 0;

float **data;

char **date;

printf("%s\t%d ", __FILE__, __LINE__);

//have a pointer to a structure to a type corporation

//and I'm allocating memory to it and I'm giving it a size

//allocate memory for 16 structures b/c each structure is a

//corporation

Corporation *corp = (Corporation *)malloc(sizeof(Corporation));

//read the data

get_stock_values(argv[1], &(corp->data), &(corp->date), str_delim, &corp->data_rows, &corp->data_cols);

//allocate memory

corp->stats = (Stats *) malloc(5*sizeof(Stats));

//stats

get_all_stats(corp->stats, corp->data, corp->data_rows, corp->data_cols-1);

//print_strings(corp->date, corp->data_rows);

//print_2d_float_array(corp->data, corp->data_cols-1, corp->data_rows);

strcpy(corp->name, "My Corp");

strcpy(corp->ticker, "abc");

strcpy(corp->industry, "computers");

strcpy(corp->sector, "electronics");

FILE *fin = fopen("text.out", "w");

if (fin == NULL)

puts("What is going on");

print_stats_table(stdout, corp);

fclose(fin);

return EXIT_SUCCESS;

}

Functions from the library:

common.h

#ifndef COMMON_H

#define COMMON_H

// enumnerated types

typedef enum month {Jan, Feb, Mar, Apr, May, Jun,

Jul, Aug, Sep, Oct, Nov, Dec} Month;

// date structure

typedef struct

{

int month;

int day;

int year;

} Date;

// statistics structure for a list of numbers

typedef struct

{

float min,

max,

mean,

median,

stddev_s,

stddev_p;

} Stats;

// Corporation structues that holds information about a corporation

// and stock pricing data

typedef struct

{

char name[128],

ticker[8],

sector[128],

industry[128],

summary_quote[128];

float last_sale,

market_cap,

adr_tso,

**data; // daily stock prices

char **date; // dates associated with daily stock pricing

int ipo_year,

data_rows,

data_cols,

date_length;

Stats *stats;

Date begin_history,

end_history;

} Corporation;

#endif

io.h file functions:

#ifndef IO_H #define IO_H #include "common.h"

// prompt the user to input data on the command line to be read into the program // array: array of floats of length size // size: length of array void read_cmdln_array(float *array, int size);

// prints the string greeting to the stream fp // fp: valid file or stdout stream // greeting: a null-terminated string with the message void print_info(FILE *fp, const char *greeting);

// prints the elements of an array of floats to the stream fp separated by tabs // array: array of floats of length size // size: the length of array void print_float_array(FILE *fp, const float array[], int size);

// prints the elements of an array of ints to the stream fp separated by tabs // fp: valid file or stdout stream // array: array of ints of length size // size: the length of array void print_int_array(FILE *fp, const int array[], int size);

// prints the stats of to the stream fp // fp: valid file or stdout stream // stats: stats structure containing the stats for an array of numbers void print_stats(FILE *fp, const Stats *stats);

// prints the elements of an array of strings, each on a new line // fp: valid file or stdout stream // strings: array of strings of length // size: the length of strings void print_strings(FILE *fp, char **strings, int size);

// prints the elements of a 2d array of floats to the stream fp in matrix format // fp: valid file or stdout stream // array: 2d array of floats of dimension rowsxcols // size: the number of rows of array // cols: the number of columns of array void print_2d_float_array(FILE *fp, const float **array, int rows, int cols);

// prints the statisitcs of stock data for corporation corp // fp: valid file or stdout stream // corp: corporation structure populated with data and information // sample table format: /* ------------------------------------------------------------- My Corp (abc) computers electronics 3-Oct-17 to 13-Oct-17 ------------------------------------------------------------- Open($) High($) Low($) Close($) Volume ------------------------------------------------------------- Mean 1.73 1.85 1.65 1.75 882442.31 Median 1.72 1.85 1.63 1.72 415087.00 Min 1.43 1.72 1.43 1.64 192615.00 Max 1.86 2.05 1.75 1.90 3427365.00 StdDevP 0.13 0.10 0.09 0.09 1005288.94 StdDevS 0.14 0.11 0.10 0.09 1066269.88 ============================================================= */ void print_stats_table(FILE *fp, Corporation *corp); #endif

stats file:

#ifndef STATS_H #define STATS_H

// sorts an array in place of floats in the specified order // array: array of length size to be sorted // size: length of stats_array // order: 'a' for ascending; 'd' for descending void sort (float array[], int size, char order);

// returns the average of array // array: array of length size // size: length of stats_array float get_average(const float array[], int size);

// returns the sample/population variance of array // array: array of length size // size: length of stats_array // ps: 's' for sample variance; 'p' for population variance float get_variance(const float array[], int size, char ps);

// returns the maximum value of array // array: array of length size // size: length of stats_array float get_max(const float array[], int size);

// returns the minimum value of array // array: array of length size // size: length of stats_array float get_min(const float array[], int size);

// returns the median of array. array is sorted by calling the sort funtion 1st // array: array of length size // size: length of stats_array float get_median(const float array[], int size); #endif

utils.h:

#ifndef UTILS_H #define UTILS_H

#include #include #include #include #include #include #include #include "common.h"

// returns the number of tokens in a dtring str delimited by delim // str: numm-terminated string delimited by delim // delim: character delimiter int get_num_tokens(const char str[], char delim);

// populate array with the tokens from string str delimited by delim //str: null-terminated string delimited by delim // array: array of floats of length size to be populated by the tokens // size: length of array // delim: delimiter of string str void get_tokens_array(const char str[], float array[], int size, char delim);

// populates the Stats structure stats with the statistics for array // stats: structure containing the statistics for array // array: array of floats of length size // size: length of array void get_stats(Stats *stats, const float array[], int size);

// retuens a dynamically allocated double pointer to float of size rows x cols float **allocate_2d_float(int rows, int cols);

// frees a 2d dynamically alocated array // array: 2d dynamically allocated array os size rows x cols // rows: number of rows of array void free_2d_float(float **array, int rows);

// return 1 if path is a regular file (not a link), 0 otherwise int is_reg_file(const char *path);

// return 1 if path is a directoy, 0 otherwis int is_dir(const char *path);

// populates and array inplace with list of regular files in a directory // path: directory containing the files // files_list: 2d array of strings holing the names (not absolute or relative) // of the files in path // size: the number of regular files in path int get_reg_files_list2(const char *path, char ***files_list, int *size);

// returns an array of regular file names (stings) in a directory // path: directory containing the files // size: the number of regular files in path char **get_reg_files_list(const char *path, int *size);

// popuates data with the daily stock values and their corresponding dates // fname: the name of the file containing the data. the format of the file is: // Date,Open,High,Low,Close,Volume // arranged in column format // data: array that hold the Open, High, Low, Close, and Volume data of size // cols x rows. The cols of the file are the rows of the array // date: the dates if the daily stock values. That's the first column of the // file of length size. The dates are of type string. // rows: the number of non empty lines in the file. this is the number of columns // of data and number of rows of date. This is assigned in inplace // cols: the number columns in the file. this is the number of rows of data // this is assigned inplace int get_stock_values(const char *fname, float ***data, char ***date, char delim, int *rows, int *cols);

// converts inplace all the characters of str from lower to upper case void to_upper(char *str);

// converts inplace all the characters of str from upper to lower case void to_lower(char *str);

// populates the Stats stucture stats with statistics of the 2d array data // stats: array of structures of size cols that hold the statistics for // each row of data // data: 2d array of size rowsxcols where each row is an array of the data of // the same context // rows: the number of rows of the 2d array data // cols: the number of columns of the 2d array data void get_all_stats(Stats *stats, const float **data, int rows, int cols);

#endif

running code:

image text in transcribed

How output will look:

image text in transcribed

This is how the code will be run (all variations and combinations are implied): 1. $ ./stocks.x fname -c company_list -o output_file $ ./stocks.x -c company_list fname -o output_file $ ./stocks.x -c company_list o $ ./stocks.x o output_file -c company_list file_name output file file_name This is how the code will be run (all variations and combinations are implied): 1. $ ./stocks.x fname -c company_list -o output_file $ ./stocks.x -c company_list fname -o output_file $ ./stocks.x -c company_list o $ ./stocks.x o output_file -c company_list file_name output file file_name

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Query Formulation And Administration Using Oracle And PostgreSQL

Authors: Michael Mannino

8th Edition

1948426951, 978-1948426954

More Books

Students also viewed these Databases questions