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

home / study / questions and answers / engineering / computer science / the hist

ID: 663716 • Letter: H

Question

home / study / questions and answers / engineering / computer science / the history teacher at your school needs help in ...

Question: The history teacher at your school needs help in grading a True/False test.
The students IDs and test answers are stored in a file. The first entry in the
file contains answers to the test in the form:
TFFTFFTTTTFFTFTFTFTT
Every other entry in the file is the student ID, followed by a blank, followed
by the students responses. For example, the entry:
ABC54301 TFTFTFTT TFTFTFFTTFT
indicates that the student ID is ABC54301 and the answer to question 1 is
True, the answer to question 2 is False, and so on. This student did not
answer question 9. The exam has 20 questions, and the class has more than
150 students. Each correct answer is awarded two points, each wrong answer
gets one point deducted, and no answer gets zero points. Write a program
that processes the test data. The output should be the students ID, followed
by the answers, followed by the test score, followed by the test grade.
Assume the following grade scale: 90%100%, A; 80%89.99%, B;
70%79.99%, C; 60%69.99%, D; and 0%59.99%, F.

Use the file in this folder as input but assume that the file name can change so prompt the user.

The file format is different than what is indicated in the book so adapt your code accordingly.

Assume that any answer that is not "T" or "t" or "F" or "f" is invalid and considered wrong. As indicated in the book, treat spaces as no answer.

Use a "switch" statement when determining the grade.

Only display the number of records processed to the console, instead save the output indicated in the book to a file named "FinalGrades.txt".

my program is as follows but I get an error reading file:

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

//function prototypes

void readDataFromFile(ifstream &, char[], string[], string[]);

void calculateScores(char[], string[], double[]);

void calculateGrades(double[], char[]);

//global declarations for constants

const int MAX_ANSWERS = 20;

const int MAX_STUDENTS = 200;

const double MAX_MARKS = 40.0;

//global declarations

int studentsCounter = 0;

char testAnswers[MAX_ANSWERS];

string studentID[MAX_STUDENTS];

string studentAnswers[MAX_STUDENTS];

double studentScores[MAX_STUDENTS];

char studentGrades[MAX_STUDENTS];

//start main function

int main()

{

//variables declaration

ifstream inFile;

string fileName;

//Prompt the user to enter the name of a file and open the file and display an error message if the fileis not found.

//prompt the user to enter the name of a file

cout << "Enter the file name";

cin >> fileName;

/*open the file and display an error message if the file not found*/

inFile.open("FinalGrades.txt");

if (inFile)

{

/*display an error message if the file not found and exit the program*/

cout <<"FinalGrades.txt"<< fileName << "is not found." << endl;

system("pause");

return 1;

}//end if

//Call readDataFromFIle, calculateScores, and calculateGrades functions to read data and calculate score and grade of students.

//call readDataFromFile function

readDataFromFile(inFile, testAnswers, studentID, studentAnswers);

//call calculateScores function

calculateScores(testAnswers, studentAnswers, studentScores);

//call calculateGrades function

calculateGrades(studentScores, studentGrades);

//Display the details of each student as ID, answers, score, and grade respectively.

//display the results

cout<<"Results of the students."<<endl;

cout << "Student_ID Student_Answers Score Grade" << endl;

cout << "-------------------------" << endl;

for(int student = 0; student<studentsCounter; student++)

{

cout << studentID[student] << " "

<< studentAnswers[student] << " "

<< studentScores[student] << " "

<< studentGrades[student] << endl;

}//end for

//close the input file

inFile.close();

//pause the system for a while

system("pause");

return 0;

}//end of main function

Explanation / Answer

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>


#define MAXANSWERS 20
#define MAXSTUDENTS 200
#define MAXMARKS 40


void readIPFile(char *ipFile, char[20], char[10], char[30]);
void GetScores(char[20], char[20], double[]);
void GetGrades(double[], char[20]);

