hi guys i have this assignment, and am totally confused am new to c++ and the book doesnt explain much ive written codes for part a,b but something felt off with the number and on part c i just have no idea what to do, can you guys help me?
i need the simplest codes for it. the program is :
reads integers from a file called "datafile2.txt" ,it has 20 integers in it and need to output to a file "answers.txt.
A.read the file and write all integers to the output file in one line
B.reread the file and compute the average of all integers
C.reread the file and compute the average of the first 12 integers
D.reread the file and compute the sum of every third integers
E.reread the file and determine the range of the integers in the file.
G.read the file and find the integers that is closest to 200
G.reared the file and compute the average of all integers on the first two line in the input file.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
int main()
{
int num;
ifstream infile;
ofstream outfile;
outfile.open("Answer.txt");
infile.open("DataFile2.txt");
infile >> num;
while (infile)
{
outfile << num << " ";
infile >> num;
}
infile.close();
infile.open("DataFile2.txt");
int sum = 0;
double avg;
while (infile)
{
infile >> num;
sum += num;
}
infile.close();
avg = sum / 20.00;
outfile << fixed << setprecision(3) << endl << "The average of all the numbers is : " << avg;
infile.open("DataFile2.txt");
int count = 1;
sum = 0;
while (count <= 12)
{
sum += num;
count++;
}
infile.close();
avg = sum / 12.00;
outfile << fixed << setprecision(3) << endl << "The average of the first 12 numbers is : " << avg;
}
Aucun commentaire:
Enregistrer un commentaire