Tải bản đầy đủ (.pdf) (37 trang)

PRO102 ASSIGNMENT 2 DISTINCTION KÈM FILE CODE

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.61 MB, 37 trang )

ASSIGNMENT 2 FRONT SHEET
Qualification

BTEC Level 5 HND Diploma in Computing

Unit number and title

PROG102: Procedural Programming

Submission date

Date Received 1st submission

Re-submission Date

Date Received 2nd submission

Student Name

Tran Duc Long

Student ID

GCH210562

Class

GCH1106

Assessor name


Dinh Duc Manh

Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature
Grading grid
P4

P5

M3

M4

D2

long


❒ Summative Feedback:

❒ Resubmission Feedback:
2.1

Grade:
Lecturer Signature:

Assessor Signature:


Date:


Table of Contents
ASSIGNMENT 2 FRONT SHEET .............................................................................................................................. 1
A- Introduction ............................................................................................................................................................. 5
B- Task 1: Code explanation and screenshot ................................................................................................................ 6
I. Implementation (P4, M3) ..................................................................................................................................... 6
1. Libraries in my program. .................................................................................................................................. 6
2. Data types in my program ................................................................................................................................. 6
3. Function to create userLogin. ........................................................................................................................... 7
4. Function to check whether a character is alphanumeric. .................................................................................. 9
5. Function to input integers and float. ............................................................................................................... 10
6. Function to enter student’s information. ......................................................................................................... 11
7. Function to enter and check number of student. ............................................................................................. 12
8. Function to print captions student's list and print students. ............................................................................ 13
9. Function to show student's list. ....................................................................................................................... 13
10. Function to arrange students by id. ............................................................................................................... 14
11. Function to add a new student in the list....................................................................................................... 14
12. Function to search student by id. .................................................................................................................. 15
13. Function to search student grades. ................................................................................................................ 15
14. Function to find max and min. ...................................................................................................................... 16
15. The main - menu function. ............................................................................................................................ 17
16. Algorithms in my program: ......................................................................................................................... 20
17. Rules for naming functions, variable names in my program ........................................................................ 21
II. Program results (P4).......................................................................................................................................... 22
C- Task 2: Testing (P5, M4) ....................................................................................................................................... 25
I. Testing ................................................................................................................................................................. 25
1.Test userLogin ................................................................................................................................................. 25
2.Test id .............................................................................................................................................................. 26

3.Test grade......................................................................................................................................................... 26
4.Testing program in console screen .................................................................................................................. 26
5.Add a new student............................................................................................................................................ 27
6.Find student by id ............................................................................................................................................ 28
7.Find student by grade ....................................................................................................................................... 28


8.Find the highest score ...................................................................................................................................... 29
9.Find the highest score ...................................................................................................................................... 29
10.Show student’s list ......................................................................................................................................... 29
11.Exit program .................................................................................................................................................. 30
II. Test plan: ............................................................................................................................................................ 31
1. Test input: ....................................................................................................................................................... 31
2. Test function: .................................................................................................................................................. 32
2. Overall evaluation of testing part.................................................................................................................... 33
D- Task 3: Evaluation (D2) ........................................................................................................................................ 34
Overall evaluation of the program. ......................................................................................................................... 34
1.Strength: ........................................................................................................................................................... 34
2.Weaknesses: ..................................................................................................................................................... 34
3.Future improvements: ...................................................................................................................................... 34
Evaluation procedural programming in my program.............................................................................................. 35
Advantages: ........................................................................................................................................................ 35
Disadvantages: .................................................................................................................................................... 35
Quote: ................................................................................................................................................................. 35
E- Conclusion: ............................................................................................................................................................ 36


A- Introduction

We live in a modern society where everything is modern and innovative. Every day, there is a

lot of innovation, especially in technology. When it comes to technology, we must admit that it has
permeated every aspect of our lives, from simple devices such as phones and computers to
complex programming applications.
In this assignment, I will discuss student mark management software, specifically how I can
use it, detailed explanations of its functions and characteristics, and finally an evaluation of the
functions and the overall program. In addition, I created 7 options in my program for users to
easily select, and each option has its own function and information to create a variety of programs.
These functions will assist the user in entering student information such as grade or id, and the
program will also assist the user in determining which students have the highest and lowest grades,
as well as statistics on how many students have good and bad grades.


