Please provide explanation as you work out the code. I want to understand why th
ID: 3588578 • Letter: P
Question
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. This is my first time working in arrays. Please only include either loop, switches. ABSOLUTELY NO POINTERS. The more information the better.Thank you very much.
Project Detail 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.Explanation / Answer
save these 3 files. compile in gcc and run. If any query please put it in comment section.
Here is a sample test run
/*****************/
Welcome to stock market
How many stocks you want to analyse? 3
Enter 3 stock value separated by space 2.1 0.4 8.9
Do you want to sort the prices? '1' for yes '2' for no 1
Which order you want to sort? '1' for ascending, '2' for descending1
Mean is 3.233333
Median is 0.400000
Minimum stock is 0.400000
Maximum stock is 8.900000
Variance is 16.055552
/*****************************/
/*
* stocks.c
*
* Created on: 08-Oct-2017
* Author:
*/
#include <stdio.h>
#include "stats.h"
int main()
{
print_greeting();
int size;
printf("How many stocks you want to analyse? ");
scanf("%d",&size);
float array[size],data[size];
read_data(array,size);
int c;
fflush(stdin);
printf("Do you want to sort the prices? '1' for yes '2' for no ");
scanf("%d",&c);
if(c==1)
{
printf("Which order you want to sort? '1' for ascending, '2' for descending");
scanf("%d",&c);
if(c==1)
sort(array,data,size,'a');
else if(c==2)
sort(array,data,size,'d');
}
float mean,median,min,max,variance;
//compute mean,median,min,max,variance;
mean=get_average(data,size);
median=get_median(data,size);
min=get_min(data,size);
max=get_max(data,size);
variance=get_variance(data,size);
//print computed value
print_results(data,size,mean,median,min,max,variance);
return 0;
}
/****************************/
/*
* stats.h
*
* Created on: 08-Oct-2017
* Author:
*/
#ifndef STATS_H_
#define 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 data[],const int size);
void read_data( float array[],const int size);
void print_results(const float array[],const int size,float mean,
float median,float min,float max,float variance);
void print_greeting();
#endif /* STATS_H_ */
/*******************/
/*
* stats.c
*
* Created on: 08-Oct-2017
* Author:
*/
#include <stdio.h>
#include "stats.h"
float get_average(const float data[],const int size)
{
int i;
float sum=0.0f,avg=0.0f;
for(i=0;i<size;i++)
sum=sum+data[i];
avg=sum/(float)size;
return avg;
}
float get_variance(const float data[],const int size)
{
int i;
float avg=get_average(data,size);
float sum=0;
for( i=0;i<size;i++)
sum=sum+ ((data[i]-avg)*(data[i]-avg));
float var=sum/(float)size;
return var;
}
float get_max(const float data[],const int size)
{
int i;
float max=data[0];
for( i=1;i<size;i++)
{
if(data[i]>max)
max=data[i];
}
return max;
}
float get_min(const float data[],const int size)
{
int i;
float min=data[0];
for( i=1;i<size;i++)
{
if(data[i]<min)
min=data[i];
}
return min;
}
void sort(const float input[], float output[],const int size,const char order)
{
int i,j;
if(order=='a' || order=='A')
{
for( i=0;i<size;i++)
{
output[i]=input[i];
for( j=i+1;j<size;j++)
{
if(input[i]>input[j])
{
output[i]=input[j];
}
}
}
}
else if(order=='d'||order=='D')
{
for( i=0;i<size;i++)
{
output[i]=input[i];
for( j=i+1;j<size;j++)
{
if(input[i]<input[j])
{
output[i]=input[j];
}
}
}
}
}
float get_median(const float data1[],const int size)
{
float data[size];
float med=0;
sort(data1,data,size,'a');
if(size%2==0)
med=(data[size/2]+data[(size/2)-1])/2.0;
else
med=data[size/2];
return med;
}
void read_data( float array[],const int size)
{
int i;
printf("Enter %d stock value separated by space ",size);
for( i=0;i<size;i++)
scanf("%f",&array[i]);
printf(" ");
}
void print_results(const float array[],const int size,float mean,
float median,float min,float max,float variance)
{
printf("Mean is %f ",mean);
printf("Median is %f ",median);
printf("Minimum stock is %f ",min);
printf("Maximum stock is %f ",max);
printf("Variance is %f ",variance);
}
void print_greeting()
{
printf("Welcome to stock market ");
}
/**************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.