Problem: Imagine a course in which each student\'s final exam counts for 40% of
ID: 3623657 • Letter: P
Question
Problem: Imagine a course in which each student's final exam counts for 40% of the final grade, the midterm exam counts for 20%, and the 5 homework grade makes up the remaining 40%. The full credit of each exam and homework is 100.
The first version: Let's put everything in the main function including getting inputs (the name, mid-term, final exam, and homework scores) and writing the output. The output must have the precision of 2, which means the number of significant digits displayed is 2. For example, 41.235 will be displayed as 41. You need to set the precision of 2 for your output and set to the original precision after the output. In this version, you don't have to keep the students records as long as you can print out the final score. You can repeat this process until no name is entered.
a. setprecision (int precision) function is std namespace sets the precision.
b. cout.precision gets the current precision of cout.
The second version:
a.You need to save the students' records in this version. First define a structure using: typedef struct { ...} StudentRecord;
b.Define an array of StudentRecord. You can set the array size.
c. Make two functions. The first function takes care of input (getting one student record) and the second one writes the output. The signature of two functions are:
i. bool GetInput(istream& is, StudentRecord& sr). This function returns true when a record is successfully entered. Otherwise, return false. You can make your own way to terminate the input. One way is to put “end” for the name.
ii. void WriteOutput (ostream& os, StudentRecord& sr). In this function, you calculate the total score, and prints out necessary information. You can decorate your output as you want.
d. In the main function, you need to call GetInput and WriteOutput properly. You can pass cin and cout as the input stream and the output stream. You can pass an array item for the student record. What is & in the function? Just use it now.
e. You need to include appropriate headers.
The final version:
a.You are going to implement file input and file output. You can use ifstream and ofstream.
b. First you ask whether there is an input file. If there is not, use your previous input mechanism to get students’ records. At the end of the program, you need to save students’ records in the file using ofstream. Basic idea of ofstream is same as cout. The only difference is cout outputs to the console and ofstream outputs to the file.
c. If there is an input file, using ifstream, get the students’ records from the file. Think of when you can stop reading from the file.
d. You also need to make a function that returns a grade (char) with the final scored. The grade should be included in the output.
e. When you make your own data file, consider putting some header information to verift it’s your data file. For example, you can put “Student Record Test 1.0” at the beginning of the file. Then, whenever you read a data file, you can verify it with the string.
f. In this version, before the students’ records, put the number of students. If you have the number of students at the beginning of the file, instead of allocating unnecessary memory space, you can allocate as much as you need with new operator and a pointer. Don’t forget to delete the pointer when you are done.
Explanation / Answer
please rate - thanks
it's not quite ready, put I'm almost out of time.
will message it to you when I'm done
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
#define MAX_STUDENTS 10
typedef struct {
string name;
int midterm;
int final;
int homework[5];
} StudentRecord;
bool correctFile(istream& is)
{string buffer;
getline(is,buffer);
if(buffer.compare("Student Record Test 1.0")==0)
return true;
return false;
}
bool GetInput (istream& is, StudentRecord& sr,bool kb)
{if(kb)
cout << "Please enter your name:(xtrl^z to exit)" << endl;
is >> sr.name;
if(!is)
return false;
if(kb)
cout <<"Please enter the midterm grade and the final scores." << endl;
is >> sr.midterm >> sr.final;
if(kb)
cout<<"Please enter your homework scores:" << endl;
for (int i=0; i<5; i++)
is >> sr.homework [i];
return true;
}
char calgrade(double t)
{
if(t>=90)
return 'A';
if(t>=80)
return 'B';
if(t>=70)
return 'C';
if(t>=60)
return 'D';
return 'F';
}
void WriteOutput (ostream& os, StudentRecord& sr)
{int sum = 0,j;
char letter;
double total;
{for (j=0; j<5; j++)
sum += sr.homework[j];
}
total=sr.final*.4+sr.midterm*.2+sum/5.*.4;
letter=calgrade(total);
os<<sr.name<<" "<<setprecision(2)<<sr.midterm<<" "<<sr.final<<" ";
for(j=0;j<5;j++)
os<<sr.homework[j]<<" "<<letter<<endl;
}
int main ()
{char f;
bool file ;
char filename[80];
int i,j,n;
string name;
double midterm, final, homework [5];
StudentRecord* studentRecord;
ifstream in;
ofstream out;
file=false;
cout<<"Where is the data? ";
cout<<"F - file K - keyboard ";
cin>>f;
if(toupper(f)=='F')
{cout<<"Enter input file name: ";
cin>>filename;
in.open(filename);
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
file=true;
}
cout<<"Enter output file name: ";
cin>>filename;
out.open(filename);
if(file)
{if(!correctFile(in))
{ cout<<"input file header is incorrect check it ";
system("pause");
return 1;
}
in>>n;
}
else
n=MAX_STUDENTS;
studentRecord=new StudentRecord[n];
n=0;
if(file)
while(GetInput(in,studentRecord[i],false))
{n++;
}
else
while(GetInput(cin,studentRecord[i],true))
{n++;
}
for(i=0;i<5;i++)
WriteOutput (out,studentRecord[i]);
cout << "Enter any number to finish the program." << endl;
cin>> n;
delete [] studentRecord;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.