//using namespace std;
const int MYSIZE=50,NUMBROFTEST=4;
double detectExamAverage(int grades[][NUMBROFTEST], int classsize);
void detectTopScore(int grades[][NUMBROFTEST], int classsize);
void calculateStudentAverage(int grades[][NUMBROFTEST], int classsize);
void storeMarks(int grades[][NUMBROFTEST], int classsize);
int main()
{

char * fileName;
FILE *ipFilePtr;

cout << "Please input name of the file: ";
cin >> fileName;
ipFilePtr = fopen(fileName, "r");


    int grades[MYSIZE][NUMBROFTEST],classsize,stnum,test;
    double exavg[NUMBROFTEST],classavg,sum=0;
    cout<<"How many students in class?"<<endl;
    cin>>classsize;
    //Read in data
    cout<<"Please enter studnts' grades"<<endl;
    for(stnum=0;stnum<classsize;stnum++)
     {
       for(test=0;test<=NUMBROFTEST;test++)
   cin>>grades[stnum][test];
     }
     //Printing test average
     for(test=0;test<NUMBROFTEST;test++)
       cout<<"test number "<<test<<"had an average of "<<detectExamAverage(grades,classsize)<<endl;

     //Printing Students' individiual average
     calculateStudentAverage(grades,classsize);
     for(test=0; test<NUMBROFTEST;test++)
      {
       exavg[test]=detectExamAverage(grades,classsize);
       sum+=exavg[test];
      }

      classavg=(double)sum/NUMBROFTEST;
      cout<<"Overall class average is "<<classavg<<endl;
    return 0;
}
// Find and print each exam's average and return to calling function
double detectExamAverage(int grades[][NUMBROFTEST], int classsize)
{
    int sum=0,stnum,test;
     double exavg;

    for(test=0;test<NUMBROFTEST;test++)
     {
   sum=0;
   for(stnum=0;stnum<classsize;stnum++)
        sum+=grades[stnum][test];
   exavg=(double)sum/classsize;

     }
   return exavg;

}
// Find and print each studet's average
void calculateStudentAverage(int grades[][NUMBROFTEST], int classsize)
{
    int sum=0,stnum,test;
     double stavg;

     for(stnum=0;stnum<classsize;stnum++)
     {
   sum=0;
   for(test=0;test<NUMBROFTEST;test++)
        sum+=grades[stnum][test];
   stavg=(double)sum/NUMBROFTEST;
   cout<<"student number "<<stnum<<"had an average of "<<stavg<<endl;

     }
   return ;

}
//Find and print highest score for each exam
void detectTopScore(int grades[][NUMBROFTEST], int classsize)
{
    int stnum,test;
    for(test=0;test<NUMBROFTEST;test++)
     {
   int highscore=grades[0][test];

   for(stnum=0;stnum<classsize;stnum++)
       {
        if(highscore<grades[stnum][test])
          highscore=grades[stnum][test];
       }
       cout<<"Highest score for test number "<<test<<" is "<<highscore<<endl;
     }
     return;
}
//Find and print how many grades above,below and equal to average for each exam
void storeMarks(int grades[][NUMBROFTEST], int classsize,int &numless,int &numgr,int &numeq)
{
   int stnum,test;
   double exavg;
   for(test=0;test<NUMBROFTEST;test++)
     {
   numgr=0;
   numless=0;
   numeq=0;
   exavg=detectExamAverage(grades,classsize);
   for(stnum=0;stnum<classsize;stnum++)
        if(grades[stnum][NUMBROFTEST]<exavg)
          numless++;
        else if(grades[stnum][NUMBROFTEST]>exavg)
          numgr++;
        else
         numeq++;
       cout<<"the number of grades greater than average in exam "<<test<<" is "<<numgr;
       cout<<" less than average is "<<numless<<" and number of grades which are equal to the average is "<<numeq<<endl;
      }

return;
}