stocksc main file include include include include ioh includ
stocks.c main file
#include
#include
#include
#include \"io.h\"
#include \"stats.h\"
#include //strtod
#include
int main(int argc, char* argv[])
{
int size, i;
char order;
//user has enter nothing in command prompt
float *array;
if(argc < 1)
{
//from Proj 3 greet and read data
// greet and get the stock values
print_greeting();
printf(\"How many stocks prices would you like to analyze? \");
scanf(\"%d\", &size);
// read the data
array = (float *)malloc(size*sizeof(float));
read_array(array, size);
}
//user has enter only one argument
else if(argc=1)
{
//user enters one value
//check whether the argument is numeric
}
//user enters a value seperated by \',\'
//assume the delimeter is a \',\'
else if(argv[1]=\",\")
{
//count the number of entries
//allocate the values into array
//need to use a single pointer to allocate the values into array, but how?
int i;
for (i = 0; i < argc; i+2)
{
//parse the input string and store the value into arrays
float * parseString(char str[])
{
float *result = (float*)malloc(10*sizeof(float)); // 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
}
{
size=atoi(argv[i]);
}
}
}
//user enters the number of entries
else
// get the number of entries
{
//allocate the values into array
int i;
for (i = 0; i < argc; ++i)
{
}
}
}
// same as from project 3
//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.h
#ifndef STATS_H
#define STATS_H
void sort (float output[], int size, char order);
float get_average(const float array[], int size);
float get_variance(const float array[], int size);
float get_median(const float array[], int size);
int get_max(const float array[], int size);
int get_min(const float array[], int size);
int get_num_tokens(const char str[], char delim);
#endif
stats.c
#include
#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 ;
}
// 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.h
#ifndef IO_H
#define IO_H
void read_array(float array[], int size);
void print_greeting(void);
void print_array(const float array[], int size);
void print_results(const float array[], int size, float median, float min, float max, float mean, float variance);
#endif
io.c
#include
#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);
}
getting these errors need help fixing them so file can compile and run.
stocks.c:75:26: error: \'array\' undeclared here (not in a function)
stocks.c:75:33: error: \'size\' undeclared here (not in a function)
stocks.c:81:1: warning: data definition has no type or storage class [enabled by default]
stocks.c:81:1: warning: parameter names (without types) in function declaration [enabled by default]
stocks.c:81:1: error: conflicting types for \'print_results\'
stocks.c:4:0:
io.h:7:6: note: previous declaration of \'print_results\' was here
stocks.c:82:1: error: expected identifier or \'(\' before \'return\'
stocks.c:83:1: error: expected identifier or \'(\' before \'}\' token
Solution
Issues found..
(1) proper header files not loaded
(2) header files .h and also include .c files also {stats.c && io.c}
(3) one extra flower brace is written which cause early closing of main function...
working code .. (only stocks.c) .. [remaining files are fine]
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include \"io.h\"
#include \"io.c\"
#include \"stats.h\"
#include \"stats.c\"
int main(int argc, char* argv[])
{
int size, i;
char order;
//user has enter nothing in command prompt
float *array;
if(argc < 1)
{
//from Proj 3 greet and read data
// greet and get the stock values
print_greeting();
printf(\"How many stocks prices would you like to analyze? \");
scanf(\"%d\", &size);
// read the data
array = (float *)malloc(size*sizeof(float));
read_array(array, size);
}
//user has enter only one argument
else if(argc=1)
{
//user enters one value
//check whether the argument is numeric
}
//user enters a value seperated by \',\'
//assume the delimeter is a \',\'
else if(argv[1]=\",\")
{
//count the number of entries
//allocate the values into array
//need to use a single pointer to allocate the values into array, but how?
int i;
for (i = 0; i < argc; i+2)
{
//parse the input string and store the value into arrays
float * parseString(char str[])
{
float *result = (float*)malloc(10*sizeof(float)); // 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
}
{
size=atoi(argv[i]);
}
}
}
//user enters the number of entries
else
// get the number of entries
{
//allocate the values into array
int i;
for (i = 0; i < argc; ++i)
{
}
}
// same as from project 3
//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;
}