Assignment
Assignment
Objectives:
This lab cover topics Input and Output into a File (Chapter 6), Control Structure (Chapter 7
and 8) and Function (Chapter 9) of lectures.
Important Tip:
When you write a computer program, start with a simple program first for example just start
with a simple reading and writing process. If your program running well, then extend the
program step by step to cover the complex situation. This idea represents in this lab. Once
you see your program running, it becomes an addiction to continue writing your program.
---------------------------------------------------------------------------------------------------------------
Step 1
1. The program1 shows in Figure 1 is to read marks of three subjects (mark1, mark2, and
mark3) for a student from keyboard, calculate total marks (totalmark) and average marks
(avgmark) and then display the value of mark1, mark2, mark3, totalmark and avgmark
on the screen.
2. The input of the Program1 shows in Figure 2 and the output shows in Figure 3.
Figure 1: Program1 (Read data from keyboard and display output on the computer’s
screen).
1
Figure 2: Input of the program1 from computer keyboard.
2
Figure 4: Program 2 (Read data input from file “student.dat” and display the output on the
computer’s screen)
3
Figure 6: Program3 (Read data input from file “student.dat” and write the output into file
output “student.out”)
4
Figure 8: Program4 (Read a studentname (string) from a file input and write a string into
file output).
6
Figure 13: Program 6 (Process for 10 student records).
8
Figure 16: Program 7 (Print message “FAIL” or “PASS” at the end of student’s record).
9
Figure 17: A new output file “student.out” with a “FAIL” or “PASS” message.
---------------------------------------------------------------------------------------------------------------
Step 8
1. Program 8 show in Figure 18 is extension of Program 7.
2. Program 8 is create another file output “studentfail.out” which contain list of student
failed at least one of the subjects.
3. Input file “student.dat” shows in Figure 12 and a new file output “studentfail.out”
shows in Figure 19.
10
Figure 18: Program 8 (Create another ouput file “studentfail.out” to record all failed
students).
11
Figure 19: Output file “studentfail.out” which contain all student’s records failed at least
one subject.
---------------------------------------------------------------------------------------------------------------
Step 9
1. Program 9 shows in Figure 20 perform the same operations with Program 8 but the
calculation of total mark and averages marks perform in different function named
caltotalmark and calaveragemark.
2. Input file “student.dat” shows in Figure 12 and the output of the file “student.out” and
“studentfail.out” shows in Figure 17 and Figure 19.
12
13
Figure 20: Program 9.
14