Answer to Question #210185 in C++ for yolonde

Question #210185

Write a program to input a list of numbers (type double) from a file and outputs the numbers and the average into another text file. Also show me the output on the screen. The file contains nothing but the numbers of type double separated by blanks and/or line breaks.

1
Expert's answer
2021-06-24T03:35:31-0400
#include <iostream> 
#include <fstream> 


using namespace std; 


int main() 
{ 
	double n; 
	ifstream in;  
	ofstream out; 
	
	in.open("text1.txt");  
	
	if(in.fail( ))
	{ 
		cout << "Error opening input file"<<endl; 
		return 1;
	}
	
	out.open("text2.txt"); 
	
	if(out.fail( )) 
	{	
		cout << "Error opening output file"<<endl; 
		return 0; 
	} 


	double sum=0;
	int count=0;


	while(!in.eof())
	{
		in>>n;
		sum=sum+n;
		count++;
	}


	double avg=0;


	avg=sum/double (count); 
	cout<<"sum "<<sum<<" count "<<count<<" average "<<avg;
	out<<avg;
	
	in.close();	 
	out.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