In: Computer Science
In C, create a program that displays the minimum and maximum values stored in a data file "datafile.txt". Use the following function prototype: void minmaxarray(float value, float *min, float *max);
#include<stdio.h>
#include<string.h>
#include <ctype.h>
//crate a file with number
void createFile()
{
FILE *fp;
char ch;
int i=0;
char str[100] = "14\n15\n20\n53\n50\n70\n17\n34\n45\n12";
fp = fopen("datafile.txt", "w");
fputs(str, fp);
fclose(fp);
}
//function to read the values from a file and put into
array
void readFile(float array[])
{
//declaration of file type pointer
FILE *fp;
//open file in read mode
fp = fopen("datafile.txt", "r");
float num;
int i=0;
while (!feof (fp))
{
fscanf (fp, "%f", &num);
array[i] = num;
i++;
}
//close file
fclose(fp);
}
//function to find the minmum and maximum value
void minmaxarray(float value[], float *min, float *max)
{
*min = value[0];
*max = value[0];
for(int i=1; i<10; i++)
{
if(*min>value[i])
*min = value[i];
if(*max<value[i])
*max = value[i];
i++;
}
}
int main()
{
//array declaration
float array[10];
float min, max;
createFile();
//function calling
readFile(array);
minmaxarray(array, &min, &max);
//display the result
printf("Minimum = %.2f", min);
printf("\nMaximum = %.2f", max);
return 0;
}
OUTPUT:
datafile.txt