Alright finally got the average to work out, now any suggestions how to find the lowest and highest values entered since the way I tried isn't really working. New code:
Code:
/*
My name is Jack Trocinski
*/
#include <stdio.h>
#include <stdlib.h>
double average(double *ptr, int j);
int main()
{
int j; // number of stocks
double *ptr; // memory allocation
int i; // used for loop
double min; // lowest price
double max; // highest price
printf("Please enter the amount of stocks prices you wish to have calculated: ");
scanf("%d", &j);
if (j > 0) {
ptr = (double*)malloc( j * sizeof(*ptr) );
for ( i = 0; i < j; ++i) {
printf("Input your stock price and press enter: ");
scanf("%lf", &ptr[i]);
if (min < ptr[i]) min = ptr[i];
if (max > ptr[i]) max = ptr[i];
}
printf("The average price entered was %f \n ", (average(ptr, j)) );
printf("The lowest price entered was: %f \n", min);
printf("The highest price entered was: %f \n", max);
free( ptr );
}
else printf("Sorry, you have entered an invalid number.\n");
system("pause");
return 0;
}
double average(double *ptr, int j)
{
int i; // for loop
double sum = 0; // sum of array
double avg; // avg of array
for ( i = 0; i < j; ++i) {
sum = sum + ptr[i];
}
avg = ( sum / ((double)j) );
return avg;
}