"C" finding MIN and MAX from .txt file?
Hey guys,
I just got assigned a project in my C programming class. The objective is to find the min and max number out of a .txt file full of numbers on separate lines called "data.txt". I am pretty much stuck at this point. I don't know how to actually scan the file and print the min and max. If anyone could lend a helping hand, I would really appreciate it!
Also, the list of numbers in the data.txt file contains hundred on hundreds of numbers. So I couldn't specify the exact amount of numbers listed in the file. I must also find the count number, sum, and the average of the given data, thanks. :cool:
So far I have:
Code:
#include<stdio.h>
int main()
{
int counter=0;
int maximum=0, minimum=1;
int num_data_points, avg;
double max, min;
FILE *sensor;
sensor = fopen("data.txt", "r");
if (sensor == NULL)
{
printf("Unable to read/write file.\n");
}
else
{
fscanf(sensor, "%d", &num_data_points);
for(counter=0; counter<num_data_points; counter++)
{
fscanf(sensor, "%lf", max);
fscanf(sensor, "%lf", min);
}
printf("Maximum = %lf\nMinimum = %lf\n", max, min);
}
return 0;
}