Write a program which opens an input text file containing the last names and thr
ID: 3629677 • Letter: W
Question
Write a program which opens an input text file containing the last names and three exam scores for three students. The format of the file is,Contents of lab06-in.txt
lname1 e11 e12 e13
lname2 e21 e22 e23
lname3 e31 e32 e33
For example, the file might contain the following information,
Contents of lab06-in.txt
Simpson 100 100 100
Jetson 81 73 91
Flintstone 34 51 67
The program shall read the last name and three exam scores for each student and calculate the student's exam average which is the sum of the three exam scores divided by 3. Then, the program shall output a table to a new text file (named lab06-out.txt) formatted as follows,
Contents of lab06-out.txt
Student Exam1 Exam2 Exam3 Avg
----------------------------------------------------
Simpson 100% 100% 100% 100.0%
Jetson 81% 73% 91% 81.7%
Flintstone 34% 51% 67% 50.7%
The last name is to be printed left-justified in a field of width 18, followed by 1 space, followed by the first exam score output right justified in a field of width 3, followed by a percent sign, followed by 5 spaces, followed by the second exam score output right justified in a field of width 3, followed by a percent sign, followed by 5 spaces, followed by the third exam score output right-justified in a field of width 3, followed by a percent sign, followed by 5 spaces, followed by the exam average output right-justified in a field of width 5 with 1 digit after the decimal point, followed by a percent sign, followed by a newline.
IMPLEMENT THE FOLLOWING PSEUDO-CODE :
Include necessary header files
Define global variable G_fin of the class ifstream
Define global variable G_fout of the class ofstream
Function OpenInputFile ( ) Returns Nothing
Call G_fin.open ( "lab06-in.txt" )
End Function OpenInputFile
Function CloseInputFile ( ) Returns Nothing
Call G_fin.close ( )
End Function OpenInputFile
Function OpenOutputFile ( ) Returns Nothing
Call G_fout.open ( "lab06-out.txt" )
End Function OpenOutputFile
Function CloseOutputFile ( ) Returns Nothing
Call G_fout.close ( )
End Function OpenOutputFile
Function ReadStudentData ( Output: lname is String, e1 is Int, e2 is Int, e3 is Int ) Returns Double -- Note: pass-by-reference
Read string from G_fin into lname using >> operator
Read ints from G_fin into e1, e2, and e3 using >> operator
Return ( e1 + e2 + e3 ) / 3
End Function ReadStudentData
Function PrintHeader ( ) Returns nothing
Send "Student..." string to G_fout using << operator
Send "--- ··· ---" string to G_fout using << operator
End Function PrintHeader
Function PrintStudentExam ( Input: e is Int ) Returns Nothing
Configure G_fout for right-justification using right manipulator
Send e to G_fout in a field of width 3 using the setw(3) manipulator
Send a percent sign followed by 5 spaces to G_fout
End Function PrintStudentExam
Function PrintStudentData ( Input: lname is String, e1 is Int, e2 is Int, e3 is Int, avg is Double ) Returns Nothing
Configure G_fout for left-justification using left manipulator
Send lname to G_fout formatted in a field of width 18 followed by a space
Call PrintStudentExam ( e1 )
Call PrintStudentExam ( e2 )
Call PrintStudentExam ( e3 )
Configure G_fout for right-justification using right manipulator
Send avg to G_fout formatted in a field of width 5 followed by '%' followed by ' ' followed by newline
End Function PrintStudentData
Function run ( ) Returns Int
Define failed as Int and initialize to 0
Define lname1, lname2, lname3 as String
Define e11, e12, e13, e21, e22, e23, e31, e32, e33 as Int
Define avg1, avg2, avg3 as Double
Call OpenInputFile ( )
avg1 ? Call ReadStudentData ( lname1, e11, e12, e13) -- Note: passed by reference
avg2 ? Call ReadStudentData ( lname2, e21, e22, e23) -- Note: passed by reference
avg3 ? Call ReadStudentData ( lname3, e31, e32, e33) -- Note: passed by reference
Call CloseInputFile ( )
Call OpenOutputFile ( )
Configure G_fout so real numbers are printed with 1 digit after the decimal point in fixed notation
Call PrintHeader ( )
Call PrintStudentData ( lname1, e11, e12, e13, avg1 )
Call PrintStudentData ( lname2, e21, e22, e23, avg2 )
Call PrintStudentData ( lname3, e31, e32, e33, avg3 )
Return failed
End Function main
Function main ( )
Return run ( )
End Function main
PLEASE GIVE AN ANSWER THAT FOLLOWS THE PSEUDOCODE AND DO NOT USE LOOPS.
Explanation / Answer
please rate - thanks
I don't understand what run is for. it appears to be in main??
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
using namespace std;
ifstream G_fin;
ofstream G_fout ;
void OpenInputFile ();
void CloseInputFile ();
void OpenOutputFile ( );
void CloseOutputFile ( );
double ReadStudentData(string&,int&,int&,int&);
void PrintHeader();
void PrintStudentExam(int);
void PrintStudentData(string,int,int,int, double);
int run();
int main()
{int failed=0;
string lname1,lname2,lname3;
int e11,e12,e13,e21,e22,e23,e31,e32,e33;
double avg1,avg2,avg3;
OpenInputFile ();
avg1= ReadStudentData ( lname1, e11, e12, e13) ;
avg2= ReadStudentData ( lname2, e21, e22, e23);
avg3=ReadStudentData ( lname3, e31, e32, e33);
CloseInputFile ( );
OpenOutputFile ( );
G_fout.precision(1);
PrintHeader ( );
PrintStudentData ( lname1, e11, e12, e13, avg1 );
PrintStudentData ( lname2, e21, e22, e23, avg2 );
PrintStudentData ( lname3, e31, e32, e33, avg3 );
return failed;
}
int run()
{
}
void PrintStudentData(string lname,int e1,int e2,int e3, double avg)
{G_fout<<left<<setw(18)<<lname<<" ";
PrintStudentExam ( e1 );
PrintStudentExam ( e2 );
PrintStudentExam ( e3 );
G_fout<<right<<setw(5)<<fixed<<avg<<"% ";
}
void PrintStudentExam(int e)
{G_fout<<right<<setw(3)<<e<<"% ";
}
void PrintHeader()
{G_fout<<"Student Exam1 Exam2 Exam3 Avg ";
G_fout<<"---------------------------------------------------- ";
}
double ReadStudentData(string& lname,int& e1,int& e2,int& e3)
{G_fin>>lname;
G_fin>>e1;
G_fin>>e2;
G_fin>>e3;
return (e1+e2+e3)/3.;
}
void CloseOutputFile ()
{G_fout.close ( );
}
void OpenOutputFile ()
{G_fout.open ( "lab06-out.txt" );
}
void CloseInputFile ()
{G_fin.close ( );
}
void OpenInputFile ()
{G_fin.open ( "lab06-in.txt" );
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.