Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m in dire need of help with this assignment, i am to read a .txt file and com

ID: 673622 • Letter: I

Question

I'm in dire need of help with this assignment, i am to read a .txt file and compute the average of all integers on the first TWO lines and output the average as a floating point number. The .txt file is as follows:

55 67 458 23 81 33
782 375 528
405 324 950 46
14 864 551 38 167 518 630

We are using only while loops, and have NOT covered arrays and other advanced techiques. My Code is below, and any help would be greatly appreciated!!

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

void main() {

   int num, count, sum;
   char inchar;
   ifstream Filein;
   ofstream Fileout;
   Filein.open("F:\Datafile2.txt");
   Fileout.open("F:\ANSWERS.txt");
   //TaskF
   sum = count = 0;
   inchar = ' ';
   while (inchar != ' ') {
       while (count <= 2) {
           count++;
           Filein >> num;
           Filein.get(inchar);
           sum = sum + num;
       }
   }
   Filein.close();
   Filein.clear();
}

Explanation / Answer

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
int num, count, sum;

char inchar;
ifstream Filein;
ofstream Fileout;
Filein.open("C:\Users\MANIKANTA Reddy\Desktop\chegg\Datafile2.txt");
Fileout.open("C:\Users\MANIKANTA Reddy\Desktop\chegg\Answers.txt");
//TaskF
sum = count = 0;
int n=0;
if(Filein.is_open()){
  
while(count < 2){
count++;
inchar = ' ';
while (inchar!=' ') {


Filein >> num;
cout<<num<<endl;
Filein.get(inchar);
sum = sum + num;
n++;
}
}

}else{
cout<<"Unable to open file";
}
double average=(double)sum/(double)n;
Fileout<<"Number of numbers read: "<<n<<endl;
Fileout<<"Sum: "<<sum<<endl;
Fileout<<"Average: "<<average<<endl;
Filein.close();
Fileout.close();
Filein.clear();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote