In: Computer Science
Your lab instructor will guide you through the steps necessary to set up this project in Visual Studio. Video instructions for people using other programs can be found here: XCode, Code::Blocks and repl.it.
Given below is the code for the question. Please copy input.txt
also. The program creates output.txt..
Please do rate the answer if it helped. Thank you.
input.txt
--------
14.1 58.7 14.1 52.9 39.0
54.1 79.0 69.0 99.1 29.1
main.cpp
-------
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile("input.txt");
float sum = 0, avg = 0, num;
int n = 0;
if(!infile){
cout << "Error: Could not
open input.txt" << endl;
return 0;
}
while(infile >> num){
sum += num;
n++;
}
infile.close();
avg = sum / n;
ofstream outfile("output.txt");
outfile << "Sum = " << sum <<
endl;
outfile << "Average = " << avg <<
endl;
outfile.close();
cout << "output.txt created" <<
endl;
return 0;
}
output.txt
==========
Sum = 509.1
Average = 50.91