B- Task 1: Code explanation and screenshot
Scenario:
A math teacher wants to manage grades of a class. He asks you to help him to write a small
application to do that. He needs to enter student IDs, student’s grades and store these
information into 2 separate arrays (integer array for IDs and float array for grades). Then he
needs to print all student IDs together with their grades. Finally, he needs to know which
student has highest grade and lowest grade. Your program should be menu based with the
options above. When an option is done, the program should go back to the main menu so he
can choose another option. There should be an option to quit program.

I. Implementation (P4, M3)
1. Libraries in my program.
Everyone is aware that there are numerous libraries available in C. And in my program, I used 6
libraries in C to create a student management program, which I will explain in detail below.

2. Data types in my program
In my program the following data types are used:
-Enum: is a user-defined data type. It is used mainly to assign names to constants, names that make

a program easy to read and maintain. And I use this type to declare constants:

- Struct is a user defined datatype that allows you to store different types of elements. And I use
this type to declare student’s information.

-int: is integer type

-float: is an number type


-char: is character type

3. Function to create userLogin.
I create a subfunction: userLogin(char username[],char password[])
In this case, I use the specifier to name the ASCII keyboard input keys:

I use while loop purpose when user enter wrong account can re-enter.
In password, I use getch(); to read characters one by one enter from the keyboard
Then I use "if else if" statements to check the condition:
+If "ch==enter" will finish entering password.
+If "ch==BKSP" will delete a character just entered from the keyboard
+If "ch==TAB" or "ch==SPACE" then continue typing
If it does not fall into the above cases, the asterisks will be printed
Next step I use the if else statement to check the username and password:
I use the string function “strcmp''(strcmp in library string.h) to compare two strings of username
and password. If username matches "duclong" and password matches "3907", the login is
successful and if incorrect, it will be re-entered.




4. Function to check whether a character is alphanumeric.
I create a function inputCheck(char key[]);
Here, I use enum data type:

In the inputCheck function, I declare variables res, isFLoat, isChar, size=strlen(key[]).(strlen
compares string length in string.h library)

Then I use a for loop to check the character. In "for" I use "if else" statement to check the
condition "!isdigit(key[i])"
if ''key[i] =='.' then increment isFloat by one unit if false isChar increments by unit
Next I use the "if else if" statement to check the condition:
If the input string has more than one '.' then it's a string
If the input string has a '.' then it is a real number


And if there is no '.' then it is an integer

5. Function to input integers and float.
I create function inputInteger() and inputFloat to input integers and real numbers
● inputInteger():
I declare input array, read array and use while loop to input integer
condition in while: I call the function inputCheck(input) which I talked about in section 4
If the input character is not a number, it will be re-entered


● inputFloat():
I declare input array, read array and use while loop to input integer
condition in while: I call the function inputCheck(input) which I talked about in section 4
If the input character is not a number, it will be re-entered


● Note: +atoi function(in library stdlib.h): convert the header if possible in the string input to
an integer of type int. If the conversion is not possible, the value 0 will be returned. Any
leading whitespace character “input” will be ignored.
+atof function(in library stdlib.h): convert the beginning if possible in the string input to a
float. If the conversion is not possible, the value 0 will be returned. Any leading whitespace
character “input” will be ignored.

6. Function to enter student’s information.
I create a function: enterStudent() to enter student’s information.


Here, I use struct data type to declare variables equivalent to student's id, name and grade:

In the student input function, I call the two functions mentioned in section 5, inputInteger() and
inputFloat() to input id and grade.

Note:+ fflush(stdin) function to clear cache.
+*c: command to remove line drift
+39[^\n]: because the array has 40 characters but I only declared 39 characters to leave one \0 in
the string

7. Function to enter and check number of student.
I create a function: enterGroups(struct Student student[], int n)
This sub function has a function to check a string when entered
I use a for loop to enter the students in order from 1.


In the loop, I call the enterStudent() function created in the section 6 and equalize the student at
position “i” and then check the condition.


8. Function to print captions student's list and print students.
I create 2 sub function: printCaptions() and printStudent(struct Student sv) to print caption of list
and print student’s information

9. Function to show student's list.
I create a function: printGroup(struct Student student[], int n)
Here I call the printCaption() function mentioned in item 8 to print the title of the list table.
I use a for loop to iterate
In the loop, I call the printStudent() function mentioned in section 8 to print out all the students in
the list entered from the keyboard.


10. Function to arrange students by id.
I create a function: arrangeStudents(struct Student list[], int n)
I use two nested for loops to survey from the beginning to the end and from the end to the
beginning.
In loop: I use if statement to check condition
if the student id at the front position is greater than the id of the student at the back position, the
two students will be swapped.

