C Programming: Please provide explanation as you work out the code, or comment.
ID: 3597274 • 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. I have include the functions we are using for the mean, variance, min, max, and median of input data. ADD A FUNCTION CALLED PARSE STRING
Thank you very much.
add a function called parse string
Project 3:
Stocks.c --this is our main:
* gcc -o stocks.x stocks.c
* ./stocks.x
*
*/
#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 stock values
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 file :
// calculates the mean of the elements of an array
float get_average(const float array[], int size)
{
int i;
float sum = 0.0;
for (i = 0; i < size; i++)
sum += array[i];
sum /= size;
return sum;
}
// calculates the variance of the emelemts of an array
// this function calls the get_average to get the mean
float get_variance(const float array[], int size)
{
int i;
float sum = 0.0;
float mean = get_average(array, size);
for (i = 0; i < size; i++)
sum += array[i] * array[i];
sum = sum/size - mean*mean;
return sum;
}
// gets the median of an array after it sorts it
float get_median(const float array[], int size)
{
int i;
float temp_array[size]; // temp array tp be manipulated
float median;
// copy oroginal array to the temp array
for (i = 0; i < size; i++)
temp_array[i] = array[i];
sort(temp_array, size, 'a');
if (size % 2 == 0)
median = (temp_array[size/2] + temp_array[size/2-1])/2.0;
else
median = temp_array[size/2];
return median;
}
// finds the maximum value of the elements of an array
int get_max(const float array[], int size)
{
int i;
float max = array[0];
for (i = 0; i < size; i++)
if (array[i] >= max)
max = array[i];
return max;
}
// finds the minimum value of the elements of an array
int get_min(const float array[], int size)
{
int i;
float min = array[0];
for (i = 0; i < size; i++)
if (array[i] <= min)
min = array[i];
return min;
}
Io.c file
* this is the io.c file
*
*
*/
#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 #%d stocks seperated by a comma. ", i+1);
scanf("%f", &array[i]);
}
}
// say hi to the user
void print_greeting(void)
{
printf("Welcome! Today we are reading and analyzing stocks. ");
}
// 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);
}
Algorithms Data: Stock prices input in several ways Result: mean, variance, min, max, and median of input data i begin 2if the user enters nothing on the command line then This is the modified Project 3 greet and read the data; 4 end 5 else if they enter one value then if it's a single number then Implement the mdified Project 3 logic in reading the data; end else assume the delimiter is a ','; count the number of entries; allocate array; parse the input string and store the values into arrays; 10 12 13 14 15 end 16else 17 18 19end end get the number of entries; put each entry into an array;Explanation / Answer
Hi,
You can use the below function to parse the srting and store them in floar array, i have added comments to help you understand.
#include <stdlib.h> /* strtod */
#include <string.h>
float * parseString(char str[])
{
float result[10];//declaring result array, using max size for size
char *token = NULL;//intializing token to split
int count=0;
char *unconverted;
for(token = strtok(str,","); token != NULL; token = strtok(NULL, ","))
{//strtok splits string according to the delimeter which is ,
result[count]=strtod(token, &unconverted);//adding to the result array, strtod, converts the token char* to float
count++;
}
return result;//returning result
}
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.