124k views
4 votes
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 student’s 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 student’s 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.

User YichenBman
by
6.4k points

2 Answers

4 votes

This program assumes that the test data file is formatted as described in your question.

def calculate_score(answers, student_answers):

score = 0

for i in range(len(answers)):

if student_answers[i] == answers[i]:

score += 2

elif student_answers[i] != ' ':

score -= 1

return max(0, score) # Ensure the score is not negative

def calculate_grade(score, total_questions):

percentage = (score / (2 * total_questions)) * 100

if 90 <= percentage <= 100:

return 'A'

elif 80 <= percentage < 90:

return 'B'

elif 70 <= percentage < 80:

return 'C'

elif 60 <= percentage < 70:

return 'D'

else:

return 'F'

def process_test_data(file_path):

with open(file_path, 'r') as file:

data = file.read().split()

answers = data[0]

student_data = data[1:]

total_questions = len(answers)

User BumbleShrimp
by
6.8k points
6 votes

Answer:

see explaination

Step-by-step explanation:

Program correct code

#include <iostream>

#include <fstream>

#include <iomanip>

#include <string.h>

using namespace std;

int main()

{

ifstream infile("Chap8.txt");

if (!infile)

{

cout << "unable to open file so exiting from program :";

return 0;

}

char answers[25];

string str;

getline(infile, str);

for (int i = 0; i < str.length(); i++)

answers[i] = str[i];

while (!infile.eof()) {

getline(infile, str);

int i;

for (i = 0; i < str.length(); i++) {

if (str[i] == ' ')

break;

else

cout << str[i];

}

cout << " ";

int k = 0;

double score = 0;

for (int j = i + 1; j < str.length(); j++, k++) {

cout << str[j];

if (str[j] == ' ')

continue;

else if (str[j] == answers[k])

score = score + 2;

else if (str[j] != answers[k])

score = score - 1;

}

cout << " ";

double avg = 100.0 * score / 40.0;

//you were outputting the avg, you have to output score instead.

cout << score << " ";

//90%-100%, A; 80%-89.99%, B; 70%-79.99%, C; 60%-69.99%, D; and 0%-59.99%, F.

if (avg >= 90 && avg <= 100)

cout << "A" << endl;

else if (avg >= 80 && avg <= 89.99)

cout << "B" << endl;

else if (avg >= 70 && avg <= 79.99)

cout << "C" << endl;

else if (avg >= 60 && avg <= 69.99)

cout << "D" << endl;

else if (avg >= -15 && avg <= 59.99)

cout << "F" << endl;

}

return 0;

}

/*OUTPUT*/

LAU76359 TFTFTFTFTFTFTFTFTFTF 1 F

PUC98563 TFFTTTFTFTFFTTFFFTFF 34 B

RAM69522 TFFTTTFT FFTTFTTTFF 21 F

GPS22106 TFFTTTFTFTFTTTTTTTTT 28 C

UGP67851 FFFFFFFFFFFFFFFFFFFF 7 F

MUD02001 T F T F T F T F T F 17 F

IBM99887 FTTF FTTFFTFFFTT -3 F

CAM78999 FTFTFT TFTFTF FTFTFT 15 F

User Amir Popovich
by
7.1k points