Hello! I just wanted some feedback on my code, and maybe some ways I could make it better. The prompt was to create a program that did the following things:
1. Read a text file with 11 columns and 35 rows. The first column was "student 1, student 2, etc.," and the next 10 columns were their supposed answers on a test (A, B, C, or D).
2. Ask for user input to fill an array for the correct answers to the test.
3. Print all of the student answers.
4. Compare the 1D correctAnswers array to the 2D studentAnswers array (that was read in from a file).
5. Print the students' scores.
6. Print the highest, lowest, and average scores.
If you need any other information, or if I could've worded my question better, please let me know!
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
//---- Libraries ----//
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream infile;
//---- Function Prototypes ----//
void print(string answers[35][11], const int ROWS, const int COLS);
void grade(string answers[35][11], const int ROWS, const int COLS, string correctanswers[10]);
//---- Main Function ----//
int main()
{
const int ROWS = 35;
const int COLS = 11;
string x;
infile.open("exam.txt");
string answers[ROWS][COLS];
if (infile.is_open())
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
infile >> answers[i][j];
}
}
}
string correctanswers[10];
cout << "Please enter the correct answers to the test in all caps.\n";
for (int x = 0; x < 10; x++)
{
cin >> correctanswers[x];
}
print(answers, 35, 11);
grade(answers, 35, 11, correctanswers);
}
void print(string answers[35][11], const int ROWS, const int COLS)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
cout << answers[i][j] << " ";
}
cout << endl;
}
}
void grade(string answers[35][11], const int ROWS, const int COLS, string correctanswers[10])
{
int highest_score = 0;
int lowest_score = 10;
int sum_of_scores;
double average_score;
for (int i = 0; i < 35; i++)
{
int student_score = 0;
for (int j = 1; j < 11; j++)
{
if (correctanswers[j-1] == answers[i][j])
{
student_score += 1;
}
else
{
student_score += 0;
}
}
cout << answers[i][0] << " score is " << student_score << endl;
if (student_score > highest_score)
{
highest_score = student_score;
}
if (student_score < lowest_score)
{
lowest_score = student_score;
}
sum_of_scores += student_score;
}
average_score = (sum_of_scores / 35);
cout << "The highest score is: " << highest_score << endl;
cout << "The lowest score is: " << lowest_score << endl;
cout << "The average score is: " << average_score << endl;
}
|