If, in addition to the other answers, you intend to write/call a function to find both the max and min values in your array of doubles, your first challenge is overcoming the axiom that only a single value (or pointer) can be returned by a function in C. There are several options available to determine the max and min in a function (or functions).
The first, and obvious, alternative is to write both a max and min function and call each. (but that would require iterating over your array twice -- that would be less than optimal). Your next option is to pass a separate array (of at least 2 double values) to store your max and min values in and either update the values or return a pointer to that array
(passing the array will automatically make values stored in the maxmin array available back in your calling function (main() in your case), but to immediately use the values in same call, you can return the pointer to the max/min array for convenience. You can also return NULL on error to indicate failure)
Or, since there are only 2 values needed, you can pass individual pointers to max and min as parameters and update the values pointed to by each within your function. Choosing some return type to indicate success/failure is also a good idea. int in this case is as efficient as any other (better from a numeric conversion standpoint)
With that in mind a simple maxmin function taking a pointer to your array of double values, pointers to each max and min and finally the number of values in the array could be written like:
/** find the max and min in array of 'n' doubles `ad`.
* update 'max' and 'min' pointers so values are available back
* in calling function.
*/
int maxmin_dbl (double *ad, double *max, double *min, size_t n)
{
if (!ad || !max || !min) return 0; /* validate parameters */
*max = (double)LLONG_MIN; /* initialize max/min to sufficiently */
*min = (double)LLONG_MAX; /* large negative/positive values. */
size_t i;
for (i = 0; i < n; i++) {
if (ad[i] > *max) *max = ad[i]; /* test for new max */
if (ad[i] < *min) *min = ad[i]; /* test for new min */
}
return 1;
}
(note: if your data values are outside the range of LLONG_MIN and LLONG_MAX, you will need to adjust your initializations accordingly)
A short example program reading values from the filename given as the first argument (or from stdin by default) and writing the array values and maximum and minimum to stdout could be written as:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
enum { MAXD = 10 }; /* constant for no. of values */
int maxmin_dbl (double *ad, double *max, double *min, size_t n);
int main (int argc, char **argv) {
size_t i, n;
double max, min, tmp, ad[MAXD] = {0.0};
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open for reading */
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
/* read double values from fp until at most 10 values read */
for (n = 0; n < MAXD && fscanf (fp, " %lf", &tmp) == 1; n++)
ad[n] = tmp;
if (fp != stdin) fclose (fp); /* close file if not stdin */
printf ("\narray values:\n\n"); /* output the values read */
for (i = 0; i < n; i++)
printf (" ad[%2zu] : %13.2lf\n", i, ad[i]);
if (maxmin_dbl (ad, &max, &min, n)) /* get max/min from array */
printf ("\n maximum : %.2lf\n minimum : %.2lf\n\n", max, min);
else {
fprintf (stderr, "error: maxmin_dbl failed.\n");
return 1;
}
return 0;
}
/** find the max and min in array of 'n' doubles `ad`.
* update 'max' and 'min' pointers so values are available back
* in calling function.
*/
int maxmin_dbl (double *ad, double *max, double *min, size_t n)
{
if (!ad || !max || !min) return 0; /* validate parameters */
*max = (double)LLONG_MIN; /* initialize max/min to sufficiently */
*min = (double)LLONG_MAX; /* large negative/positive values. */
size_t i;
for (i = 0; i < n; i++) {
if (ad[i] > *max) *max = ad[i]; /* test for new max */
if (ad[i] < *min) *min = ad[i]; /* test for new min */
}
return 1;
}
Example Use/Output
$ ./bin/arrdbl_maxmin <../dat/10int_nl.txt
array values:
ad[ 0] : 8572.00
ad[ 1] : -2213.00
ad[ 2] : 6434.00
ad[ 3] : 16330.00
ad[ 4] : 3034.00
ad[ 5] : 12346.00
ad[ 6] : 4855.00
ad[ 7] : 16985.00
ad[ 8] : 11250.00
ad[ 9] : 1495.00
maximum : 16985.00
minimum : -2213.00
Look over all the answers, those that directly address your error, and then let us know if you have any further questions.