Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C file that computes the statistics of and sort a series of stock prices

ID: 3586885 • 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 all user-defined functions in a
header file called stats.h.
Instructions

put inside it these files: Makefile, stocks.c, 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);

Do not use the pow function or any other mathematical functions.


So all the coding above has to be in stats.h file and I guess I need to make arrays to compute the
average variance min max if the user wants it sorted ascending or decending. first how do I make stats.h work with my stocks.c with arrays and 2. what would be the coding for this.

In C program please help and user input of numbers.

So if user says yes then ask how many stocks then the prices, then if he wants to sort Ascending or decending then calculates
so if user says no then ask prices of stock , if they wanted sorted. A negative will stop program and but calcualtes and sorts what the user entered :)

Explanation / Answer

Anser to Question One(how do I make stats.h work with my stocks.c with arrays):

You Just need to write all your function inside stats.h and save it and for security just compile it .Then in stocks.c just use #include"stats.h" in the header section and then use can call or use all the function define in stats.h in stocks.c.

Answer to Question Two(what would be the coding for this.):

Code for Stocks.c

#include <stdio.h>
#include"stats.h"
#define MAXIMUM_SIZE 100
void main() {

int data[MAXIMUM_SIZE];
int output[MAXIMUM_SIZE];
int size;
char choice ,choice1;
int i, j, temp;
float average ,median,max,min,variance;

  
printf("Do you want to enter the number of stocks: Y/N ");
//taking input for character
choice = getchar( );
switch ( choice ) {
case Y:
printf("Enter number of stocks: ");
scanf("%d", &size);
/* Input elements in array */
printf("Please Enter Price for the Stocks: ");
for(i=0; i<size; i++)
{
scanf("%d", &data[i]);
}
printf("How you want to sort the Price: A for Ascending/ D for Decending ");
//taking input for character
choice1 = getchar( );
switch ( choice1 ) {
   case A:
average= get_average(const float data[], const int size);
variance = get_variance(const float data[], const int size);
max= get_max(const float data[], const int size);
min get_min(const float data[], const int size);
output= sort(const float input[], float output[], const int size,const char order);
median= get_median(const float input[], const int size);
break;
case D:
/* Code */
break;

}
break;
case N:
/* Input elements in array */
printf("Please Enter Price for the Stocks: ");
for(i=0; i<MAXIMUM_SIZE; i++)
{
scanf("%d", &data[i]);
}
printf("How you want to sort the Price: A for Ascending/ D for Decending ");
//taking input for character
choice1 = getchar( );
switch ( choice1 ) {
   case A:
average= get_average(const float data[], const int size);
variance = get_variance(const float data[], const int size);
max= get_max(const float data[], const int size);
min get_min(const float data[], const int size);
output= sort(const float input[], float output[], const int size,const char order);
median= get_median(const float input[], const int size);
break;
case D:
/* Code */
break;

}
break;
  
}

printf("", );


}

Code For states.h

float get_average(const float data[], const int size){
  
float average= 0.0;
float sum = 0.0;
int i=0;
for (i ; i < size ; i++) {
sum = sum + data[i];
}
average = sum / size;
return average;

}

float get_variance(const float data[], const int size){

float average= 0.0;
float sum = 0.0;
float sum_one= 0.0;
float variance = 0.0;
float temp = 0.0;
int i=0;
for (i; i < size ; i++) {
sum = sum + data[i];
}
average = sum / size;
for (i; i < size ; i++)
{   
   temp = data[i] - average;
sum_one = sum_one + temp * temp;
}
variance = sum_one / (float)size;
return variance;

}

float get_max(const float data[], const int size){

int i=0;
int position = 0;
float max_value = data[0];


for (i ; i < size; i++)
{
if (data[i] > max_value)
{
max_value = array[i];
position = i+1;
}
}
  
return data[position];

}

float get_min(const float data[], const int size){

int i=0;
int position = 0;
float min_value = data[0];


for (i ; i < size; i++)
{
if (data[i] < min_value)
{
min_value = array[i];
position = i+1;
}
}
  
return data[position];

}
float get_median(const float input[], const int size){
  
float median,temp = 0.0;
int i,j=0;


// Sorting the array - Ascending Order
for (i ; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (input[i] > input[j])
{
temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
}

   if( size % 2 == 0){
median = (input[size/2] + input[size/2+1])/2.0 ;
}   
else{   
median = input[size/2 + 1];
}

return median;
}

void sort(const float input[], float output[], const int size,
const char order){

float temp = 0.0;
int i,j=0;


// Sorting the array - Ascending Order
if(order == 'a'){
for (i ; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (input[i] > input[j])
{
temp = input[i];
input[i] = input[j];
input[j] = temp;
output[j] =temp;
}
}
}
}

// Sorting the array - Decending Order
if(order == 'a'){
for (i ; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (input[i] < input[j])
{
temp = input[i];
input[i] = input[j];
input[j] = temp;
output[j] =temp;
}
}
}
}
return output[];
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote