INDIVIDUAL ASSIGNMENT
TECHNOLOGY PARK MALAYSIA
CT018-3-1-ICP
INTRODUCTION TO C PROGRAMMING
TAN XIAO RONG TP059541
APU1F2006CS(DA)
HAND OUT DATE: 18 JANUARY 2021
HAND IN DATE: 22 FEBRUARY 2021
WEIGHTAGE: 50%
INSTRUCTIONS TO CANDIDATES:
1. Submit your assignment online in Moodle unless advised otherwise.
2. Late submission will be awarded zero (0) unless Extenuating Circumstances (EC)
are upheld.
3. Cases of plagiarism will be penalized.
4. You must obtain at least 50% in each component to pass this module.
Table of Content
1.0 Introduction and Assumption
2.0 Design of the Program
2.1 Pseudocode
2.2 Flow Chart
3.0 Sample of source code
3.1 Sample of output
4.0 Conclusion
5.0 Reference
1.0 Introduction and Assumption
There is a university that requires a telephone directory system. A university is same as an
organization. For a small-scale university or company, there will be at least four different apartments,
and each of the apartments will need at least 3 employees to organize it.
A complete telephone operator system must contain different types of features such as adding
new employees, searching employee’s extension number, updating new numbers for employees,
classifying different departments, etc. According to figure 1, there will be five departments in the
university, each department will be available for a thousand employees inside which means there are
also thousands of extension numbers available to use.
Extension number Department Amount limit
1000 to 1999 ACTUARIAL 1000
3000 to 3999 DATABASE 1000
5000 to 5999 ENGINE 1000
7000 to 7999 FINANCE 1000
9000 to 9999 VISA 1000
Figure 1: Distribution of extension number and department
Each of every number is unique. 9 representative visa department, 7 representative finance
department, 5 representative engine department, 3 representative database department, 1 representative
actuarial department. The limit amount is leaving to future use, it is also available for the sub-department
in the future.
The university is in the scaling phase, and it only has twenty-five employees inside. To record
all the information, there will only require one text file. Inside the text file, there will record four data,
which are, employee name, id, department name, and extension number. When initializing the input, the
program will use the structure to store the data into a list form then store into a text file directly. After
the list is completely generated, it should be able to use the program to open to read, write, or append.
Firstly, there should be ten employees already working in the university, so in the beginning will
input the existing employees by using structures. Secondly, the users should be able to search and update
the employee extension number by input their name and id number. Next, the university is in the scaling
phase, so the system should be allowed to add the new employee into the text file. Lastly, the text file
was adding those employees’ information randomly, so the order of every department must be in a mixed
order. A list must be in a neat status, so it should require sorting those mixed data in every department
stays together. Since, every department has their representative digit, so it should sort those digits in
ascending order. All features should be arranging into a menu function and allowed users to choose
them.
2.0 Design of the program
2.1 Pseudocode
2.2 Flow Chart
Program call menu function Structure declaration
START
START
Declare a record structures name as
employee
menu()
Declare name, id, dept, no
End
END
Menu function
menu()
Declare choice as empty character variable
Print "|******************* ' w ' WELCOME TO
TELEPHONE DIRECTORY SYSTEM FOR APU STAFF > w <
********************|\n"
Print "\t\t A. Input existed
employee data.\n"
Print "\t\t B. Search
extension number.\n"
Print "\t\t C. Add new
employee.\n"
Print "\t\t D. Update old
number.\n"
Print "\t\t E. Arrange mix
record.\n"
Print "\t\t Enter
selection:"
Read choice
Read i
True
case A
Input break
case a
False
True
case B
Search break
case b
False
True
case C
Add break
case c
False
True
case D
Update break
case d
False
True
case E
List break
case e
False
default Print
"Invalid input.
Adios.
END
Input function
Input()
Declare fp as a file pointer
Declare i as an empty integer variable
Declare size as an empty integer variable
Define fp as "tel.txt" text file
Open fp in write mode
False True
fp NOT =
NULL
Print "Enter the amount of
employee: "
Print "Error
create file."
Read size of employee
exit to 1
exit to 1
Definestructure
structure employee
employee as emp[size]
as emp[size]
Writeexit
"\n"to into
1 file
True
DO i = 0 TO False
size
exit to 1
Print "\n"
exit to 1
Print "\n"
Flush output buffer
Print "Enter extension
number: "
Close fp
Get string emp[i].no
END
Print "Enter name: "
Get string emp[i].name
Print "Enter ID: "
Get string emp[i].id
Print "Enter department
name: "
Get string emp[i].dept
exit to 1
Print "\n"
write array emp[i].no, emp[i].name,
emp[i].id, emp[i].dept into file
Search function Search()
Declare fp as a file pointer
Declare content as empty character array
Declare search as empty character array
Declare name_id as empty character array
Declare num as zero integer
Define fp as "tel.txt" text file
Open fp in read mode
exit to 1
Print "Enter name or id: "
exit to 1
Read name_id
False True
fp NOT =
NULL
Print "File
unreadable."
True DOWHILE (fp not False
pointing to end-of-
file)
Get file string as content line by line
Define search as name_id is substring of content
False True
num = 0
True search NOT = False
NULL exit to 1
Print "Invalid input."
Print "Result
found.\n"
exit to 1
Print content
num = 1
break
Close fp
END
Add()
Add function
Declare fp as a file pointer
Define fp as "tel.txt" text file
Open fp in append mode
Declare i as an empty integer variable
Declare size as an empty integer variable
False True
fp NOT =
NULL
Print "Enter the amount of
employee: "
exit "Error
Print to 1
create file."
Read size of employee
exit to 1
exit to 1
structure
Definestructure employee
employee as emp[size]
as emp[size]
True
DO i = 0 TO False
size
exit to 1
Print "\n"
Flush output buffer
exit to 1
Print "\n"
Print "Enter extension
number: "
Get string emp[i].no Close fp
END
Print "Enter name: "
Get string emp[i].name
Print "Enter ID: "
Get string emp[i].id
Print "Enter department
name: "
Get string emp[i].dept
exit to 1
Print "\n"
write array emp[i].no, emp[i].name,
emp[i].id, emp[i].dept into file
Update function Update()
Declare fp as a file pointer
Declare ftemp as a file temporary pointer
Define filename as "tel.txt" text file
Declare buffer as character array
Declare old_num as character array
Declare new_num as character array
Declare content as character array
Define fp as filename
Open filename in read mode
Open ftemp in write mode
Search
exit to 1
Print "Enter old number: "
exit to 1
Read old_num
exit to 1
Print "Enter new number: "
exit to 1
Read new_num
False filename OR True
ftemp NOT =
NULL
Printexit
"Unable
to 1 to
open file."
True DOWHILE (Get filename False exit to 1
exit to 0
character array as buffer
NOT = NULL )
rep
END
Put filename character array buffer
to ftemp
Close fp
REMOVE filename
RENAME ftemp
as filename
exit to 1
Print "Update successfully !"
Define fp as filename
Open fp in read mode
False DOWHILE (fp True
NOT in end-
of-file)
Get file
exitcharacter
to 1
array content
exit to 1
Display content
Close fp
END
List function List()
Define structure employee emp[50]
Declare
Declare content as
content as empty
emptycharacter
character
Declare i as zero integer variable
Declare size as an empty integer variable
Declare num as zero integer
Declare fp as a file pointer
Define fp as "tel.txt" text file
Open fp in read mode
False True
fp NOT =
NULL
content = Get character from fp
Print "No
such file."
True DOWHILE
DOWHILE (content(content
NOT = END-OF- False
FILE)
NOT = END-OF-FILE) exit to00
exit to
Read formatted string from fp END
Define fp as "tel.txt" text file
content = Get character from fp
Increase i value Open fp in write mode
True DOWHILE (content NOT = END-OF- False
DO i = FILE)
0 TO size
Define fp as "tel.txt" text file
Display array
Open fp in read mode
write array into fp
True DOWHILE ((content = False
DOWHILE (content NOT from
Get character = END-OF-FILE)
fp)
NOT= END-OF-FILE)
Display content
Close fp
END
Replace function
rep(str, old_num, new_num)
Declare pos as character array
Declare temp as string
Declare pos as character array
Declare length as empty integer
True DOWHILE ((pos = False
old_num
DOWHILE (contentis substring
NOT = END-OF-FILE)
str) NOT = NULL)
copy temp string as str
count = pos - str
END
str[count] = '\0'
concatenate str with new_num
Concatenate (temp + count + length) to str
Comparator function
comparator(str1, str2)
RETURN string comparing (structure employee extension no value) as str1,
(structure employee extension no value) as str2)
3.0 Sample of source code
Header file content:
Main C file content:
Another C file content:
Use structure to make a block of data.
Menu function:
Input function:
The structure of employee will declare as emp and the emp will be an array. It will use in the loop
condition. For example, if user types 5, then loop will stop until five employee details written.
Search function:
Add function:
Update function and replace function:
This program will declare two file pointers, but one of them will be a temporary file. The temporary
file will record the new content then rename as the original file when the original was remove. The
replace function will copy the current line which to require variable. Then, the replace of new word
will place to the current line, next will need to concatenate the rest of the current line data which
include the backup data, the index of old word and the length of old word to complete a new replace
data (C program to find and replace a word in file - Codeforwin, 2021).
*tmpfile() function is defined in the “stdio.h” header file. The temporary file will atomically deleted
after termination of program.
List function and comparator function:
This function will need to read the formatted string in every line then loop in purpose. When comes to
the comparator function, it will compare the string in each of the line specific string. In this case, it
will compare the extension no itself and sort those data in ascending order (Comparator function of
qsort() in C - GeeksforGeeks, 2021).
3.1 Sample of output
Figure 3.1.1
According to figure 3.1.1, if type other than choice from A to E or, a to e, the program will jump
directly to default and end the program.
Figure 3.1.2
When type a, the program will create and write a file name tel.txt, input extension number, name, id,
department name then records into the file.
Figure 3.1.3
The program will need user to input employee name or id to search the information.
Figure 3.1.4
The program able to add the new employee information into the text file.
Figure 3.1.5
The update program will able to search the employee information and input the extension no to renew.
Figure 3.1.6
The sort of program will sort the extension number. Because the department have their representative
number. The end of this program will display and rewrite the sorted data.
4.0 Conclusion
The university will need the telephone operator system to organize the extension no and basic
information of employee. Due to the university still in the education and business scaling phase, the
must be a lot of preparation cannot be ignored.
In the future, the university will be growing and growing for no doubt. This telephone operator
system can be used for more longer than other temporary system because the amount of employee
limitation will be one thousand. The one thousand space can be extended as sub-department. For
example, visa department have a sub-department and fifty employees. The representative digits of visa
sub-department will be same as visa department, but the number behind the only digits will be different.
In addition, there are fewer pitfalls for the program. Firstly, it would be troublesome if the
program cannot remove the employee who no longer work in the company. Secondly, although the
program can be able to put one thousand amounts of employees’ information but it just one text file
there, it would be uneasy to manage the whole bunch of information.
In conclusion, the program was existed both advantages and setbacks. In the future, the program
should be improved more useful features to cover more possible situation. It could be generated more
text file to record the employee information or adding a removing specific data feature, etc. There are a
lot of possibility inside the programming.
5.0 Reference
1. Robertson, L., 2007. Simple program design. Australia: Thomson.
2. It.iitb.ac.in. 2021. [online] Available at:
<https://www.it.iitb.ac.in/~vijaya/ssrvm/dokuwiki/media/s6_l7_16dec.pdf> [Accessed 18 February 2021].
3. Codeforwin. 2021. C program to find and replace a word in file - Codeforwin. [online] Available at:
<https://codeforwin.org/2018/02/c-program-find-and-replace-a-word-in-file.html> [Accessed 18 February
2021].
4. GeeksforGeeks. 2021. Comparator function of qsort() in C - GeeksforGeeks. [online] Available at:
<https://www.geeksforgeeks.org/comparator-function-of-qsort-in-c/> [Accessed 18 February 2021].