Write a C file that computes the statistics of and sort a series of stock prices
ID: 3590461 • Letter: W
Question
Write a C file that computes the statistics of and sort a series of stock prices. The algorithms and file name are listed below.
put inside it these files: Makefile, stocks.c, stats.c,io.c, io.h, stats.h
Prototypes for user defined functions are:
1 float get_average(const float data[], const int size);
2 float get_variance(const float data[], const int size);
3 float get_max(const float data[], const int size);
4 float get_min(const float data[], const int size);
5 void sort(const float input[], float output[], const int size, const char order);
6 float get_median(const float input[], const int size);
7 void read_data(float array[], int size);
8 void print_results(const float array[], int size, float mean, float median, float min, float max, float variance);
9 void print_greeting();
io.h & io.c contains the prototypes and implementation of input/output functions - functions that interact with the user
stats.h & stats.c contains the prototypes and implementation the rest of the functions - the ones that do the computations
stocks.c is the main program
Do not use the pow function or any other mathematical functions. Don’t use pointers either.
C program
Algorithms
Data: Stock prices input on several lines
Result: mean, variance, min, max, median, sorting of input data
1 begin
2 Ask the user for the number of stocks they want to analyze;
3 read them one at a time into an array;
4 When done, ask them if they want to sort their prices (excluding any
negative values) in ascending, descending, or as entered order;
5 Compute the stats ignoring negative values;
6 Display the stock prices according to order, then the statistics. ;
7 end
Explanation / Answer
io.h:
void sort(const float input[], float output[], const int size,
const char order);
void read_data(float array[], int size);
void print_results(const float array[], int size, float mean,
float median, float min, float max, float variance);
void print_greeting();
io.c:
#include <stdio.h>
#include "io.h"
void read_data(float array[], int size)
{
int i = 0;
printf("Please enter the stocks you wish to analyze: ");
for(i = 0; i < size; i++)
{
printf("Please enter the stock price #%d: ", i + 1);
scanf("%f", &array[i]);
}
}
void print_results(const float array[], int size, float mean,
float median, float min, float max, float variance)
{
printf("Mean = $%.2f ", mean);
printf("Median = $%.2f ", median);
printf("Min = $%.2f ", min);
printf("Max = $%.2f ", max);
printf("Variance = $%.2f ", variance);
}
void print_greeting()
{
printf(" This application will compute the statistics of and sort ");
printf("a series of stock prices. ");
}
stats.h:
float get_average(const float data[], const int size);
float get_variance(const float data[], const int size);
float get_max(const float data[], const int size);
float get_min(const float data[], const int size);
void sort(const float input[], float output[], const int size,
const char order);
float get_median(const float input[], const int size);
void read_data(float array[], int size);
//void print_results(const float array[], int size, float mean, float
//median, float min, float max, float variance);
void print_greeting();
stats.c:
#include <float.h>
#include <limits.h>
#include "stats.h"
void sort(const float input[], float output[], const int size, const char order)
{
int i = 0,
j = 0;
float temp;
for(i = 0; i < size; i++)
output[i] = input[i];
for(i = 0; i < size - 2; i++)
{
for(j = i + 1; j <= size - 1; j++)
{
if(order == 'A')
{
if (output[i] > output[j])
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else if(order == 'D')
{
if(output[i] < output[j])
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
}
}
}
float get_average(const float data[], const int size)
{
int i = 0;
float sum = 0.0,
average = 0.0;
for (i = 0; i < size; i++)
sum += data[i];
average = sum/size;
return average;
}
float get_max(const float data[], const int size)
{
float max = data[0];
for(int i = 1; i < size; i++)
if(data[i] > max)
max = data[i];
return max;
}
float get_min(const float data[], const int size)
{
float min = data[0];
for(int i = 0; i < size; i++)
if(data[i] < min)
min = data[i];
return min;
}
float get_median(const float input[], const int size)
{
float output[size];
sort(input, output, size, 'a');
if(size % 2 == 0)
return (output[size/2] + output[size/2+1]) / 2;
else
return output[size/2];
}
float get_variance(const float data[], const int size)
{
double mean = get_average(data, size);
double variance = 0;
for(int i = 0; i < size; i++)
variance += (data[i] - mean) * (data[i] - mean);
variance /= size;
return variance;
}
And stocks.c:
#include <stdio.h>
#include <float.h>
#include <limits.h>
#include "io.c"
#include "stats.c"
int main()
{
int size = 0;
float median = 0.0f,
variance = 0.0f,
max = 0.0f,
min = 0.0f,
average = 0.0f;
float array[size],
data[size],
input[size];
char order;
//print greeting
print_greeting();
//ask user number of stocks being analyzed
printf("Enter the number of stocks we will be analyzing: ");
scanf("%d", &size);
//read data one at a time
read_data(data, size);
//Ask user if they want to sort their prices
printf("Do you want to sort the prices in ascending, ");
printf("or descending order? (A/D). ");
scanf(" %c", &order);
//get the stats
average = get_average(data, size);
median = get_median(data, size);
variance = get_variance(data, size);
max = get_max(data, size);
min = get_min(data, size);
//print the results
print_results(data, size, average, median, min, max, variance);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.