Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Linux Virtual Machine I need help fixing and implementing my function into my project. Here are the rules Here is the algorithm to use Here

Linux Virtual Machine

I need help fixing and implementing my function into my project. Here are the rules

image text in transcribed

Here is the algorithm to use

image text in transcribed

Here is my code so far

Stocks.c

#include #include #include "io.h" #include "stats.h"

int main(int argc, char* argv[]) { int size, i; char order;

// greet and get the number of stocks print_greeting(); printf("How many stocks prices would you like to analyze? "); scanf("%d", &size);

// read the data float array[size]; read_array(array, size);

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

for ( j = i + 1; 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

for ( j = i + 1; j

if ( output[i]

{

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

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

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

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

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

if (array[i]

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

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("This program asks for user input and gives variance, average, median, min, and max of stock prices. "); }

// 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(" Here are the Calculations "); print_array(array, size); printf("median: $%.2f ", median); printf("min: $%.2f ", min); printf("max: $%.2f ", max); printf("variance: $%.2f ", variance); printf("mean: $%.2f ", mean);

}

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

utils.c

#include #include #include #include "utils.h"

int get_num_tokens(const char str[], char delim)

{

int i = 0,count = 1;

while (str[i] != '\0')

{

if (str[i++] == delim)

count++;

}

return count;

}

void get_tokens_array(const char str[],float array[], float size, char delim)

{

char *input = const_cast(str);

char *d = &delim;

char *token = strtok(input, d);

int i = 0;

while (token != NULL)

{

printf(" token # %d is %s ", i+1, token);

array[i++] = atof(token);

token = strtok(NULL, d);

}

size = i;

}

Utils.h

#ifndef UTILS_H #define UTILS_H

int get num tokens(const char str[], char delim);

void get tokens array(const char str[], float array[], float size, char delim);

#endif

To be clear I can not use some use commands like malloc, etc.. I can only use strings, pointers, and argc and argv

5. add a function called int get num.tokens(const char strll, char delim): This is a utility function that counts the number of tokens in a string given a delimiter. 6. add a function called void get tokens array(const char strl, float arrayll. 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. 7. (optional) add a function called bool has.delim(const char strl, char delim); this is utilities function that checks to see if a string has a given token. Alternatively you can use strchr function in string.h to do the same thing. 8. If you need to copy a string, create a temporary string of size 1024 or 2948 characters and copy the original string into this temporary string using the strcpy function fom string.h Do not use the pow function or any other mathematical functions Use any looping construct you like.Also don't use pointers or dy- namic memory allocation or global variables

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

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

More Books

Students also viewed these Databases questions