Write a program to read 10 integers from an input file and output the average, minimum, and maximum of those numbers to an output file. Take the name of the input file and output file from the user. Make sure to test for the conditions 1. if the count of numbers is less than 10 in the input file, then your average should reflect only for that many numbers 2. if the count of numbers is more than 10 in the input file, then your average should reflect only for ten numbers only.

Respuesta :

Answer:

#include <iostream>

#include <climits>

#include<fstream>

using namespace std;

int main ()

{

fstream filein;

ofstream fileout;

string inputfilename, outputfilename;

// ASk user to enter filenames of input and output file

cout<<"Enter file input name: ";

cin>>inputfilename;

cout<<"Enter file output name: ";

cin>>outputfilename;

// Open both file

filein.open(inputfilename);

fileout.open(outputfilename);

// Check if file exists [Output file will automatically be created if not exists]

if(!filein)

{

cout<<"File cannot be opened"<<endl;

return 0;

}

int min = INT_MAX;

int max = INT_MIN; //(Can't use 0 or any other value in case input file has negative numbers)

int count = 0; // To keep track of number of integers read from file

double average = 0;

int number;

// Read number from file

while(filein>>number)

{

// If min > number, set min = number

if (min>number)

{

min = number;

}

// If max < number. set max = number

if(max<number)

{

max = number;

}

// add number to average

average = average+number;

// add 1 to count

count+=1;

// If count reaches 10, break the loop

if(count==10)

{

break;

}

}

// Calculate average

average = average/count;

// Write result in output file

fileout<<"Max: "<<max<<"\nMin: "<<min<<"\nAverage: "<<average;

// Close both files

filein.close();

fileout.close();

return 0;

}