C++ Program: Write a program that takes its input from a file of numbers of type
ID: 3816294 • Letter: C
Question
C++ Program: Write a program that takes its input from a file of numbers of type double, ouputs the largest number found in the file and the average of the numbers in the file to a file. The file contains nothing but numbers of type double seperates by blanks and/or line breaks. You will need to create one or more test files in to test your program. Allow the user to repeat the operation.
Here's what I have so far and I'm stuck:
/* This program takes its input from a file of numbers of type double and outputs:
- the largest number found in the file
- the average of the numbers in the file to a file
The file contains nothing but numbers of type double deperated by line breaks.
Contains one or more test files to test the program & allows uer to repeat the operation. */
#include <iostream>
#include <fstream> //for i/o stream
#include <iomanip> //for setprecision
using namespace std;
//main function
int main()
{
ifstream in_stream;
ofstream out_stream;
//variable declaration
char filename[16]; //filename contains 15 or fewer characters
int num; //integers in the file
int num_of_int; //number of integers in the file
double largest_int = 0; //holds the largest number
double avg_of_int = 0; //holds the integer of all the integers
//get the filename from the user
cout << "Enter the filename: ";
cin >> filename;
//verify if the file exists
in_stream.open("infile.txt");
if (in_stream.fail())
{
cout << "Input file opening failed. ";
exit(1);
}
out_stream.open("outfile.txt");
if (out_stream.fail())
{
cout << "Output file opening failed. ";
}
//close the files
in_stream.close();
out_stream.close();
}
Explanation / Answer
#include <iostream>
#include <fstream> //for i/o stream
#include <iomanip>
#include<stdio.h>
#include<stdlib.h> //for setprecision
using namespace std;
//main function
int main()
{
ifstream in_stream;
ofstream out_stream;
//variable declaration
char filename[16]; //filename contains 15 or fewer characters
int num; //integers in the file
int num_of_int; //number of integers in the file
double largest_int = 0; //holds the largest number
double avg_of_int = 0; //holds the integer of all the integers
//get the filename from the user
cout <<"Enter the filename: ";
cin >> filename;
//verify if the file exists
in_stream.open(filename);
if (in_stream.fail())
{
cout << "Input file opening failed. ";
exit(1);
}
out_stream.open("outfile.txt");
if (out_stream.fail())
{
cout << "Output file opening failed. ";
}
double sum=0;
double d;
int counter=0;
while(!in_stream.eof())
{
in_stream>>d;
sum+=d;
if(d>largest_int)
largest_int=d;
counter++;
}
out_stream<<"The Largest number in the file is "<<largest_int;
out_stream<<" The average of all the numbers in the file is "<<sum/counter;
out_stream<<"nEnd program.";
//close the files
in_stream.close();
out_stream.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.