Quiz scores program

closed account (ShUk4iN6)
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


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
#include<fstream>
#include<iostream>
using namespace std;

int main()
{
	ifstream fin;
	int quiz_score, student_id,highest_score,lowest_score,total = 0, count_max= 0, count_min=0,count =0, average, numbers;
	double average=0;
	double total=0;


	fin.open("quiz.dat");

	if (!fin.file())  {
         cout << "Error opening stream." << endl;
         abort();
     }

	fin>> student_id>>quiz_score;
	while(!fin.eof())
	{
		average += (total / count);
		for( int quiz_score = 0; quiz_score < count_min; count_min++ )
{
if( numbers[quiz_score] < lowest_score )
{
count_min++;
lowest_score;
}

if( numbers[quiz_score] > highest_score )
{
count_max++;
highest_score;
}



	cout << "Student ID number : " << student_id << endl;
	cout << "Highest Score     : " << highest_score << endl;
	cout << "Lowest Score      : " << lowest_score << endl;
	cout << "Quiz Average      : " << average << endl;
		}
	}

		return 0;
	}
Okay, let's rock!

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?

sudent_id quiz_score
0001 123
0002 96
0003 140
etc

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  

Last edited on
closed account (ShUk4iN6)
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?


closed account (ShUk4iN6)
yes there are 3 or more quiz scores per student
closed account (ShUk4iN6)
so are you saying that i should do something like this
'
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
#include<fstream>
#include<iostream>
using namespace std;

int main()
{
	ifstream fin;
	int number[100],quiz_score,highest_score,lowest_score, count_max= 0, count_min=0,count =0, numbers;
	int student_id[100];
	int HScore[100];
	int LScore[100];
	int Avarage[100];
	double average=0;
	float total=0;
	int line = 0;
	lowest_score = 1000000;
	highest_score = 0;



	fin.open("quizzes.dat");

	while(!fin.eof() && line++ < 100)
		for (int i = 0; i < 100; i++) student_id[i] = HScore[i] = LScore[i] = Avarage[i] = 0;
 {
  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;
 }

 cout << "Highest Score     : " << highest_score << endl;
 cout << "Lowest Score      : " << lowest_score << endl;
 cout << "Quiz Average      : " << total / line << endl; 
 fin.close(); // close the file  

return 0;
}
I hope it will work properly because I didn't try it :)

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
#include <iostream>
#include <fstream>
#include <vector>

using namespace 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.
closed account (ShUk4iN6)
thank you screw that works=]]
You are welcome.
Topic archived. No new replies allowed.