Answer to Question #210186 in C++ for thanksforthehelp

Question #210186

Search a file of a list of double numbers then find the largest and smallest numbers. Append your results at the end of file and show your results on the output window. 

1
Expert's answer
2021-06-24T03:35:35-0400
#include <iostream>
#include <fstream>
using namespace std;
int main() {
	fstream my_file;
	double num[100];
	int i=0;
	my_file.open("text.txt");
	if (!my_file) {
		cout << "No such file";
	}
	else {
		double n;
        
		while (1) {
			my_file >> n;
			num[i]=n;
			if (my_file.eof())
				break;
			i++;
		}


	}
	double min=num[0];
	double max=num[0];
	for(int j=0;j<=i;j++){
	    if (num[j]<min)
	        min=num[j];
	}
	for(int j=0;j<=i;j++){
	    if (num[j]>max)
	        max=num[j];
	}
	cout<<"\nSmallest: "<<min;
	cout<<"\nLargest: "<<max;
	my_file.close();
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS