SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Streams And File Io
Exercise:
Programming Projects
Question:8 | ISBN:9780132846813 | Edition: 5

Question

Write a program to compute numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in the following format: Each line contains a student’s last name, then one space, then the student’s first name, then one space, then ten quiz scores all on one line. The quiz scores are whole numbers and are separated by one space. Your program will take its input from this file and send its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number (of type double) at the end of each line. This number will be the average of the student’s ten quiz scores. Use at least one function that has file streams as all or some of its arguments.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string> 
using namespace std; 
  
void calculate(ifstream&, ofstream&); 
  
int main() 
{ 
    ifstream in; 
    ofstream out; 
  
    in.open("GradeBook1.txt"); 
  
    if (in.fail()) 
    { 
        cout << "Input file failed. Please try again or use another file./n"; 
        system("pause"); 
        return 1; 
    } 
  
    out.open("GradeBook2.txt"); 
    calculate(in, out); 
    out.close(); 
    in.close(); 
  
    cout << "Program has run successfully, grades averages have been written to GradeBook2.txt." << endl; 
  
    system("pause"); 
    return 0; 
} 
  
void calculate(ifstream& in, ofstream& out) 
  
{ 
    int i, sum, a[10]; 
    string first, last; 
    double avg; 
    in >> first; 
    while (in) 
  
    { 
        sum = 0; 
        in >> last; 
        for (i = 0; i<10; i++) 
        { 
            in >> a[i]; 
            sum += a[i]; 
        } 
  
        out << first << " " << last << " "; 
        for (i = 0; i<10; i++) 
            out << a[i] << " "; 
        avg = sum / 10.00; 
        out << std::setprecision(2) << std::fixed << avg << endl; 
        in >> first; 
    } 
} 

 

0 0

Discussions

Post the discussion to improve the above solution.