Question
I run my code on the command line but when I do I seem to get a segmentation fault. What could be wrong with it.
I run my code on the command line but when I do I seem to get a segmentation fault. What could be wrong with it.
The goal of the code is to take a .csv and organize the information in rows and coloumns
Here is the main code
#include
#include
#include "../include/common.h"
#include "../include/stats.h"
#include "../include/utils.h"
#include "../include/io.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;
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 *pointer1 = NULL;
char temp_delim = '.';
pointer1 = strchr(argv[1], temp_delim);
if (pointer1 != NULL)
{
char *fname = argv[1];
get_num_lines(fname, delim, &rows, &cols);
open = (float *) malloc (rows *sizeof(float));
close = (float *) malloc (rows *sizeof(float));
high = (float *) malloc (rows *sizeof(float));
low = (float *) malloc (rows *sizeof(float));
volume = (float *) malloc (rows *sizeof(float));
stocks_array = (float **) malloc (rows * sizeof(float));
date = (char **) malloc (rows * sizeof(char *));
for (int i =0; i
{
stocks_array[i] = (float *) malloc ((cols-1) * sizeof(float));
date[i] = (char *) malloc (10 * sizeof(char));
}
get_1d_array_values(fname, open, close, high, low, volume, delim, rows, cols);
stats_array = (Stats *) malloc ((cols -1) *sizeof(Stats));
get_stats(stats_array, open, rows);
get_stats(stats_array + 1, close, rows);
get_stats(stats_array + 2, high, rows);
get_stats(stats_array + 3, low, rows);
get_stats(stats_array + 4, volume, rows);
print_stats_table (stats_array, cols - 1);
free(stocks_array);
}
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 < size; i++)
stocks[i] = atof(argv[i+1]);
option = 1;
}
if (option == 1)
{
// get stats and print results
get_stats(&stats, stocks, size);
print_results(&stats, stocks, size);
// free memory
free(stocks);
}
return 0;
}
void get_num_lines(const char *fname, char delim, int *rows, int *cols)
{
int c, r; //colum and rows
char row[256] = {0x0};
FILE *project_5;
project_5 = fopen( fname, "r");
delim = ',';
while( !feof (project_5))
{
fgets(row, 255, project_5);
if(row[0] == ' ')
{
r++;
}
}
char *token = strtok(row, &delim);
while(token != NULL)
{
c++;
token = strtok(NULL, &delim);
}
*cols = c;
*rows = r;
fclose(project_5);
}
void get_1d_array_values(const char *fname, float *open, float *close, float *high, float *low, float *volume, char delim, int rows, int cols)
{
int i =0;
char row[256];
char temp_row[1024];
char *token;
FILE *project_5;
project_5 = fopen(fname, "r");
fseek(project_5, 0, SEEK_SET);
fgets(row, 255, project_5);
for (i =0; i < rows; i++)
{
fgets(row, 255, project_5);
strcpy(temp_row, row);
token = strtok(temp_row, &delim);
token = strtok(NULL, &delim);
open[i] = atof(token);
token = strtok(NULL, &delim);
close[i] = atof(token);
token = strtok(NULL, &delim);
high[i] = atof(token);
token = strtok(NULL, &delim);
low[i] = atof(token);
token = strtok(NULL, &delim);
volume[i] = atof(token);
}
fclose(project_5);
}
void get_2d_array_values(const char *fname, float **stocks_array, char **date, char delim, int rows, int cols)
{
FILE *project_5;
project_5 = fopen(fname, "r");
int r, c, end_coloumn;
char* token;
char row[256];
for(r=0; r < rows; ++r)
{
fgets(row, 255, project_5);
token = strtok(row, &delim);
for (c=1; c < cols; ++c)
{
token = strtok(NULL, &delim);
if ( c == 1)
{
end_coloumn = (token - &row[0]);
}
stocks_array[r][c] = atof(token);
}
for(end_coloumn = 0; end_coloumn > 0; --end_coloumn)
{
date[r][end_coloumn] = row[end_coloumn];
}
}
fclose(project_5);
}
void print_stats_table(const Stats *stats_array, int size)
{
int i = 0;
printf(" open high low close volume ");
printf("min: ");
for(i = 0; i < size; i++)
{
printf("%2f ",stats_array[i].min);
}
printf(" max: ");
for(i = 0; i < size; i++)
{
printf("%2f ",stats_array[i].max);
}
printf(" average: ");
for(i = 0; i < size; i++)
{
printf("%2f ",stats_array[i].mean);
}
printf(" stdv: ");
for(i = 0; i < size; i++)
{
printf("%2f ",stats_array[i].stddev);
}
printf(" median: ");
for(i = 0; i < size; i++)
{
printf("%2f ",stats_array[i].median);
}
}
Here are the rest of the codes incase you want to try to run it
io.c
#include #include "io.h"
// 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 < size; i++) { printf ("Please enter stock entry #%d: ", i+1); scanf("%f", array + 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 < size; i++) printf("%.2f ", array[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);
}
io.h
#ifndef IO_H #define IO_H #include "common.h"
void read_array(float *array, int size); void print_greeting(void); void print_array(const float array[], int size); void print_results(const Stats *stats, const float array[], int size); void print_stats(const Stats *stats);
#endif
stats.c
#include
#include "stats.h"
// sorts the values of an array according to order
void sort (float output[], const int size, char order)
{
int i, j;
float temp;
if (order == 'a' || order == 'A')
{
for ( i = 0; i < size - 1; ++i )
for ( j = i + 1; j < size; ++j )
if ( output[i] > output[j] )
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else if (order == 'd' || order == 'D')
{
for ( i = 0; i < size - 1; ++i )
for ( j = i + 1; j < size; ++j )
if ( output[i] < output[j] )
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else
return;
}
// 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 < size; i++)
sum += array[i];
sum /= size;
return sum;
}
// calculates the variance of the emelemts of an array
// this function calls the get_average to get the mean
float get_variance(const float array[], int size)
{
int i;
float sum = 0.0;
float mean = get_average(array, size);
for (i = 0; i < size; i++)
sum += array[i] * array[i];
sum = sum/size - mean*mean;
return sum;
}
// 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 < size; i++)
temp_array[i] = array[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 < size; i++)
if (array[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 < size; i++)
if (array[i] <= min)
min = array[i];
return min;
}
stats.h
#ifndef STATS_H #define STATS_H
void sort (float output[], int size, char order); float get_average(const float array[], int size); float get_variance(const float array[], int size); float get_median(const float array[], int size); float get_max(const float array[], int size); float get_min(const float array[], int size);
#endif
utils.c
#include #include "utils.h" #include "stats.h" int get_num_tokens(const char str[], char delim) { int counter = 0; char temp[1024]; strcpy(temp, str);
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 < size - 1) { token = strtok(NULL, &delim); array[++idx] = atof(token); }
}
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); }
utils.h
#ifndef UTILS_H #define UTILS_H
#include #include #include "common.h"
int get_num_tokens(const char str[], char delim); void get_tokens_array(const char str[], float array[], int size, char delim);
void get_stats(Stats *stats, const float array[], int size);
#endif
common.h
#ifndef COMMON_H #define COMMON_H
typedef struct { float min, max, mean, stddev, median; } Stats;
#endif
The test file is big but here it is
intc.csv
13-Oct-17 | 39.44 | 39.81 | 39.28 | 39.67 | 16829366 |
12-Oct-17 | 39.35 | 39.39 | 38.98 | 39.19 | 18286944 |
11-Oct-17 | 39.48 | 39.67 | 39.06 | 39.3 | 30754708 |
10-Oct-17 | 39.93 | 39.95 | 39.38 | 39.65 | 29890017 |
9-Oct-17 | 39.68 | 39.88 | 39.52 | 39.86 | 18494080 |
6-Oct-17 | 39.6 | 39.89 | 39.42 | 39.63 | 18887536 |
5-Oct-17 | 39.5 | 39.65 | 39.21 | 39.53 | 17710277 |
4-Oct-17 | 39.39 | 39.4 | 38.86 | 39.34 | 28368824 |
3-Oct-17 | 38.95 | 39.7 | 38.95 | 39.38 | 34002193 |
2-Oct-17 | 38.12 | 39.09 | 38.08 | 39.04 | 37394514 |
29-Sep-17 | 37.84 | 38.15 | 37.7 | 38.08 | 23217336 |
28-Sep-17 | 37.32 | 37.88 | 37.29 | 37.83 | 21171441 |
27-Sep-17 | 37.62 | 37.69 | 37.1 | 37.54 | 25727062 |
26-Sep-17 | 37.21 | 37.64 | 37 | 37.47 | 29790369 |
25-Sep-17 | 37.05 | 37.23 | 36.85 | 37.16 | 23180838 |
22-Sep-17 | 36.95 | 37.22 | 36.95 | 37.18 | 21631754 |
21-Sep-17 | 36.99 | 37.27 | 36.85 | 37.2 | 22954158 |
20-Sep-17 | 37.23 | 37.29 | 36.66 | 37.07 | 23957476 |
19-Sep-17 | 37.2 | 37.3 | 37.02 | 37.23 | 23850525 |
18-Sep-17 | 37 | 37.33 | 36.8 | 37 | 19393834 |
15-Sep-17 | 36.55 | 37.08 | 36.22 | 37 | 33596072 |
14-Sep-17 | 36.19 | 36.7 | 36.16 | 36.48 | 18093946 |
13-Sep-17 | 36 | 36.4 | 35.97 | 36.33 | 15509787 |
12-Sep-17 | 35.88 | 36.34 | 35.74 | 36.09 | 19489892 |
11-Sep-17 | 35.49 | 36 | 35.14 | 35.77 | 20037575 |
8-Sep-17 | 35.19 | 35.54 | 35.08 | 35.19 | 14125018 |
7-Sep-17 | 35.88 | 35.95 | 35.33 | 35.54 | 16297077 |
6-Sep-17 | 35.22 | 35.94 | 35.11 | 35.76 | 28076438 |
5-Sep-17 | 35.02 | 35.33 | 34.93 | 35.02 | 19300594 |
1-Sep-17 | 35.24 | 35.39 | 35.07 | 35.09 | 12821972 |
31-Aug-17 | 34.94 | 35.18 | 34.87 | 35.07 | 16366772 |
30-Aug-17 | 34.75 | 34.96 | 34.63 | 34.89 | 18565007 |
29-Aug-17 | 34.51 | 34.75 | 34.46 | 34.73 | 15843668 |
28-Aug-17 | 34.78 | 34.8 | 34.59 | 34.65 | 20712876 |
25-Aug-17 | 34.82 | 34.93 | 34.58 | 34.67 | 14726829 |
24-Aug-17 | 34.7 | 34.89 | 34.55 | 34.71 | 14301892 |
23-Aug-17 | 34.54 | 34.81 | 34.38 | 34.66 | 19648134 |
22-Aug-17 | 35.02 | 35.19 | 34.62 | 34.65 | 26097791 |
21-Aug-17 | 35.09 | 35.28 | 34.7 | 34.92 | 26493271 |
18-Aug-17 | 35.29 | 35.31 | 34.99 | 35.01 | 16231408 |
17-Aug-17 | 35.6 | 35.68 | 35.17 | 35.17 | 19524961 |
16-Aug-17 | 35.98 | 36.07 | 35.56 | 35.81 | 22013991 |
15-Aug-17 | 36.3 | 36.32 | 35.82 | 36 | 21706563 |
14-Aug-17 | 36.12 | 36.47 | 36.08 | 36.34 | 18469448 |
11-Aug-17 | 36.26 | 36.4 | 35.79 | 35.87 | 19275060 |
10-Aug-17 | 36.4 | 36.56 | 36.11 | 36.14 | 22693316 |
9-Aug-17 | 36.28 | 36.66 | 36.02 | 36.59 | 22796797 |
8-Aug-17 | 36.37 | 36.7 | 36.3 | 36.41 | 22819871 |
7-Aug-17 | 36.39 | 36.55 | 36.22 | 36.43 | 18169335 |
4-Aug-17 | 36.45 | 36.56 | 36.1 | 36.3 | 20520301 |
3-Aug-17 | 36.55 | 36.59 | 36.15 | 36.49 | 26611347 |
2-Aug-17 | 36.33 | 36.67 | 36.06 | 36.64 | 35004283 |
1-Aug-17 | 35.66 | 36.43 | 35.57 | 36.35 | 38710244 |
31-Jul-17 | 35.47 | 35.74 | 35.32 | 35.47 | 27056989 |
28-Jul-17 | 35.13 | 35.86 | 35 | 35.31 | 36291484 |
27-Jul-17 | 34.78 | 35.25 | 34.67 | 34.97 | 43062842 |
26-Jul-17 | 34.7 | 34.97 | 34.59 | 34.75 | 15499322 |
25-Jul-17 | 34.55 | 34.74 | 34.4 | 34.67 | 18096682 |
24-Jul-17 | 34.73 | 34.8 | 34.38 | 34.5 | 16146284 |
21-Jul-17 | 34.54 | 34.82 | 34.4 | 34.73 | 22245041 |
20-Jul-17 | 34.54 | 34.84 | 34.48 | 34.75 | 17506216 |
19-Jul-17 | 34.68 | 34.68 | 34.46 | 34.56 | 17036197 |
18-Jul-17 | 34.43 | 34.58 | 34.25 | 34.53 | 14362830 |
17-Jul-17 | 34.74 | 34.74 | 34.36 | 34.47 | 21044407 |
14-Jul-17 | 34.48 | 34.71 | 34.26 | 34.68 | 16316353 |
13-Jul-17 | 34.33 | 34.4 | 34.12 | 34.24 | 15046199 |
12-Jul-17 | 34.28 | 34.39 | 33.94 | 34.25 | 23768337 |
11-Jul-17 | 33.64 | 33.93 | 33.43 | 33.92 | 25737540 |
10-Jul-17 | 33.25 | 33.74 | 33.23 | 33.65 | 29918436 |
7-Jul-17 | 33.7 | 34.12 | 33.7 | 33.88 | 18304460 |
6-Jul-17 | 34.12 | 34.29 | 33.56 | 33.63 | 20733189 |
5-Jul-17 | 33.52 | 34.43 | 33.48 | 34.34 | 30010803 |
3-Jul-17 | 33.51 | 34.03 | 33.43 | 33.46 | 12676894 |
30-Jun-17 | 33.87 | 33.93 | 33.54 | 33.74 | 24432020 |
29-Jun-17 | 33.92 | 34.1 | 33.34 | 33.54 | 25215664 |
28-Jun-17 | 33.77 | 34.24 | 33.74 | 34.2 | 25940057 |
27-Jun-17 | 34 | 34.14 | 33.65 | 33.65 | 27078918 |
26-Jun-17 | 34.26 | 34.51 | 34.03 | 34.07 | 18854766 |
23-Jun-17 | 34.21 | 34.55 | 34.1 | 34.19 | 29260888 |
22-Jun-17 | 34.56 | 34.63 | 34.29 | 34.36 | 24602910 |
21-Jun-17 | 34.33 | 34.6 | 34.09 | 34.58 | 27138493 |
20-Jun-17 | 35.45 | 35.45 | 34.84 | 34.86 | 21536505 |
19-Jun-17 | 35.61 | 35.61 | 35.32 | 35.51 | 21583752 |
16-Jun-17 | 35.28 | 35.33 | 35.01 | 35.21 | 30762781 |
15-Jun-17 | 35.21 | 35.48 | 35.14 | 35.31 | 20410009 |
14-Jun-17 | 35.99 | 36.05 | 35.22 | 35.53 | 19149017 |
13-Jun-17 | 35.68 | 35.95 | 35.53 | 35.88 | 20588247 |
12-Jun-17 | 35.59 | 36.01 | 35.41 | 35.73 | 28488290 |
9-Jun-17 | 36.5 | 36.56 | 35.31 | 35.71 | 33322053 |
8-Jun-17 | 36.34 | 36.53 | 36.16 | 36.48 | 17154167 |
7-Jun-17 | 36.13 | 36.57 | 36.1 | 36.26 | 17191983 |
6-Jun-17 | 36.16 | 36.45 | 35.98 | 36.13 | 18420926 |
5-Jun-17 | 36.3 | 36.5 | 36.2 | 36.34 | 11685699 |
2-Jun-17 | 36.27 | 36.33 | 36 | 36.32 | 19127420 |
1-Jun-17 | 36.12 | 36.13 | 35.8 | 36.12 | 17510915 |
31-May-17 | 36.31 | 36.38 | 35.87 | 36.11 | 17741896 |
30-May-17 | 36.27 | 36.44 | 36.09 | 36.18 | 13026915 |
26-May-17 | 36.26 | 36.33 | 36.13 | 36.26 | 11145523 |
25-May-17 | 36.12 | 36.39 | 36.06 | 36.26 | 13148542 |
24-May-17 | 35.98 | 36.18 | 35.89 | 36.12 | 20860257 |
23-May-17 | 35.87 | 35.99 | 35.56 | 35.86 | 16903704 |
22-May-17 | 35.48 | 36.11 | 35.38 | 35.77 | 14638066 |
19-May-17 | 35.15 | 35.55 | 35.13 | 35.4 | 18340127 |
18-May-17 | 35.19 | 35.47 | 35.08 | 35.22 | 17171872 |
17-May-17 | 35.67 | 35.99 | 35.02 | 35.04 | 26670188 |
16-May-17 | 35.75 | 35.89 | 35.42 | 35.82 | 22291843 |
15-May-17 | 35.54 | 35.72 | 35.42 | 35.63 | 22120137 |
12-May-17 | 35.72 | 35.74 | 35.4 | 35.53 | 19730842 |
11-May-17 | 35.91 | 36 | 35.45 | 35.69 | 21508923 |
10-May-17 | 36.27 | 36.38 | 35.93 | 36.01 | 25133822 |
9-May-17 | 36.48 | 36.75 | 36.28 | 36.37 | 17819144 |
8-May-17 | 36.73 | 36.85 | 36.48 | 36.54 | 17660214 |
5-May-17 | 36.86 | 36.93 | 36.5 | 36.82 | 18119243 |
4-May-17 | 36.99 | 37.11 | 36.64 | 36.85 | 16437088 |
3-May-17 | 36.72 | 37.17 | 36.62 | 36.98 | 22040256 |
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