My program needs to read double numbers from an input file and store them in an array. I've written the code and tested several functions using an array in the main program. Now I need to adapt the program to read from a data file. Any help would be greatly appreciated. Here's the code...
#include <stdio.h>
#define MAXEL 10
double find_max(double [], double);
double find_min(double [], double);
int get_min_range(double list[], double first, double last);
void sel_sort(double list[], int n);
int main(void){
double nums[] = {5.00, 3.00, 0.00, 9.00, 8.00, 1.00, 7.00, 4.00, 10.00, 2.00};
int i = 0;
double sum = 0;
sel_sort(nums, 10);
printf("\nThe descending order of the array is:\n\n");
while( i < 10){
printf("%6.2f\n", nums[i]);
i++;
}
printf("\nThe maximum value in the array is %6.2f. \n", find_max(nums, 10));
printf("\nThe minimum value in the array is %6.2f. \n\n", find_min(nums, 10));
return(0);
}
double find_max(double last[], double num_ele){
int i, max = last[0];
for(i = 1; i < num_ele; i++)
if(max < last[i]) max = last[i];
return (max);
}
double find_min(double first[], double num_ele){
int i, min = first[0];
for(i = 1; i < num_ele; i++)
if(min > first[i]) min = first[i];
return (min);
}
int get_min_range( double list[], double first, double last){
int i, small_sub;
small_sub = first;
for(i = first + 1; i <= last; ++i)
small_sub = i;
return(small_sub);
}
void sel_sort(double *list, int n){
int temp;
int i;
for(i = 0; i < n - 1; i++){
int max = i;
int j = 0;
for(j = i + 1; j < n; j++){
if(list[j] < list[max]) { max = j; }
}
temp = list[max];
list[max] = list[i];
list[i] = temp;
}
}