1
/********************************************************************
2 **
3 ** Programmer: Dr. Marianne Vakalis
4 **
5 ** Date: September 1998
6 **
7 ** Course: Comp120, Monday 06:00 - 09:40 p.m.
8 **
9 ** Assignment: Example program for Learning Unit 1
10 **
11 ** File Name: /home/cs/comp120/fileprog.C
12 ** /home/cs/comp120/fileprog.exe
13 **
14 ** Description: inputs identification numbers, project scores,
15 ** midterm exam scores, and final exam scores for
16 ** students from a data file, and computes and prints
17 ** project averages, midterm averages, semester
18 ** midterm exam scores, and final exam scores for
19 ** averages, and letter grades.
20 **
21 ** Input: a data file
22 **
23 ** Output: a result file
24 **
25 ********************************************************************/
26
27 // Preprocessor directives:
28
29 #include <iostream>
30 #include <fstream>
31 #include <stdlib.h>
32
33 int computedProjectAverage(studentRecord stRec);
34 int computedMidtermExamAverage(studentRecord stRec);
35 int computedSemesterAverage(int prAvg, int mtermAvg, int finExam);
36 char computedLetterGrade(int average);
37 // File name definitions:
38
39 const char inputFileName[15] = "STU.TXT";
40 const char outputFileName[15] = "STU.RES";
41
42 // Global structure declaration:
43
44 struct studentRecord
45 {
46 int studentIdno;
47 int project1Score;
48 int project2Score;
49 int project3Score;
50 int test1Score;
51 int test2Score;
52 int finalExamScore;
53 char letterGrade;
54 };
55 // end struct studentRecord
56
57
58 void main()
59 {
60 studentRecord stuData;
61 ifstream studentFile;
62 ofstream resultFile;
63 int projAvg, midtermAvg, semAvg;
64
65 // Function body:
66
67 studentFile.open(inputFileName);
68
69 if (studentFile.fail())
70 {
71 cerr <<
"\n***> Open error while reading input file "
72
<<
inputFileName;
73 exit(-1);
74 }
75 // end if
76
77 resultFile.open(outputFileName);
78
79 if (resultFile.fail())
80 {
81 cerr <<
"\n***> Open error on output file "
82
<<
outputFileName;
83 exit(-1);
84 }
85 // end if
86
87 studentFile >> stuData.studentIdno
>> stuData.project1Score
88
>>
stuData.project2Score >> stuData.project3Score
89
>>
stuData.test1Score >> stuData.test2Score
90
>>
stuData.finalExamScore;
91
92 while (! studentFile.eof())
93 {
94 projAvg =
computedProjectAverage(stuData);
95 midtermAvg =
computedMidtermExamAverage(stuData);
96 semAvg =
computedSemesterAverage(projAvg, midtermAvg,
97
stuData.finalExamScore);
98
99 resultFile <<
"Student idno : " << stuData.studentIdno << "\n"
100
<<
"Project average : " << projAvg << "\n"
101
<<
"Midterm exam average: " << midtermAvg << "\n"
102
<<
"Semester average : " << semAvg << "\n"
103
<<
"Final letter grade : "
104
<<
(stuData.letterGrade = computedLetterGrade(semAvg))
105
<<
"\n\n";
106
107 studentFile >>
stuData.studentIdno >> stuData.project1Score
108
>>
stuData.project2Score >> stuData.project3Score
109
>>
stuData.test1Score >> stuData.test2Score
110
>>
stuData.finalExamScore;
111 }
112 // end while
113
114 studentFile.close();
115 resultFile.close();
116 }
117 // end function main
118
119
120 /*************************************************
121 **
122 ** Name: computedProjectAverage
123 **
124 ** Description: computes a student's project average using the
125 ** formula:
126 ** (0.40 * (best of the first two project scores) +
127 ** 0.60 * third project score)
128 **
129 ** Parameters
130 **
131 ** Input:
132 ** s - student data record
133 **
134 ** Output:
135 ** returns computed project average
136 **
137 **************************************************/
138 int computedProjectAverage(studentRecord s)
139 {
140 int bestOfFirstTwo;
141
142 // Function body:
143
144 bestOfFirstTwo = s.project1Score;
145
146 if (s.project1Score < s.project2Score)
147 bestOfFirstTwo =
s.project2Score;
148 // end if
149
150 return (0.40 * bestOfFirstTwo + 0.60 *
s.project3Score);
151 }
152 // end function computedProjectAverage
153
154
155 /*************************************************
156 **
157 ** Name: computedMidtermExamAverage
158 **
159 ** Description: computes a student's midterm exam average using the
160 ** formula:
161 ** (0.35 * first test score + 0.65 * second test score)
162 **
163 ** Parameters
164 **
165 ** Input:
166 ** s - student data record
167 **
168 ** Output:
169 ** returns computed midterm exam average
170 **
171 **************************************************/
172 int computedMidtermExamAverage(studentRecord s)
173 {
174 return (0.35 * s.test1Score + 0.65 *
s.test2Score);
175 }
176 // end function computedMidtermExamAverage
177
178
179 /*************************************************
180 **
181 ** Name: computedSemesterAverage
182 **
183 ** Description: computes a student's semester average using the
184 ** formula:
185 ** (0.30 * project average +
186 ** 0.40 * midsemester exam average +
187 ** 0.30 * final exam score)
188 **
189 ** Parameters
190 **
191 ** Input:
192 ** pavg - student's project average
193 ** mavg - student's midterm average
194 ** finalExam - student's final exam grade
195 **
196 ** Output:
197 ** returns computed semester average
198 **
199 **************************************************/
200 int computedSemesterAverage(int pavg, int mavg, int finalExam)
201 {
202 return (0.30 * pavg + 0.40 * mavg + 0.30 *
finalExam);
203 }
204 // end function computedSemesterAverage
205
206
207 /*************************************************
208 **
209 ** Name: computedLetterGrade
210 **
211 ** Description: computes a student's letter grade using the
212 ** following scheme:
213 ** Semester Average Letter Grade
214 ** ---------------- ------------
215 ** 90 - 100 A
216 ** 80 - 89 B
217 ** 70 - 79 C
218 ** 60 - 69 D
219 ** 0 - 59 F
220 **
221 ** Parameters
222 **
223 ** Input:
224 ** avg - student's semester average
225 **
226 ** Output:
227 ** returns computed letter grade
228 **
229 **************************************************/
230 char computedLetterGrade(int avg)
231 {
232 char grade;
233
234 // Function body:
235
236 if (avg < 80)
237 if (avg < 60)
238
grade
= 'F';
239 else
240
if
(avg < 70)
241
grade
= 'D';
242
else
243
grade
= 'C';
244
//
end if
245 // end if
246
247 if (avg >= 80)
248 if (avg < 90)
249
grade
= 'B';
250 else
251
grade
= 'A';
252 // end if
253 // end if
254
255 return grade;
256 }
257 // end function computedLetterGrade
258
259