Question: C Programming: Please provide explanation as you work out the code. I want to understand why the code is being used, not just view the

C Programming: Please provide explanation as you work out the code. I want to understand why the code is being used, not just view the code. This needs to be in C.

I have included project 3 and 2 as described in the explanation:

Thank you very much.

C Programming: Please provide explanation as you work out the code. I

want to understand why the code is being used, not just view

the code. This needs to be in C. I have included project

Project 3:

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 stock values 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 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;

}

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); #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("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 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);

}

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

Project 2:

/** * * */

#include #include

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

int num_stocks = 1, real_num_stocks = 0; // handles the negative stock values

float min_value = (float) LONG_MAX, max_value = (float) LONG_MIN, mean = 0.0f, variance = 0.0f, stock_price=1.0f;

char know_num_stocks = 'n'; printf("Do you know how many stocks prices you have? "); scanf("%c", &know_num_stocks);

if (know_num_stocks == 'y' || know_num_stocks == 'Y') { printf("How many?: "); scanf("%d", &num_stocks);

if (num_stocks == 1) { // not much to do. the variance is zero so no need to reassign it scanf("%f", &stock_price); mean = stock_price; min_value = stock_price; max_value = stock_price; } else { for (int i = 1; i

if (stock_price

// update good stock price count and accumulate sums per formulas real_num_stocks++; mean += stock_price; variance += stock_price * stock_price;

// update the min price if (stock_price

// update the max price if (stock_price >= max_value) max_value = stock_price; } } // computation can be done here (uncomment next two lines) //mean /= real_num_stocks; //variance = variance/real_num_stocks - mean * mean;

} else if (know_num_stocks == 'n' || know_num_stocks == 'N') { printf("Ok. Enter the stock prices one at a time (negative price terminates the input) "); num_stocks = 0; for (;;) { printf("Please enter stock price #%d: ", num_stocks + 1); scanf("%f", &stock_price);

if (stock_price >= 0) { real_num_stocks++; mean += stock_price; variance += stock_price * stock_price;

// update the min price if (stock_price

// update the max price if (stock_price >= max_value) max_value = stock_price; } else break; }

// computation can be done here (uncomment next two lines) //mean /= real_num_stocks; //variance = variance/real_num_stocks - mean * mean; } else printf("Get out while you're ahead "); // finish the computation here (uncomment above if you comment this) if (real_num_stocks == 0) printf("You did not enter anything meaningful. Run the program again. Bye! "); else { mean /= real_num_stocks; variance = variance/real_num_stocks - mean * mean;

printf("Mean = $%.2f ", mean); printf("Variance = $%.2f ", variance); printf("Max = $%.2f ", max_value); printf("Min = $%.2f ", min_value); } return 0;

}

Project Detail Start with a working version of the code from Project 3 and write a program that inputs data on the command line (in addition to the way you did it in Project 2) to process stock values. Follow the algorithm below. Instructions 1. Create a folder with the name lastname initials proj4- all lower case. 2. put inside it these files: Makefile, stocks.c, stats.c,io.c, io.h, statsh. 3. Submit one zipped file in the format lastname initials proj3.zip, (or .tar.gz, tgz,.7z,.bz2, etc... to Dropbox 4. Due date and time: no later than 1159PM Friday October 27. 5. add a function called parse string Do not use the pow function or any other mathematical functions. Use any looping construct you like. Project Detail Start with a working version of the code from Project 3 and write a program that inputs data on the command line (in addition to the way you did it in Project 2) to process stock values. Follow the algorithm below. Instructions 1. Create a folder with the name lastname initials proj4- all lower case. 2. put inside it these files: Makefile, stocks.c, stats.c,io.c, io.h, statsh. 3. Submit one zipped file in the format lastname initials proj3.zip, (or .tar.gz, tgz,.7z,.bz2, etc... to Dropbox 4. Due date and time: no later than 1159PM Friday October 27. 5. add a function called parse string Do not use the pow function or any other mathematical functions. Use any looping construct you like

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!