Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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. This needs to be in C. This is my first time working in arrays. Please only include either loop, switches. ABSOLUTELY NO POINTERS.

I have included project 3 and 2 as described in the explanation:

The more information the better.Thank you very much.

image text in transcribedimage text in transcribed

image text in transcribed

Project 3:

Code:

Three files; stocks.c, stats.c, and io.c:

stocks.c:

* stocks.c

* program is for input stock price using a for looping construct

*

*

* gcc -o stocks.x stocks.c

* ./stocks.x

*

*/

#include

#include

#include

#include "io.h"

#include "stats.h"

int main()

{

int size = 0;

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 == 'y' || order == 'Y')

sort(array, data, size, 'a');

else if(order == 'n' || order == 'N')

sort(array, data, size, 'd');

}

//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.c:

/*

* stats.c

*/

#include

#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

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

//the sum of the mean squared

temp += data[i] * data[i];

//is th varianceum of stocks - mean squared

var = temp / size - mean * mean;

return var;

}

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

{

int i;

float max = data[i];

for( i = 1;i

{

if(data[i] > max)

max = data[i];

}

return max;

}

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

{

int i;

float min = data[i];

for( i = 1;i

{

if(data[i]

min = data[i];

}

return min;

}

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

{

int i,

j = 0.0f;

if(order =='a' || order=='A')

{

for( i = 0; i

{

output[i] = input[i];

for( j = i+1;j

{

if(input[i] > input[j])

{

output[i] = input[j];

}

}

}

}

else if(order == 'd'||order == 'D')

{

for( i = 0; i

{

output[i] = input[i];

for(j = i+1; j

{

if(input[i]

{

output[i] = input[j];

}

}

}

}

}

float get_median(const float input[],const int size)

{

float med = 0.0f;

if(size % 2 == 0)

med = (input[size /2] + input[(size /2) - 1])/ 2.0;

else

med = input [size/2];

return med;

}

io.h:

* this is the io.c file * * */ #include #include "io.h"

void read_data( float array[],const int size) { int i; printf("Please enter stock price(s) %d stock value separated by space ",size); for( i=0; i

Project 2:

/**

*

*

*/

#include

#include

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

{

printf("Please enter stock price #%d: ", i);

scanf("%f", &stock_price);

if (stock_price

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 = 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 = 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;

}

Project Detail Start with a working version of the code from Project 3 and write a program that inputs data on the command line (in addition to the way you did it in Project 2) to process stock values. Follow the algorithm below. 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, statsh 3. Submit one zipped file in the format lastname initials 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Oracle8I Building Efficient Databases

Authors: Jonathan Lewis

1st Edition

0201715848, 978-0201715842

More Books

Students also viewed these Databases questions

Question

Explain what a patent is and describe different types of patents.

Answered: 1 week ago

Question

x-3+1, x23 Let f(x) = -*+3, * Answered: 1 week ago

Answered: 1 week ago