C Programming: Please provide explanation as you work out the code. I want to un
ID: 3598847 • Letter: C
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 code.
I have included project 3 that we are using for this project.
Thank you very much.
stocks.c:
#include <limits.h>
#include <stdio.h>
#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 <limits.h>
#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 < size - 1; ++i )
for ( j = i + 1; j < size; ++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 < size - 1; ++i )
for ( j = i + 1; j < size; ++j )
if ( output[i] < output[j] )
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else
return;
}
io.c
#include <stdio.h>
#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 < size; i++)
{
printf ("Please enter stock entry #%d: ", i+1);
scanf("%f", &array[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 < size; i++)
printf("%.2f ", array[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);
}
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, stats.h, utils.h, utils.c.Explanation / Answer
I have answered it before as well.
Kindly find the detailed explanation below.
**************************************************************************************
I have provided explanation for each of the following .c files line by line.
Kindly go through it very carefully.
Project3:
**************************************************************************************
File: stocks.c
In the begining 5 includes are used since the file stocks.c uses the functions from the following header files:
#include <stdio.h>
#include <float.h>
#include <limits.h>
#include "io.h"
#include "stats.h"
size is for the size of the array that stores the stocks
int size = 0,i;
Now we use 5 variables for storing the median mean variance max min and initialized to default values:
float median = 0.0f,
mean = 0.0f,
variance = 0.0f,
max = 0.0f,
min = 0.0f;
Creating an array to store the stocks with size of size
float array[size]
print_greeting() This prints a welcome message. This function is defined in io.c, which we have included so we are using that function
Number of stocks he wishes to analize
printf("How many stocks you want to analyze? ");
scanf("%d", &size);size of the array
read_data(array,size);This is also a method of io.c see which reads data into the array. The explanation is given in io.c below.
Asking user if the prices needs to be sorted whether in ascending a or A or descending d or D order
printf("Do you want to sort the prices? (Y/N)");
scanf(" %c", &set);
sort(array, data, size, 'a');method in stats.c which sorts array in ascending or descending depends on the 4th parameter here it is ascending.
Calling the functions of stats.c to get the values of median mean variance max min
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, mean);
printing the computed values
print_results(data, size, mean, median, min, max, variance);
**************************************************************************************
**************************************************************************************
File: stats.c
float get_average(const float data[],const int size)
this function is used to calculate average of the stocks stored in the data array.
First it calculates the sum of all stocks and stores it in sum variable and then
average=sum/size
float get_variance(const float data[],const int size, float mean)
This method calcuates the variance. Variance of the stocks requires the value of mean, so first mean is calculated and then passed as a parameter to this function.It is calculated as:
for(i = 0; i < size; i++)
//the sum of the mean squared
temp += data[i] * data[i];
//is th variance/num of stocks - mean squared
var = temp/size-mean * mean;
Formula is provided in the comments
float get_max(const float data[], const int size)this method calculates the maximum of the data array, is considers maximum to be the first element and the update it when a number higher that it is found in the data array
float get_min(const float data[],const int size)this method calculates the minimum in the same way as above
void sort(float input[], float output[], const int size, const char order)//the name of the array to be sorted is passed into the function with size and type of sort ascending or descending as a character a A d D
The bubble sort technique is used here, the algorithm is provided in the question tself. If you have doubts kindly post in the comments Ill explain it separately. If it is not clear.
float get_median(const float input[], const int size)this method calculates the median of the input array with size as passed in the function
if size is even:
med = (input[size / 2] + input[(size / 2) - 1])/ 2.0;
if size is odd
med = input [size/2];
for even number of elements the center of array is not possible as there will be uneven number of elements of either side of the array so we take the mean of the middle 2 elements. For eg
a,b,c,d,e,f ha s6 elements in sorted manner to find median we do:
(c+d)/2
since the there are even number of elements.
All the methods as explained above have been declared and defined in this file but have been called and used in stocks.c file
I hope it is clear till here.
**************************************************************************************
**************************************************************************************
File: io.c
void read_data( float array[], const int size)
this function reads data from user, runs a loop an stores it in the array of type float using scanf("%f", &array[i]);
void print_results(const float array[], const int size, float mean, float median, float min, float max, float variance)
this function prints the values of the of all the parameters passed to it as:
printf("Mean is %0.2f ", mean);
printf("Median is %0.2f ", median);
printf("Minimum stock is %f0.2 ", min);
printf("Maximum stock is %f0.2 ", max);
printf("Variance is %f0.2 ", variance);
void print_greeting()
This method prints greetings just a message as:
Welcome to the stock market. Today we are analizing stock.
All the function of this file are called in stocks.c as explained above. It is defined and declared here but used in the stocks.c file.
I hope it is clear till here.
**************************************************************************************
Project2:
**************************************************************************************
File: stocks_for.c
In this file, the programs asks:
Do you know how many stocks prices you have?
i.e IF SIZE OF STOCKS OS KNOWN
printf("How many?: ");
scanf("%d", &num_stocks);
Next we take the input for size of stocks if it is known
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;
}
here we directly assign the values of the variable since only 1 elemnts is there
else we run a loop
printf("Please enter stock price #%d: ", i);
scanf("%f", &stock_price);
if (stock_price < 0)
continue; //We ignore the negative values, control goes back to top of the loop
Calculate the mean and variance max and min inside the loop
mean += stock_price;
variance += stock_price * stock_price;
if (stock_price <= min_value)
min_value = stock_price;
if (stock_price >= max_value)
max_value = stock_price;
IF SIZE OF STOCKS IS NOT KNOWN
We run an infinite loop like:
[[ for (;;) ]]
till a negative number is encountered and the rest of the logic is same as the before
if (stock_price >= 0)
real_num_stocks++; this checks if the user has entered any meaningfull data like positive numbers, so mean var max min will have some meaningful values
First check if there are any meaningfull data because negative values are ignored
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;
If there are meaningfull values, printf prints them on the console
printf("Mean = $%.2f ", mean);
printf("Variance = $%.2f ", variance);
printf("Max = $%.2f ", max_value);
printf("Min = $%.2f ", min_value);
}
**************************************************************************************
I hope now you understand how the code works. If you have any doubts in any part you can comment.
Kindly rate the answer if you find it useful.
All the best. :)
***************************************************************************************
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.