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

Problem: Complete Programming Project 9 on page 231 of your textbook. Your progr

ID: 3620927 • Letter: P

Question

Problem: Complete Programming Project 9 on page 231 of your textbook. Your program should read in data from a text file. All entries on one line and are separated by one space. The 10 quiz scores are integers, and each line of the text file is formatted as such:

lastName firstName 01 002 03 004 005 006 07 008 09 010

The scores may be 1, 2 or 3 digits. The output is to be in a separate file, identical to the input file, except that there is one additional field, a double, at the end of each line. This number is to be the average of the 10 scores. Your program should also check that the input file exists before attempting to read from it. Additionally, you do NOT have to use functions, as indicated in the project description in the book. You may create all the program parts inside of main().

In the File the Data is as so :
Price Betty 40 50 60 70 60 50 40 30 60 90
Goodman James 60 70 80 90 50 60 90 90 100 90
Smith Charles 70 80 90 60 70 60 80 90 90 90
Jones Warren 70 70 80 90 70 80 90 80 70 60

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

int main()
{
    string line; //this will contain the data read from the file
int pos1 = 0, pos2=0;
int count =0;
double total =0.0;
double average = 0.0;
ifstream myfile1 ("D:\input.dat"); //opening the file.
ofstream myfile2("D:\output.dat");
if (myfile1.is_open()) //if the file is open
{
    while (! myfile1.eof() ) //while the end of file is NOT reached
    {
      getline (myfile1,line); //get one line from the file
    for ( int i = 1; i<=11;++i)
    {
        pos2 = line.find(' ',pos1);
        string s = line.substr(pos1,pos2);
        if ( i <=2) pos1 = pos2+1;
        else {
            total = total + atoi(s.c_str());
            pos1 = pos2+1;
        }
    }
    string s = line.substr(pos1);
    total = total + atoi(s.c_str());
    average = total / 10.0;
    myfile2<<line<<" "<<average<<endl;
    }
}
myfile1.close();
myfile2.close();
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