11. Function to add a new student in the list.
I create a sub function: addStudentToGroup(struct Student students[],int n, struct Student student)
I call enterStudent() (section 6) to enter new student information
if a new student is added to the list, the number of students will increase by one


12. Function to search student by id.
I create a function: searchID(struct Student students[], int n, int id)
I call the function printCaption() (section 8 ) to print the title
I then use a for loop to find that student id

In for, I use "if" statement to check
If the student id entered is the same as the student id at position "i", show that student on the
screen.

13. Function to search student grades.
I create a function: searchGrade(struct Student students[], int n, int id)
I call the function printCaption() (section 8 ) to print the title
I then use a for loop to find that student id


In for, I use "if" statement to check: if the student’s grade entered is the same as the student’s
grade at position "i", show that student on the screen.

14. Function to find max and min.
I create 3 a function to find max and min:
searchMaxGrade(struct Student students[], int n),
searchMinGrade(struct Student students[], int n).
printStudentByGrade(struct Student students[], int n, float grade)
+Find student with the highest grade:
I use for loop to search
In loop i use if statement to check if condition:
max < students[i].grade then max = students[i].grade then returns max

+Find student with the lowest grade:
I use for loop to search


In loop i use if statement to check if condition:
min > students[i].grade then min = students[i].grade then returns min


+ Print the student with the same highest or lowest grade score:
Here I continue to call the function printCaptions() (section 8) to print the title
Then I use a for loop to print out the students with the same score

15. The main - menu function.
The main part of program is to display the main menu of the program this give user choices Leads
to subroutines that perform user choice. Thanks for "do....while" loop, after user choose 1 in 7
choices the program will repeat to help users choose another choices. If user want to quit program,
they just only choose choice 0. If the user enters a number that is not in the allowable amount, the
program displays an "invalid “error and forces the user to enter it again.


In the main function I declare variables and in turn call the functions declared in the previous
sections: userLogin() -section 3, enterGroups()-section 6.
Then I use a do...while loop to repeat the menu for the user, I'll leave choice !=0 if the user wants
to exit the program press 0.
In the loop I will use the switch statement to create the menu cases for the user:
+case 0: Exit program
+ case 1:
I call the functions: addStudentToGroup()-section 11 , arrangeStudent()- section 10 , printGroupsection 9 respectively, to create a new student in the list.
Then I use break command to avoid slipping to the following cases.
+case 2:
I call the functions: inputInteger(), searchID() to find student by id.
Then I use break command to avoid slipping to the following cases.
+case 3:
I use "do while" loop to check the point condition, if the user enters a number greater than 10 or
less than 0, it will re-enter it.
Then I use break command to avoid slipping to the following cases.
In loop: I call function: inputFloat() to find student by grade
+case 4:

I call the functions: searchMaxGrade() and printStudentByGrade() respectively to find student
with highest. Then I use break command to avoid slipping to the following cases.
+case 5:
I call the functions: searchMinGrade() and printStudentByGrade() respectively to find student with
lowest. Then I use break command to avoid slipping to the following cases.
+case 6:
I call the function: sprintGroup() to show student’s list. Then I use break command to avoid
slipping to the following cases.



16. Algorithms in my program:
The bubble sort algorithm now sorts the sequence of numbers by repeating the task of swapping
two consecutive numbers if they are in the wrong order (in the case of ascending sort, the latter is


less than the former) until the sequence is sorted numbers. The implementation of the simple
algorithm is as follows:
there are two nested for loops and run variables i and j.
let "i" run from 0 to n-2 , let "j" run from n-1 to i+1. If the element at position j is smaller than the
element at position j-1 then swap places for those two elements.
Swap algorithm for two elements a and b:
+store the value of a in a temporary variable
+assign the value of b to a
+assign the value of the temporary variable to b
and I use this algorithm to sort students by id number ascending which I covered in section 10 .

17. Rules for naming functions, variable names in my program
My functions and variables are named according to camel rules. First letter not capitalized start
capitalizing from 2nd word. Doing it this way will make my code cleaner, easier to remember.

Some examples in my program:

In addition to camel naming, I also use comments to annotate to help me recall the function of each
function. Examples in my program:


II. Program results (P4)




C- Task 2: Testing (P5, M4)
I. Testing
1.Test userLogin
We must enter the username and password to access the program. If you enter it incorrectly, the
program will allow you to re-enter it until you enter it correctly, at which point you will be able to
access the data you require.


×