In C, create a program that displays the minimum and maximum values stored in a data...

Free

60.1K

Verified Solution

Question

Programming

In C, create a program that displays the minimum and maximumvalues stored in a data file \"datafile.txt\". Use the followingfunction prototype:  void minmaxarray(float value, float*min, float *max);

Answer & Explanation Solved by verified expert
4.2 Ratings (508 Votes)

#include
#include
#include

//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 *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


Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students