C Programming: Please provide explanation as you work out the code, or comment.
ID: 3597264 • Letter: C
Question
C Programming: Please provide explanation as you work out the code, or comment. I want to understand why the code is being used, not just view the code. This needs to be in C. He not only wants it in files but wants the code in one full page. Please provide that.
Thank you very much.
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, stats.h. 3. Submit one zipped file in the format lastnameinitials 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.Explanation / Answer
**************************************************************************************
I have provided explanation for each of the following .c files line by line.
Kindly go through it very carefully. I have answered it before as well you can see the answer.
Kindly find the code and the explanation below.
CODE:
stocks.c
* gcc -o stocks.x stocks.c
* ./stocks.x
*
*/
#include <stdio.h>
#include <float.h>
#include <limits.h>
#include "io.h"
#include "stats.h"
int main()
{
int size = 0,i;
float median = 0.0f,
mean = 0.0f,
variance = 0.0f,
max = 0.0f,
min = 0.0f;
float array[size],
data[size];
char set = 'n',
order;
// print greeting
print_greeting();
//ask user number of stocks being analyzed
printf("How many stocks you want to analyze? ");
scanf("%d", &size);
//read data one at a time
read_data(array,size);
//Ask user if they want to sort the prices
printf("Do you want to sort the prices? (Y/N)");
scanf(" %c", &set);
if(set == 'y' || set == 'Y')
{
printf("Do you want to sort the prices in ascending, ");
printf("or descending order? (Type A/D) ");
scanf(" %c", &order);
if(order == 'a' || order == 'A')
sort(array, data, size, 'a');
else if(order == 'd' || order == 'D')
sort(array, data, size, 'd');
}
else if(set=='n' || set=='N')
{
for(i=0;i<size;++i)
data[i]=array[i];
}
//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, mean);
//print computed value
print_results(data, size, mean, median, min, max, variance);
return 0;
}
stats.c
*
* This stats.c file includes the functions for
* average, variance, max, min, and median.
*
*/
#include <stdio.h>
#include "stats.h"
float get_average(const float data[],const int size)
{
int i = 0;
float sum = 0.0f,
avg = 0.0f;
for(i = 0; i < size; i++)
sum += data[i];
//average = sum of stocks/size of the array
avg = sum/ size;
return avg;
}
float get_variance(const float data[],const int size, float mean)
{
int i = 0;
float var = 0.0f,
temp = 0.0f;
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;
return var;
}
float get_max(const float data[], const int size)
{
int i = 0;
float max = data[i];
//determining the max price entered by the user
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=0;
float min = data[i];
//determining the min price entered by the user
for( i = 1;i < size; i++)
{
if(data[i] < min)
min = data[i];
}
return min;
}
void sort(float input[], float output[], const int size, const char order)
{
int i,j;
float temp;
//asceding order of prices entered
if(order =='a' || order=='A')
{
for( i = 0; 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;
}
}
}
}
//descending order of prices entered
else if(order == 'd'||order == 'D')
{
for( i = 0; 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;
}
}
}
}
for(i=0;i<size;++i)
output[i] = input[i];
}
float get_median(const float input[], const int size)
{
float med = 0.0f;
if(size % 2 == 0)
//median calculation of prices entered
med = (input[size / 2] + input[(size / 2) - 1])/ 2.0;
else
med = input [size/2];
return med;
}
io.c
* this is the io.c file
*
*
*/
#include <stdio.h>
#include "io.h"
void read_data( float array[], const int size)
{
int i = 0;
//read data
printf("Please enter the stocks you wish to use: ");
for( i=0; i < size; i++)
{
printf("Please enter %d stock price(s): ",size);
scanf("%f", &array[i]);
}
}
void print_results(const float array[], const int size, float mean,
float median, float min, float max, float variance)
{
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()
{
printf("Welcome to the stock market. Today we are analizing stock. ");
}
Project2:
stocks_for.c
/**
*
*
*/
#include <stdio.h>
#include <limits.h>
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 <= num_stocks; i++)
{
printf("Please enter stock price #%d: ", i);
scanf("%f", &stock_price);
if (stock_price < 0)
continue;
// 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 <= min_value)
min_value = 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 <= min_value)
min_value = 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;
}
EXPLANATION:
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.