Need help with C++ please. Thank you. Write a program that will create a grade d
ID: 3789431 • Letter: N
Question
Need help with C++ please. Thank you.
Write a program that will create a grade distribution for students taking a class taught by multiple teachers in the same department using vectors. When you have completed the assignment, put the files: studentReport.cpp and data.txt in a zip file called assignment02.zip.
Student scores on exams for all of the classes have been randomly collected and stored in a file. A common denominator for every class it that an exam is always worth 100 points. Students may have taken anywhere from 2 to 5 exams in their course.
You will need to read data from a file and store it in a two-dimensional vector ( aka a vector of vectors ). I have created the data file for you. The description of the data file is at the end of the assignment.
After you have read all the data and stored it in the vector, I want your program to create a log file with the contents of your vector of vectors. Call the file "vector_data.txt". Here is a link to a file that shows what your log file should look like: vector_data_log_file.txt. After writing out the log file, you can begin processing the data in your vectors to create the grade report. Your report should be written to the file "grade_report.txt".
For every student you will need to calculate an average. Use the average to determine a letter grade using the scale 90 - 100...A 80 - 89...B 65 - 79...C 55 - 64...D < 55...F.
For every student you will be printing: (1) exam scores (2) average score (3) letter grade. You will also need to keep track of the number of A's, B's, C's, etc. so that you can display this information at the end of the report.
Conceptually, a two-dimensional array and a two-dimensional vector are similar. An empty vector can be created initially and then the vector can be "grown" to accomodate data. You use the push_back( ) member function to add elements to a vector.
Note, fields 4-6 may not be present in a record since a student may only have taken 2 exams while a different student may have taken up to 5.
Here is a declaration of an empty vector of vectors: vector< vector > scores; This would create an empty vector called scores. Make sure you do NOT put ( ) following the name of the vector or C++ will think you are trying to create a function. The first dimension will represent a specific student, the second dimension will represent a specific student's scores on a set of exams.
Note: You must put a space before and after vector in your declaration. Otherwise some older C++ compilers get confused about what you are doing.
Each element in scores when added is a vector of integers. Lets say a student took numScores tests where numScores is 3. Assume the record for this student looks like this: 3 85 83 79. Here is how this information would be stored: scores.push_back( vector(numScores) ); This creates a vector of 3 elements. Having created the vector, you can use a loop to get the scores and replace the default values stored in the vector. Use unsigned int (or size_t) as the type for your loop control variable.
Here is how you would reference the first student's first exam score in your vector: scores.at(0).at(0) similiar to array notation of scores[0][0]. In fact you could use the array notation but then there would not be bounds checking (one of the reasons vectors are preferred over C-style arrays).
Please use the at() member function whenever you access an element in a vector.
When you create a loop to process elements in a vector, use the data type "unsigned" (recall that this declares an unsigned int) when declaring the loop control variable. Otherwise, a warning message may be displayed by the compiler. Take advantage of the fact that you can determine the size of a vector using a member function.
Here is a desription of the data file:
Input Record Layout space delimited records Data Type Field Description integer Number of exam scores integer Exam 1 score Exam 2 score integer Exam 3 score integer Exam 4 score integer 0 Exam 5 score integerExplanation / Answer
Answer:
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
srand(time(NULL));
ifstream infile;
ofstream outfile;
int numberOfStudents;
int numScores;
int examScore;
vector< vector<int> > scores;
numberOfStudents = rand() % 11 + 90; // range 90-100
outfile.open("data.txt");
for(int i = 0; i < numberOfStudents; i++)
{
numScores = rand() % 4 + 2; // range 2-5
outfile << numScores;
for(int j = 0; j < numScores; j++)
{
examScore = rand() % 101; // range 0-100;
outfile << " " << examScore;
}
outfile << endl;
}
outfile.close();
infile.open("data.txt");
infile >> numScores;
while(infile)
{
vector<int> vect;
for(int i = 0; i < numScores; i++)
{
infile >> examScore;
vect.push_back(examScore);
}
scores.push_back(vect);
infile >> numScores;
}
infile.close();
outfile.open("vector_data.txt");
outfile << numberOfStudents << " student records were processed." << endl;
outfile << "Here is the accumulated data in the vector studentData:" << endl << endl;
unsigned int studentsLength;
unsigned int scoresLength;
studentsLength = scores.size();
for( unsigned int i = 0; i < studentsLength; i++)
{
scoresLength = scores.at(i).size();
outfile << scoresLength;
for(unsigned int j = 0; j < scoresLength; j++)
{
outfile << " " << scores.at(i).at(j);
}
outfile << endl;
}
outfile.close();
outfile.open("grade_report.txt");
double total;
double average;
char grade;
int As = 0;
int Bs = 0;
int Cs = 0;
int Ds = 0;
int Fs = 0;
studentsLength = scores.size();
for( unsigned int i = 0; i < studentsLength; i++)
{
total = 0;
scoresLength = scores.at(i).size();
for(unsigned int j = 0; j < scoresLength; j++)
{
examScore = scores.at(i).at(j);
total += examScore;
outfile << examScore << " ";
}
average = total / scoresLength;
if(average >= 90)
{
grade = 'A';
As++;
}
else if(average >= 80)
{
grade = 'B';
Bs++;
}
else if(average >= 65)
{
grade = 'C';
Cs++;
}
else if(average >= 55)
{
grade = 'D';
Ds++;
}
else
{
grade = 'F';
Fs++;
}
outfile << setprecision(2) << average << " ";
outfile << grade << endl;
}
outfile << endl;
outfile << "Number of A grades: " << As << endl;
outfile << "Number of B grades: " << Bs << endl;
outfile << "Number of C grades: " << Cs << endl;
outfile << "Number of D grades: " << Ds << endl;
outfile << "Number of F grades: " << Fs << endl;
outfile.close();
return 0;
}
Output:
data.txt:
2 28 9
4 64 100 81 42
5 75 53 86 44 76
2 59 58
4 76 73 88 27
2 84 67
4 76 6 10 55
3 63 83 35
4 46 0 68 89
5 8 7 7 52 49
3 78 6 55
3 80 8 46
3 58 79 88
2 85 98
5 10 60 37 11 13
3 79 47 3
4 55 11 8 73
5 100 50 33 20 35
4 95 82 39 52
2 26 51
2 91 5
2 16 42
5 29 91 44 42 95
2 97 72
5 70 98 17 86 30
4 21 8 98 69
3 49 95 40
2 5 30
4 60 47 13 25
4 70 69 85 30
4 47 1 31 83
4 48 35 29 52
4 4 49 91 52
3 51 58 65
5 89 36 83 1 49
2 9 18
4 60 49 54 7
3 51 56 15
2 91 11
2 12 15
5 69 33 63 20 91
3 43 45 29
3 12 44 99
5 29 40 82 44 60
4 60 11 78 42
4 34 19 26 47
4 24 82 33 88
5 90 81 10 35 77
3 47 20 66
2 49 72
4 93 32 38 53
5 15 61 52 50 80
5 63 80 68 10 12
4 79 1 36 55
2 12 23
4 99 89 85 48
5 1 40 25 6 59
5 21 19 52 37 99
5 66 78 29 77 56
4 55 58 87 76
3 65 99 76
2 54 61
3 81 28 17
2 34 42
5 22 28 23 25 26
4 92 71 13 68
2 98 89
3 50 64 77
3 29 52 78
3 12 54 29
5 71 0 41 80 5
2 7 95
5 100 79 46 70 58
5 63 22 68 79 72
5 21 87 60 74 30
2 52 84
3 59 21 37
3 0 8 62
2 2 16
4 47 28 43 5
3 72 27 41
5 65 73 38 51 99
5 47 7 29 31 10
3 52 14 88
2 22 49
2 25 32
4 38 26 73 9
2 44 36
2 61 68
2 65 85
2 42 32
2 71 63
3 92 81 76
4 65 99 28 56
vector_data.txt
94 student records were processed.
Here is the accumulated data in the vector studentData:
2 28 9
4 64 100 81 42
5 75 53 86 44 76
2 59 58
4 76 73 88 27
2 84 67
4 76 6 10 55
3 63 83 35
4 46 0 68 89
5 8 7 7 52 49
3 78 6 55
3 80 8 46
3 58 79 88
2 85 98
5 10 60 37 11 13
3 79 47 3
4 55 11 8 73
5 100 50 33 20 35
4 95 82 39 52
2 26 51
2 91 5
2 16 42
5 29 91 44 42 95
2 97 72
5 70 98 17 86 30
4 21 8 98 69
3 49 95 40
2 5 30
4 60 47 13 25
4 70 69 85 30
4 47 1 31 83
4 48 35 29 52
4 4 49 91 52
3 51 58 65
5 89 36 83 1 49
2 9 18
4 60 49 54 7
3 51 56 15
2 91 11
2 12 15
5 69 33 63 20 91
3 43 45 29
3 12 44 99
5 29 40 82 44 60
4 60 11 78 42
4 34 19 26 47
4 24 82 33 88
5 90 81 10 35 77
3 47 20 66
2 49 72
4 93 32 38 53
5 15 61 52 50 80
5 63 80 68 10 12
4 79 1 36 55
2 12 23
4 99 89 85 48
5 1 40 25 6 59
5 21 19 52 37 99
5 66 78 29 77 56
4 55 58 87 76
3 65 99 76
2 54 61
3 81 28 17
2 34 42
5 22 28 23 25 26
4 92 71 13 68
2 98 89
3 50 64 77
3 29 52 78
3 12 54 29
5 71 0 41 80 5
2 7 95
5 100 79 46 70 58
5 63 22 68 79 72
5 21 87 60 74 30
2 52 84
3 59 21 37
3 0 8 62
2 2 16
4 47 28 43 5
3 72 27 41
5 65 73 38 51 99
5 47 7 29 31 10
3 52 14 88
2 22 49
2 25 32
4 38 26 73 9
2 44 36
2 61 68
2 65 85
2 42 32
2 71 63
3 92 81 76
4 65 99 28 56
grade_report.txt
28 9 18 F
64 100 81 42 72 C
75 53 86 44 76 67 C
59 58 58 D
76 73 88 27 66 C
84 67 76 C
76 6 10 55 37 F
63 83 35 60 D
46 0 68 89 51 F
8 7 7 52 49 25 F
78 6 55 46 F
80 8 46 45 F
58 79 88 75 C
85 98 92 A
10 60 37 11 13 26 F
79 47 3 43 F
55 11 8 73 37 F
100 50 33 20 35 48 F
95 82 39 52 67 C
26 51 38 F
91 5 48 F
16 42 29 F
29 91 44 42 95 60 D
97 72 84 B
70 98 17 86 30 60 D
21 8 98 69 49 F
49 95 40 61 D
5 30 18 F
60 47 13 25 36 F
70 69 85 30 64 D
47 1 31 83 40 F
48 35 29 52 41 F
4 49 91 52 49 F
51 58 65 58 D
89 36 83 1 49 52 F
9 18 14 F
60 49 54 7 42 F
51 56 15 41 F
91 11 51 F
12 15 14 F
69 33 63 20 91 55 D
43 45 29 39 F
12 44 99 52 F
29 40 82 44 60 51 F
60 11 78 42 48 F
34 19 26 47 32 F
24 82 33 88 57 D
90 81 10 35 77 59 D
47 20 66 44 F
49 72 60 D
93 32 38 53 54 F
15 61 52 50 80 52 F
63 80 68 10 12 47 F
79 1 36 55 43 F
12 23 18 F
99 89 85 48 80 B
1 40 25 6 59 26 F
21 19 52 37 99 46 F
66 78 29 77 56 61 D
55 58 87 76 69 C
65 99 76 80 B
54 61 58 D
81 28 17 42 F
34 42 38 F
22 28 23 25 26 25 F
92 71 13 68 61 D
98 89 94 A
50 64 77 64 D
29 52 78 53 F
12 54 29 32 F
71 0 41 80 5 39 F
7 95 51 F
100 79 46 70 58 71 C
63 22 68 79 72 61 D
21 87 60 74 30 54 F
52 84 68 C
59 21 37 39 F
0 8 62 23 F
2 16 9 F
47 28 43 5 31 F
72 27 41 47 F
65 73 38 51 99 65 C
47 7 29 31 10 25 F
52 14 88 51 F
22 49 36 F
25 32 28 F
38 26 73 9 36 F
44 36 40 F
61 68 64 D
65 85 75 C
42 32 37 F
71 63 67 C
92 81 76 83 B
65 99 28 56 62 D
Number of A grades: 2
Number of B grades: 4
Number of C grades: 12
Number of D grades: 18
Number of F grades: 58