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

Your program will prompt the user for the name of the input file and read the da

ID: 3631908 • Letter: Y

Question

Your program will prompt the user for the name of the input file and
read the data from it. Your program will generate a report showing the
ID#, the best two scores, the average of the best two scores and letter grade
corresponding to the average. The report will be stored in an output text
file whose name the user will be prompted to enter.
A modular implementation must be designed using functions. In addition
to the main function, your program will consist of the following user-defined
functions; you may design more:
/**
* sorts three numbers in ascending order. After
* this function is executed, smallest will be the
* smallest number, median, the median value, and
* largest the largest.
* @param smallest a non-negative fixed-point value
* @param median a non-negative fixed-point value
* @param largest a non-negative fixed-point value
*/
void sort3(double &smallest, double &median, double &largest)
/**
* computes the average of two numbers
* @param num1 a non-negative number
* @param num2 a non-negative number
* @return the average of num1 and num2
*/
double averageOf2(double num1, double num2)
/**
* computes the corresponding letter grades for a number
* @param score a non-negative number less than or equals to 100
* @return A <- 90<=score<=100; B <- 80<=score<90; C <- 70<=score<80;
* D <- 60<=score<70; F <- 0<=score<60.
*/
char calcGrade(double score)
All other tasks not performed by a sub-function should be performed by the
main functions: these tasks include opening the input and output files as
well as file input/output operations.
Your program should prompt the user for the name of the input and out-
put files. Be sure to perform error-check on both files to determine whether
the input file is successfully opened and whether the output file is successfully
created. Your program should then generate a report following the format
below. Ensure that numeric values in the report are formatted so that they have the same number of decimal places as shown in the sample output be-
low. Once the data from the input file have been processed, close the file.
Also, close the output file before the program terminates.
For a sample input file, ence7010f11.grd, with the data below:
2008-EN-001 95 80 100
2009-NE-095 60 98 97.5
2011-PA-158 80 70 50
A typical program interaction would be:
Enter the name of the data file> ence7010f11.grd
Enter the name of the output file> ence7010f11.rpt
Here is the contents of the sample output file (ence7010f11.rpt):
Encephalopathy (Med-7010)
Final Course Report
Fall 2011
================================================
ID# Score Score Average Grade
2008-EN-001 95.00% 100.00% 97.500% A
2009-NE-095 97.50% 98.00% 97.750% A
2011-PA-158 70.00% 80.00% 75.000% C
------------------------------------------------
*** END OF REPORT ***
================================================

Here's what I have come up with so far...I haven't worked on the format of my cout as much, but I feel like I'm close with my functions and calling them.

void sort3(double &smallest, double &median, double &largest)
{
if(largest<=smallest && smallest <= median)
{
double temp = largest;
largest = smallest;
smallest = median;
median = temp;
}
else if(largest<=median && median<= smallest)
{
double temp= largest;
largest = median;
median = smallest;
smallest = temp;
}
else if(median<=largest && largest<=smallest)
{
double temp = median;
median = smallest;
smallest = largest;
largest = temp;
}
else if(median<=smallest && smallest <= largest)
{
double temp = median;
median = smallest;
smallest = temp;
}
else if(smallest<=largest && largest<=median)
{
double temp = largest;
largest = median;
median = temp;
}

}

double averageOf(double num1, double num2)
{
return ((num1 + num2)/2.0);
}


char calcGrade(double score)
{

if(score<=100 && score>=90)
{
return 'A';
}
else if(score<90 && score>=80)
{
return 'B';
}
else if(score<80 && score>=70)
{
return 'C';
}
else if(score<70 && score>=60)
{
return 'D';
}
else
{
return 'F';
}
}

int main()

{
fstream in;
string filename;
cout<<"What is input filename? "<<endl;
cout<<"What is the name of the file you wish to open."<<endl;
cin>>filename;

in.open(filename.c_str(),ios::in);
if(in.fail())
{
cout<<"Unable to open "<<filename<<"."<<endl;
exit(1);
}
string studentID;
double score1, score2, score3, num1, num2;

cout<<" Encephalopthy (MED-7010)"<<endl;
cout<<" Final Course Report "<<endl;
cout<<" FALL 2011 "<<endl;
cout<<"========================================="<<endl;
cout<<"ID# Score Score Average Grade"<<endl;
in>>studentID;

while(!in.eof())
{
double median, largest, smallest;
in>>score1>>score2>>score3;
sort3(score1,score2,score3);
double average = averageOf(score2, score3);
char grade = calcGrade(average);
cout<<studentID<<" "<<score2<<" "<<score3<<" "<<average<<" "<<grade<<endl;
in>>studentID;
}
cout<<"-------------------------------------------"<<endl;
in.close();
return 0;
}

Please explain the void3 function if possible. I understand what it should do, however can not get it to operate properly

Explanation / Answer

please rate - thanks

I did the sort differently --see the comments

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void sort3(double &num1, double &num2, double &num3)
{double small,medium,large;
if(num1<num2)
   {if(num1<num3)
        {small=num1;        //num1 is smallest
        if(num2<num3)     //sort num2 & num3
                {medium=num2;   //num3 is larger
                large=num3;
                }
         else
                {medium=num3;    //num2 is larger
                large=num2;
                }
            }
       else              //num3 is smallest
             {small=num3;
            medium=num1; //you already know num1<num2
            large=num2;
            }
        }
    else               //num2 or num3 smallest
        if(num2<num3) //check
             {small=num2;   //num2 smallest
               if(num1<num3)   //now sort num1 & num3
                    {medium=num1; //num3 is larger
                    large=num3;
                    }
                else
                    {medium=num3;   //num1 is larger
                    large=num1;
                    }
                }
            else
                {small=num3;    //only thing left since you know num2<num1
                medium=num2;
                large=num1;
                }
num1=small;        //put them in the appropriate paremeter
num2=medium;
num3=large;
}

double averageOf(double num1, double num2)
{
return ((num1 + num2)/2.0);
}


char calcGrade(double score)
{

if(score<=100 && score>=90)
{
return 'A';
}
else if(score<90 && score>=80)
{
return 'B';
}
else if(score<80 && score>=70)
{
return 'C';
}
else if(score<70 && score>=60)
{
return 'D';
}
else
{
return 'F';
}
}

int main()

{
ifstream in;
ofstream out;
string filename;
cout<<"What is input filename? "<<endl;
cout<<"What is the name of the file you wish to open."<<endl;
cin>>filename;

in.open(filename.c_str(),ios::in);
if(in.fail())
{
cout<<"Unable to open "<<filename<<"."<<endl;
exit(1);
}

string studentID;
double score1, score2, score3, num1, num2;
cout<<"What is the name of the output file you wish to open."<<endl;
cin>>filename;
out.open(filename.c_str());
out<<" Encephalopthy (MED-7010)"<<endl;
out<<" Final Course Report "<<endl;
out<<" FALL 2011 "<<endl;
out<<"========================================="<<endl;
out<<"ID# Score Score Average Grade"<<endl;
in>>studentID;

while(!in.eof())
{
double median, largest, smallest;
in>>score1>>score2>>score3;
sort3(score1,score2,score3);
double average = averageOf(score2, score3);
char grade = calcGrade(average);
out<<setprecision(2)<<fixed;
out<<studentID<<" "<<score2<<" "<<score3<<" "<<average<<" "<<grade<<endl;
in>>studentID;
}
out<<"--------------------------------------------------------"<<endl;
out<<"***END OF REPORT*** ";
out<<"========================================="<<endl;
in.close();
return 0;
}