Hey everyone, I am trying to write a program to loop through "Quiz.dat"file and for each student, output the students ID number along with the students quiz average after the highest and lowest quiz scores for the student have been thrown out.I came to a point that I have no more ideas whatelse to do. So I need some help.Thanks for the help
You didn't define an array so the number variable won't be used as an array: if( numbers[quiz_score] < lowest_score )
You should put this line at the beginning of your code :int number[100] (I prefer the vector because it isn't limited as the array in my opinion.) Here the max number of student is 100!
My second question is that all student has got sudent_id and quiz_score? The file (Quiz.dat) contains the sequence of like this record?
because in your 20 line you read the student_id once! You should put it into the while loop like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int line = 0;
lowest_score = 1000000;
highest_score = 0;
while(!fin.eof() && line++ < 100) // this is a checking for the max storable data
{
fin>> student_id>>quiz_score;
if (lowest_score > quiz_score) lowest_score = quiz_score;
if (highest_score < quiz_score) highest_score = quiz_score;
cout << "sudent id: " << student_id << " score: " << quiz_score << endl;
total = total + quiz_score; // it has the same effect as total += quiz_score
}
cout << "Highest Score : " << highest_score << endl;
cout << "Lowest Score : " << lowest_score << endl;
cout << "Quiz Average : " << total / line << endl; // it will be a rounded result,
// if you want to preciese result you should use float or double
fin.close(); // close the file
yes they all do. I got it working. I have one more question I need highest, lowest and average scores for all of them how can i do that by the way thanks for the help
Should it have more than one piece of quiz_score for a student? Do more quiz_score belong to a student?
In my solution all students have only one quiz_score. If more quiz score belong to a student then three arrays have to be made which can store an count the Highest, Lowest, Avarage for a student.
Like this:
1 2 3 4 5 6 7 8
int student_id[100];
int HScore[100];
int LScore[100];
int Avarage[100];
// Initialization for arrays
for (int i = 0; i < 100; i++) student_id[i] = HScore[i] = LScore[i] = Avarage[i] = 0;
In the while loop you should search the student_id in student_id array because their position have to be specified. If there is being (Is it correct in English? Sorry) then it just have to be incremented the HScore an LScore values at specified index.
(And it seems at this point that the solution with vector would be better because there is dynamically array and you can inserted the incoming data in the vector. In addition if more data belonged to a student_id then it would be better to use struct or own class.)
So my question :) again
Should it have more than one piece of quiz_score for a student?
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
struct student_data
{
int student_id;
int total_score;
int quiz_number;
int HScore ;
int LScore ;
};
int main()
{
vector <student_data *> student_vector;
ifstream fin;
fin.open("quizzes.dat");
while(!fin.eof())
{
int student_id, quiz_score;
fin>> student_id>>quiz_score;
bool found = false;
for (int i = 0; i < student_vector.size() && !found; i++) // check if there is an existing student_id
{
student_data * sd = student_vector[i];
if (sd->student_id == student_id) // yes, found it
{
sd->quiz_number++;
sd->total_score += quiz_score;
if (sd->HScore < quiz_score) sd->HScore = quiz_score;
if (sd->LScore > quiz_score) sd->LScore = quiz_score;
found = true;
}
}
if (!found || student_vector.empty()) // didn't find it or the student vector is empty
{
student_data * sd = new student_data;
sd->student_id = student_id;
sd->total_score = quiz_score;
sd->HScore = sd->LScore = quiz_score;
sd->quiz_number = 1;
student_vector.push_back(sd);
}
}
fin.close();
// Until now we load the data to vector
// Now them will be listed
for (int i = 0; i < student_vector.size(); i++)
{
student_data * sd = student_vector[i];
cout << "Student_id: " << sd->student_id << endl;
cout << "Highest Score: " << sd->HScore << endl;
cout << "Lowest Score: " << sd->LScore << endl;
cout << "Average: " << sd->total_score / sd->quiz_number << endl << endl;
}
// All data have to be released
for (int i = 0; i < student_vector.size(); i++)
{
student_data * sd = student_vector[i];
delete sd;
}
return 0;
}
Of course the my solution would have been better if I had used a class plus get and set functions.