Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Segmentation error :( how do I fix this in my code and need help adding just the regular program stock.x runs normal program regular program

Segmentation error :( how do I fix this in my code and need help adding just the regular program

stock.x runs normal program regular program

stock.x 13 is on command line run prgram

stock.x 13 26 13 is on command line but with spaces run program

stock.x 13, 26, 13, 40 is on command line but with commas run program

image text in transcribed

#include

#include

#include

#include

#include "io.h"

#include "io.c"

#include "stats.h"

#include "stats.c"

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

{

int size, i;

char order;

//user has enter nothing in command prompt

float *array;

if(argc

{

//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=1)

{

//user enters one value

//check whether the argument is numeric

}

//user enters a value seperated by ','

//assume the delimeter is a ','

else if(argv[1]=",")

{

//count the number of entries

//allocate the values into array

/eed to use a single pointer to allocate the values into array, but how?

int i;

for (i = 0; i

{

//parse the input string and store the value into arrays

float * parseString(char str[])

{

float *result = (float*)malloc(10*sizeof(float)); // declaring result array, using max size for size

char *token = NULL;//intializing token to split

int count=0;

char *unconverted;

for(token = strtok(str,","); token != NULL; token = strtok(NULL, ","))

{

//strtok splits string according to the delimeter which is ,

result[count]=strtod(token, &unconverted);//adding to the result array, strtod, converts the token char* to float

count++;

}

return result;//returning result

}

{

size=atoi(argv[i]);

}

}

}

//user enters the number of entries

else

// get the number of entries

{

//allocate the values into array

int i;

for (i = 0; i

{

}

}

// 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;

}

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); int get_max(const float array[], int size); int get_min(const float array[], int size); int get_num_tokens(const char str[], char delim); #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 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 // 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

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

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 int 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 int get_min(const float array[], int size) { int i; float min = array[0];

for (i = 0; i

return min;

}

io.h

#ifndef IO_H #define IO_H

void read_array(float array[], int size); void print_greeting(void); void print_array(const float array[], int size); void print_results(const float array[], int size, float median, float min, float max, float mean, float variance);

#endif

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

// say hi to the user void print_greeting(void) { printf("Welcome! Today we are reading and analyzing stocks. "); }

// 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 float array[], int size, float median, float min, float max, float mean, float variance) {

printf(" Say something here about the ouput "); print_array(array, size); printf("median: $%.2f ", median); printf("min: $%.2f ", min); printf("max: $%.2f ", max); printf("variance: $%.2f ", variance); printf("mean: $%.2f ", mean);

}

Algorithms Data: Stock prices input in several ways Result: mean, variance, min, max, and median of input data i begin 2 if the user enters nothing on the command line then This is the modified Project 3 greet and read the data; 4 end else if they enter one value then if it's a single number then Implement the mdified Project 3 logic in reading the data; end else assume the delimiter is a: count the number of entries; allocate array parse the input string and store the values into arrays 12 13 14 end end 16 else get the number of entries put each entry into an array 18 19 end 20 end 21 Now you should have the array populate 22 The same as project 3 from this point forward; Algorithm 1: Gather the data from the user then process How to run the code $./stocks .x # conditional online 2 $ ./stocks.x 13 # conditional on line 6 $ ./stocks .x 12.5,11.31 ,12,22.43 , 23.1,31 ,12,11,11.11 # conditional o /stocks . x 12.5 11.31 12 22.43 23.1 31 12 11 11.11 # conditional o

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

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions

Question

5. Prepare for the role of interviewee

Answered: 1 week ago

Question

6. Secure job interviews and manage them with confidence

Answered: 1 week ago