Write a program, maxmin.c, which finds the maximum and minimum of a set of
floats stored in the file samplesin.txt. You can assume that the number of values in the input file is less than 100.
Your main must use the following steps:
open the two files samplesin.txt and maxminout.txt.
read the values from samplesin.txt into the array x.
compute the maximum and minimum values found in the array x.
write the maximum and minimum values to the le maxminout.txt.
Your output file should look like :
There are 80 values in the array
The maximum value in the array is 63.605999
The minimum value in the array is 4.808900
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
int n = 0;
int num;
float arr[100];
ifstream File;
File.open("samplesin.txt");
ofstream File2;
File.open("maxminout.txt");
while(!File.eof())
{
File >> arr[n];
n++;
}
File.close();
int max=arr[0];
int min=arr[0];
for(int i=0;i<n;n++)
{
if(arr[i]>max){
max=arr[i];
}
if(arr[i]<min){
min=arr[i];
}
}
File2<<"\nThere are "<<n<<" values in the array";
File2<<"\nThe maximum value in the array is "<<max;
File2<<"\nThe minimum value in the array is "<<min;
File2.close();
cout << "done\n";
return 0;
}
Comments
Leave a comment