segmentation fault

hi all.. i was stuck at this problem for quite some time. i do not know if my program will produce a correct output or not, because i got segmentation fault.. and i have been struggling to figure it out. i try debugging the program, and it says I have the problem at compute average. but I do not know how to fix 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

// FUNCTION PROTOTYPES GO HERE:
   void read_student(int & students);	
   void read_exams(int students,int * scores1, int * scores2, int * scores3);
   void compute_totals(int students,int*scores1,int * scores2, int* scores3,int* totals);
   double compute_average(int students,int *totals);
   int find_min(int students, int * totals); 
   int find_max(int students, int * totals);
   int above_average(int * totals,int students,double avg);
   void display_table(int *scores1, int *scores2, int *scores3,int *totals,int students, double avg,int max,int min,int above);
   void print_top();
   void call_student(int students,int *scores1,int *scores2,int *scores3,int *totals,double avg);
   void print_bottom(int students,double avg,int min,int max,int above);

int main()
{
	int * scores1;
	int * scores2;
	int * scores3;
	int * totals;
	int students(0);
	double avg(0.0);
	int min, max, above;
	

	// Prompt for the number of students
	   read_student(students);

	// Allocate arrays to hold exam scores and totals
	   scores1 = new int[students];
	   scores2 = new int[students];
	   scores3 = new int[students];
	   totals = new int[students];

	// Prompt and read exams for each student
	   read_exams(students,scores1,scores2,scores3);

	// Compute exam totals for each student
	   compute_totals(students,scores1,scores2,scores3,totals);

	// Compute the average of the total scores
           avg = compute_average(students,totals);

	// Find the minimum total score
	   min =find_min(students,totals);

	// Find the maximum total score
	   max = find_max(students,totals);

	// Compute the number of students with total scores at or average of the total scores
	   above = above_average(totals,students,avg);
	   
	// Display table
	   display_table(scores1,scores2,scores3,totals,students,avg,max,min,above);
	   
	// De-allocate arrays
	   delete [] scores1;
	   delete [] scores2;
	   delete [] scores3;
	   delete [] totals;


	return 0;
}
		 
	// FUNCTION DEFINITIONS GO HERE:
	void read_student(int & students )
	   {
		cout<<"Enter number of student: "; //ask user input
			
		while (cin>>students && students<=0) //check if user input is >0
		{
			cout<<"Sorry, you must enter positive value."<<endl;
			cout<<"Enter number of student: ";	
		}
		cout<<endl;
	   }
	
	void read_exams(int students,int * scores1, int * scores2, int * scores3)
	   {
		
		for(int i=0; i<students;i++)
		{
			cout<<"Enter the three exam scores for student #"<<i+1<<":";
		        cin>>scores1[i]>>scores2[i]>>scores3[i];	

		}
	   } 
	 void compute_totals(int students,int*scores1,int * scores2, int* scores3,int* totals)
	{
		for(int i =0;i<students; i++)
		{
			totals[i] = scores1[i] + scores2 [i] + scores3 [i];
		}
	}

	 double compute_average(int students,int *totals)
	{
		double average(0.0);		
		int total(0);
	
		for (int i=0; i<students; i++)
		{
			total +=totals[i];
		}
		
		average = double(total)/students;

		return(average);	
	}
	 int find_min(int students, int * totals)
	{
		int min= totals[0];

		for (int i=1;i<students;i++)
		{
			if(totals[i] < totals[0])
			{
				min=totals[i];
			} 
		}	
		return (min);
	}
	 int find_max(int students, int * totals)
	{
		int max= totals[0];

		for (int i=1;i<students;i++)
		{
			if(totals[i] > totals[0])
			{
				max=totals[i];
			} 
		}	
		return (max);
	}
	
	int above_average(int * totals,int students,double avg)
	{
		int up(0);

		for (int i=0;i<students;i++)
		{
			if(totals[i]>avg)
			{
				up++;
			}
		}
		return (up);
	}
	
	void display_table(int *scores1, int *scores2, int *scores3,int *totals,int students, double avg,int max,int min,int above)
	{
	
		//call function 1
		print_top;
		//call function 2
		for(int i=0;i<=students;i++)
		{	
			call_student(students, scores1,scores2,scores3,totals,avg);	
		}	
		//call function 3
		print_bottom(students,avg,min,max,above);
	
	}

	//function 1
	void print_top()
	{
		cout<<"-----------------------------------------------"<<endl;
		cout<<"Student"<<setw(3)<<"Score 1"<<setw(3)<<"Score 2"<<setw(3)<<"Score 3"<<setw(3)<<"total";
	}

	//function 2
	void call_student(int students,int *scores1,int *scores2,int *scores3,int *totals,double avg)
	{
		int i;		
		
		cout<<i+1<<setw(3)<<scores1<<setw(3)<<scores2<<setw(3)<<scores3<<setw(3)<<totals;
		
		if(totals[i]>avg)
		{
			cout<<"+"<<endl;
		}else if(totals[i]<avg)
		{
			cout<<"-"<<endl;
		}else
		cout<<"="<<endl;	
	}
	
	//function 3
	void print_bottom(int students,double avg,int min,int max,int above)
	{
		cout<<"-----------------------------------------------"<<endl;
		cout<<"-----------------------------------------------"<<endl;
		cout<<"The number of Students is:"<<students;
		avg = round(avg);
		cout<<"The avg total score (rounded) is:"<<avg;
		cout<<"The maximum total score is:"<<max;
		cout<<"The minimum total score is:"<<min;
		cout<<"Total scores at or above the avg is:"<<above;
		cout<<"-----------------------------------------------"<<endl;	
	}


example of the correct out put right here


Enter number of students: 3

Enter the three exam scores for student #1: 11 12 13
Enter the three exam scores for student #2: 11 14 15
Enter the three exam scores for student #3: 16 17 18

-----------------------------------------------
Student   Score 1   Score 2   Score 3   Total
-----------------------------------------------
      1   11        12        13        36    -
      2   11        14        15        40    -
      3   16        17        18        51    +
-----------------------------------------------
The number of students is:              3
The avg total score (rounded) is:       42
The maximum total score is:             51
The minimum total score is:             36
Total scores at or above the avg is:    1
-----------------------------------------------
Last edited on
for (int i=0; i, students; i++)
Not sure what you meant here, but the condition should be i < students.

Explanation: the comma operator evaluates each expression successively and has the value of the last expression. So, for instance, x = (54 == 42), 13; will assign 13 to x. Of course, all the expressions that are not the last one and don't have side effects do nothing. That example is equivalent to just x = 13;, but not to x = i++, 13;.
yea, my error there, I just fixed it..
after i change it into "i<students",

the segmentation fault still shows up.


I am not sure what should do to fix this problem..
There's another one in the loop condition in display_table(). <= instead of <.
i will try to fix that, but i think the main problem is the compute_average. because my program stuck there and cannot produce the remaining input.


so, unless i could figure out what to do in there, any change that I do below the compute average is kinda useless..

because i cannot check it, and i still could compile the program
Well, compute_average() is correct, so the problem is definitely not there. Besides find_max() and find_min() which are incorrect, and call_student() which prints pointers instead of data, I can't find any other obvious bugs. Certainly nothing that would cause a crash.
Are you sure you didn't forget to recompile?
i find the problem when i ask my friend, now i am trying to finish it. thanks for the help, appreciate it.
Topic archived. No new replies allowed.