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

I need to refactor the code below to include reasonable functions. I can not jus

ID: 3670760 • Letter: I

Question

I need to refactor the code below to include reasonable functions. I can not just copy whole blocks of code into the functions. Look at what goes where, what is the best way to handle data, what is the most efficient functions call?

This my original code which I got some help from someone on Chegg. Can you help me refactor and include functions in thhe coode below?


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

using namespace std;
void printFileToScreen();

char grade(float marks){
    if(marks >=90)
        return 'A';

    else if(marks >=80)
        return 'B';

    else if(marks >=70)
        return 'C';

    else if(marks >=60)
        return 'D';

    else
        return 'F';
    }
int main()
{
//close the input file here
printFileToScreen();
cout<<"Data Saved Successfully!!!";
return 0;
}
void printFileToScreen()
{
char data[100];
int n;
float average;
float total = 0;
int k = 0;
float marks;
float totalAll = 0;
int a=0,b=0,c=0,d=0,f=0;


// open a file in write mode.
ofstream outfile;
outfile.open("class_statistics.txt");

cout << "Writing to the file" << endl;

cout << "Enter Teacher name: ";
cin.getline(data, 100);

// write inputted data into the file.
outfile <<"Teacher: "<<data << endl;
cout << "Enter Class name: ";
cin.getline(data, 100);
outfile <<"Class: "<<data << endl;
cout<<"Enter number of students: ";
cin>>n;
for(int i=1;i<=n;i++){
    k = 0;
    total = 0;
    cout<<"Enter name of "<<i<<" student: ";
    cin.ignore();
    cin.getline(data, 100);
    cout<<"Enter at most 10 grades of "<<i<<" student:[999 to stop] ";
    for(int j=0; j<10;j++){
        cin>>marks;
        if(marks == 999)
                break;
        total = total + marks;
        k++;
        }
        average = double(total)/k;

        totalAll = totalAll + average;
            char g = grade(average);
            if(g=='A')
                a++;
            else if(g=='B')
                b++;
            else if(g=='C')
                c++;
            else if(g=='D')
                d++;
            else
                f++;
            outfile <<"Student Name: "<<data<<" ";
            outfile <<"Average: "<<average<<" ";
            outfile <<"Grade: "<<g<<endl;
    }
    outfile <<"Student Count: "<<n<<endl;
    outfile <<"Student Average: "<<double(totalAll)/n<<endl;
    outfile <<"A's: "<<a<<endl;
    outfile <<"B's: "<<b<<endl;
    outfile <<"C's: "<<c<<endl;
    outfile <<"D's: "<<d<<endl;
    outfile <<"F's: "<<f<<endl;
    outfile.close();
}

Explanation / Answer

Please find the modularized code below :

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void printFileToScreen();
char grade(float marks){
if(marks >=90)
return 'A';
else if(marks >=80)
return 'B';
else if(marks >=70)
return 'C';
else if(marks >=60)
return 'D';
else
return 'F';
}
int main()
{
//close the input file here
printFileToScreen();
cout<<"Data Saved Successfully!!!";
return 0;
}

void printStudentDetails(char[] data,float average,char g){
outfile <<"Student Name: "<<data<<" ";
outfile <<"Average: "<<average<<" ";
outfile <<"Grade: "<<g<<endl;
return;
}

void outTofile(ofstream outfile,int n,float totalAll,int a,int b,int c,int d,int f){
outfile <<"Student Count: "<<n<<endl;
outfile <<"Student Average: "<<double(totalAll)/n<<endl;
outfile <<"A's: "<<a<<endl;
outfile <<"B's: "<<b<<endl;
outfile <<"C's: "<<c<<endl;
outfile <<"D's: "<<d<<endl;
outfile <<"F's: "<<f<<endl;
outfile.close();
return;
}

void printFileToScreen()
{
char data[100];
int n;
float average;
float total = 0;
int k = 0;
float marks;
float totalAll = 0;
int a=0,b=0,c=0,d=0,f=0;

// open a file in write mode.
ofstream outfile;
outfile.open("class_statistics.txt");
cout << "Writing to the file" << endl;
cout << "Enter Teacher name: ";
cin.getline(data, 100);
// write inputted data into the file.
outfile <<"Teacher: "<<data << endl;
cout << "Enter Class name: ";
cin.getline(data, 100);
outfile <<"Class: "<<data << endl;
cout<<"Enter number of students: ";
cin>>n;
for(int i=1;i<=n;i++){
k = 0;
total = 0;
cout<<"Enter name of "<<i<<" student: ";
cin.ignore();
cin.getline(data, 100);
cout<<"Enter at most 10 grades of "<<i<<" student:[999 to stop] ";
for(int j=0; j<10;j++){
cin>>marks;
if(marks == 999)
break;
total = total + marks;
k++;
}
average = double(total)/k;
totalAll = totalAll + average;
char g = grade(average);
if(g=='A')
a++;
else if(g=='B')
b++;
else if(g=='C')
c++;
else if(g=='D')
d++;
else
f++;
printStudentDetails(data,average,g):
}
  
outTofile(outfile, n, totalAll, a, b, c, d, f);

}