This is for C Programming and we are working on arrays. Please convert this to a
ID: 3590595 • Letter: T
Question
This is for C Programming and we are working on arrays.
Please convert this to an array program. Please include a main, io, calc, and makefile. No pointers please.
Thank you so much.
/**
*
*
*/
#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;
}
This is the stats.c portion of my current project:
/*
*
*
*
*
* This stats.c file includes the functions for
* average, variance, max, min, and median.
*
*/
#include <float.h>
#include <limits.h>
#include "stats.h"
float get_average(const float data[], const int size)
{
int i = 0;
float sum = 0.0,
average = 0.0;
for (i = 0; i < size; i++)
sum += data[i];
average = sum/size
return average
}
float get_variance(const float data[], const int size)
{
int i;
float mean = 0.0,
sum = 0.0,
variance 0.0;
mean = get_average(data, size);
for (i = 0; i < size; i++)
sum = sum + data[i] - mean) * (data[i] - mean);
variance = sum/ (size -1);
return sum / size;
}
float get_max(const float data[], const int size)
{
int i;
float max = 0.0;
if(size > 0)
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 = 0.0;
if(size > 0)
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 = 0,
j = 0;
float temp;
for(i = 0; i < size; i++)
output[i] = input[i];
for(i = 0; i = size - 2; i++)
{
for(j = i + 1; j <= size - 1; j++)
{
if(order == 'A')
{
if (output[i] > output[j])
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else if(order == 'D')
{
if(output[i] < output[j])
{
temp = output[i];
otput[i] = output[j];
output[j] temp;
}
}
}
}
}
float get_median(const float input[], const int size)
{
float median = 0.0;
if(size % 2 == 0)
median = (input[size /2] + input[size / 2 - 1]) / 2.0;
else
median = input[size / 2];
return median;
}
Explanation / Answer
#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;
float stock_price[100005];//ARRAY
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[0]);
mean = stock_price[0];
min_value = stock_price[0];
max_value = stock_price[0];
}
else
{
for (int i = 1; i <= num_stocks; i++)
{
printf("Please enter stock price #%d: ", i);
scanf("%f", &stock_price[i-1]);
if (stock_price[i-1] < 0)
continue;
// update good stock price count and accumulate sums per formulas
real_num_stocks++;
}
for(int i = 1; i <= num_stocks; i++)
{
mean += stock_price[i-1];
variance += stock_price[i-1] * stock_price[i-1];
// update the min price
if (stock_price[i-1] <= min_value)
min_value = stock_price[i-1];
// update the max price
if (stock_price[i-1] >= max_value)
max_value = stock_price[i-1];
}
}
// 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;
int i=0;
for (;;)
{
printf("Please enter stock price #%d: ", num_stocks + 1);
scanf("%f", &stock_price[i]);
if (stock_price[i] >= 0)
{
real_num_stocks++;
}
else
break;
i++;
}
for(int j=0;j<i;j++)
{
mean += stock_price[j];
variance += stock_price[j] * stock_price[j];
// update the min price
if (stock_price[j] <= min_value)
min_value = stock_price[j];
// update the max price
if (stock_price[j] >= max_value)
max_value = stock_price[j];
}
// 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;
}
This is the stats.c portion of my current project:
/*
*
*
*
*
* This stats.c file includes the functions for
* average, variance, max, min, and median.
*
*/
#include <float.h>
#include <limits.h>
#include "stats.h"
float get_average(const float data[], const int size)
{
int i = 0;
float sum = 0.0,
average = 0.0;
for (i = 0; i < size; i++)
sum += data[i];
average = sum/size
return average
}
float get_variance(const float data[], const int size)
{
int i;
float mean = 0.0,
sum = 0.0,
variance 0.0;
mean = get_average(data, size);
for (i = 0; i < size; i++)
sum = sum + data[i] - mean) * (data[i] - mean);
variance = sum/ (size -1);
return sum / size;
}
float get_max(const float data[], const int size)
{
int i;
float max = 0.0;
if(size > 0)
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 = 0.0;
if(size > 0)
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 = 0,
j = 0;
float temp;
for(i = 0; i < size; i++)
output[i] = input[i];
for(i = 0; i = size - 2; i++)
{
for(j = i + 1; j <= size - 1; j++)
{
if(order == 'A')
{
if (output[i] > output[j])
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else if(order == 'D')
{
if(output[i] < output[j])
{
temp = output[i];
otput[i] = output[j];
output[j] temp;
}
}
}
}
}
float get_median(const float input[], const int size)
{
float median = 0.0;
if(size % 2 == 0)
median = (input[size /2] + input[size / 2 - 1]) / 2.0;
else
median = input[size / 2];
return median;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.