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

Assignment 1 Procedural Programming (PROG102)

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 (2.62 MB, 23 trang )

ASSIGNMENT 1 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

Bui Quang Minh

Class

Student ID

GCD1104

GCD210325

Assessor name


Phan Thanh Tra

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
P1

P2

P3

M1

M2

D1


 Summative Feedback:

Grade:
Lecturer Signature:

 Resubmission Feedback:

Assessor Signature:


Date:


Contents
I.INTRODUCTION
1. Scenario .................................................................................................................................. 4
2. Programming language .......................................................................................................... 4
3. Background of C ..................................................................................................................... 4
4. Procedural programming …………………………………………………………………………………………………… 5
5. Key factors .............................................................................................................................. 5
6. User requirement .................................................................................................................... 9
7. Use case diagram .................................................................................................................... 9
8. Advantages and disadvantages of procedural programming .................................................. 9
II.ANALYSIS
1. Date types and date structures ............................................................................................... 10
2. Date types used ...................................................................................................................... 12
3. Condition statement ............................................................................................................... 12
4. condition statement used ....................................................................................................... 14
5. Loop statement ....................................................................................................................... 14
6. Loop statement used .............................................................................................................. 15
III. DESIGN
1. Work breakdown structure ..................................................................................................... 16
2. Flowcharts ............................................................................................................................... 16
3. Review flowcharts ................................................................................................................... 20
4. Finding ID with the grade given before ................................................................................... 20
5. Alternative solution ................................................................................................................. 20
IV. EVALUATION
1. Evaluate my solution .............................................................................................................. 21
2. Evaluate how procedural programming is applied to my problem ........................................ 22



I. INTRODUCTION:
P1
1) Scenario: A math teacher told me that he wants a program that can be used to monitor the grades of
a class. He can enter student IDs and student grades into the program. The former should be stored in an
integer array and the latter should be stored in a float array. When he enters the program, there is a menu
containing four options. The first option would be to enter the student IDs and student grades. The next
option is printing all student IDs with their grades. The third one is printing student who has the highest
and lowest grade. When he gets what he needs, he can go back to the main menu to choose another
option. Moreover, if he wants to turn off the program, he selects the last option: to quit the program.

2) Definition of programming language: A programming language is a computer language that is
used by programmers (developers) to communicate with computers. It is a set of instructions written in
any specific language (C, C++, Java, Python) to perform a specific task. A programming language is
mainly used to develop desktop applications, websites, and mobile applications.
According to Stack Overflow’s 2020 Developer Survey, JavaScript currently stands as the most commonlyused language in the world (69.7%), followed by HTML/CSS (62.4%), SQL (56.9%), Python (41.6%) and Java
(38.4%).

1.1 the most commonly used language in the world 2020

3) Background of C programming language: C programming language was developed in 1972 by

Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A.
Dennis Ritchie is known as the founder of the c language. It was developed to overcome the problems of
previous languages such as B, BCPL, etc. Initially, C language was developed to be used in UNIX
operating system. It inherits many features of previous languages such as B and BCPL.
Features of C Programming Language:


1.2 Features of C Programming Language


4) Definition of procedural programming: Procedural programming is a programming paradigm built
around the idea that programs are sequences of instructions to be executed. They focus heavily on
splitting up programs into named sets of instructions called procedures, analogous to functions. A
procedure can store local data that is not accessible from outside the procedure's scope and can also
access and modify global data variables.
Surprisingly, it can often be broken down into three simple programming structures called sequences,
selections, and loops. These come together to form the most basic instructions and algorithms for all types
of software.
The three basic programming constructs
Programs are designed using common building blocks. These building blocks, known as programming
constructs (or programming concepts), form the basis for all programs.
There are three basic building blocks to consider:
 sequence is the order in which instructions occur and are processed
 selection determines which path a program takes when it is running
 iteration is the repeated execution of a section of code when a program is running

1.3 Three basic building blocks


5) Key factors:
Predefined functions
A predefined function is a function available in a procedural programming language from a library of
available functions. These functions allow a programmer to complete common tasks without creating the
required code themselves. This can help a developer save time during production. One example of a predefined function is sqrt() which returns the square root of a number.

1.4 Example of predefined functions
Local variables
A local variable is a programming variable that has a local scope of use. This means the variable only
functions in the function in which the developer defines it. Local variables only work in this capacity, so

they can cause code to fail, leaving a task unfinished if a professional or user attempts to use the variable
in a method outside of its scope.
Global variables
Global variables increase functionality when local variables are insufficient. Developers can use global
variables in nearly all functions. When defined globally, a variable makes itself available to all methods
and functions in the code, allowing the developer to access key data throughout the program's many
procedures.

1.5 Example of local and global variables


Modularity
Modularity is a structure in which a developer divides the functionality of its code into a series of smaller
blocks. The programmer can then call these blocks, often called methods or functions in procedural
programming languages, in their code to access them.
This makes important functions repeatable to create a more efficient setup code, compared to one that
requires the programmer to reuse the same code at multiple points when including a task that they need
more than once.
Parameter Passing
Parameter passing involves passing input parameters into a module (a function in C and a function and
procedure in Pascal) and receiving output parameters back from the module. For example, a quadratic
equation module requires three parameters to be passed to it, these would be a, b and c.
 Pass By Value: This method uses in-mode semantics. Changes made to formal parameter do not
get transmitted back to the caller. Any modifications to the formal parameter variable inside the
called function or method affect only the separate storage location and will not be reflected in
the actual parameter in the calling environment. This method is also called as call by value.
// C program to illustrate
// call by value
#include <stdio.h>
void func(int a, int b)

{
a += b;
printf("In func, a = %d b = %d\n", a, b);
}
int main(void)
{
int x = 5, y = 7;
// Passing parameters
func(x, y);
printf("In main, x = %d y = %d\n", x, y);
return 0;
}
Output:
In func, a = 12 b = 7
In main, x = 5 y = 7


Pass by reference(aliasing): This technique uses in/out-mode semantics. Changes made to formal
parameter do get transmitted back to the caller through parameter passing. Any changes to the
formal parameter are reflected in the actual parameter in the calling environment as formal
parameter receives a reference (or pointer) to the actual data. This method is also called as call
by reference. This method is efficient in both time and space.

// C program to illustrate
// call by reference
#include <stdio.h>
void swapnum(int* i, int* j)
{
int temp = *i;
*i = *j;

*j = temp;
}
int main(void)
{
int a = 10, b = 20;
// passing parameters
swapnum(&a, &b);


printf("a is %d and b is %d\n", a, b);
return 0;
}
Output:
a is 20 and b is 10

Programming libraries
Programming libraries are a collection of classes, values, and subroutines and they come ready built into
programs. The programming libraries can be used whenever the developer wishes to. Many different
libraries are used in programs however; they will all serve different functions. Many programs will use
libraries in programming and each will be unique to their own program. The line of codes that are used
will different to other programs.
<stdio.h>, <stdlib.h>, <math.h>
Procedures
When you have procedures in programming, the program that has them in will follow the procedures step
by step, systematically. The program does exactly what it is told to do in the order that has been set by
the programmer
Procedure for answering customer calls:
Answer the phone before the third ring
Greet customer by saying, "Hello, you've reached Lead Strategy Incorporated. This is [your name]
speaking. How can I help you today?"

Use positive language
Answer the customer's questions
Ask the customer to please hold while you transfer them
Dial the extension number
Press the send button
Procedural programming paradigm
Procedural programming is derived from structured programming. Procedures, also known as methods,
functions, routines or sub-routines, simply contain a series of computantional steps to be carried out.
Procedures cna be carried out during any point of the program, sometimes other procedures can call out
another procedure during it's cycle of runn
function pourIngredients() {
- Pour flour in a bowl
- Pour a couple eggs in the same bowl
- Pour some milk in the same bowl
}
function mixAndTransferToMold() {


- Mix the ingredients
- Pour the mix in a mold
}
function cookAndLetChill() {
- Cook for 35 minutes
- Let chill
}
pourIngredients()
mixAndTransferToMold()
cookAndLetChill()

6) User quirements:

As a teacher, I want to enter the IDs and grades of my students, so that they are stored in the program
As a teacher, I want to print the student IDs and grades, so that I can see all of the IDs and grades of my
students.
As a teacher, I want to know the student who has the highest and lowest grades, so that I can see the
student who has the highest and lowest grade.
As a teacher, I do not want to use the program, so that I can turn off the program.

M1
1) Use case diagram:

1.4 Use case diagram

2) Advantages and Disadvantages of Procedural Programming
Procedural Programming comes with its own set of pros and cons, some of which are mentioned below.


Advantages:








Procedural Programming is excellent for general-purpose programming
The coded simplicity along with ease of implementation of compilers and interpreters
A large variety of books and online course material available on tested algorithms, making it easier
to learn along the way
The source code is portable, therefore, it can be used to target a different CPU as well

The code can be reused in different parts of the program, without the need to copy it
Through Procedural Programming technique, the memory requirement also slashes
The program flow can be tracked easily

Disadvantages:






The program code is harder to write when Procedural Programming is employed
The Procedural code is often not reusable, which may pose the need to recreate the code if is
needed to use in another application
Difficult to relate with real-world objects
The importance is given to the operation rather than the data, which might pose issues in some
data-sensitive cases
The data is exposed to the whole program, making it not so much security friendly

II. ANALYSIS
P2
1) Date types and data structures:
Date types:
Data Type

Format
Specifier

Minimal Range


Typical
Size

unsigned char

%c

0 to 255

8

char

%c

-127 to 127

8

signed char

%c

-127 to 127

8

int

%d, %i


-32,767 to 32,767

16 or 32

unsigned int

%u

0 to 65,535

16 or 32

signed int

%d, %i

Same as int

Same as int
16 or 32

short int

%hd

-32,767 to 32,767

16


%hu

0 to 65,535

16

signed short int

%hd

Same as short int

16

long int

%ld, %li

-2,147,483,647 to 2,147,483,647

32

long long int

%lld, %lli

-(263 – 1) to 263 – 1 (It will be added by the C99
standard)

64


signed long int

%ld, %li

Same as long int

32

unsigned
int

short

Bit


unsigned long int

%lu

0 to 4,294,967,295

32

unsigned long
long int

%llu


264 – 1 (It will be added by the C99 standard)

64

float

%f

1E-37 to 1E+37 along with six digits of the precisions
here

32

double

%lf

1E-37 to 1E+37 along with six digits of the precisions
here

64

long double

%Lf

1E-37 to 1E+37 along with six digits of the precisions
here

80


2.1 Data types
Data Structures: in C are used to store data in an organised and efficient manner. The C Programming
language has many data structures like an array, stack, queue, linked list, tree, etc. A programmer selects
an appropriate data structure and uses it according to their convenience.
ARRAY
An Array is a sequential collection of elements, of the same data type. They are stored sequentially in
memory. An Array is a data structure that holds a similar type of elements. The array elements are not
treated as objects in c like they are in java.
Imagine you are at a musical instrument store and I tell you to arrange all the keyboards under the
brand Casio at one place one above the other. This sequential collection of records is called an Array. An
array is a sequential collection of elements of the same data type. In our example above, Casio is the data
type and all the keyboards you collected are of the brand Casio. All the elements in an array are addressed
by a common name.
There are two types of arrays:


Single dimensional array



Multidimensional array

//Employee IDs example
#include<stdio.h>
int main()
{
int i=0;



int empID[5];
empID[0]=10280;
empID[1]=10260;
empID[2]=10270;
empID[3]=10285;
for(i=0;i<4;i++)
{
printf("%d n,empID[i]);
}
return 0;
}
//Output:
10280
10260
10270
10285
10275

2) Date types used:
Names of variable

Meaning

Datatypes

IDs

Student IDs

Int


Grades

Student’s grades

Float

Highestgrade

Highest grade

Float

Lowestgrade

Lowest grade

float

3) Condition statement:
If statement
The single if statement in C language is used to execute the code if a condition is true. It is also called a
one-way selection statement. When we use the if condition, we pass the argument and if the argument
will be satisfied then the respective code will be executed otherwise nothing can happen.

If-else statement
The if-else statement in C language is used to execute the code if the condition is true or false. It is also
called a two-way selection statement.



The single if statement may work pretty well, but if you want to work with multiple variables or the
extended conditional parameters, then the if-else statement is the optimum choice. By using the if
statement, only one block of the code executes after the condition is true but by using the if-else
statement, there are two possible blocks of code where the first block is used for handling the success
part and the other one for the failure condition.

If..else If ladder
The if-else-if statement is used to execute one code from multiple conditions. It is also called a multipath
decision statement. It is a chain of if..else statements in which each if statement is associated with an else
if statement and the last would be an else statement.


Switch Statement
switch statement acts as a substitute for a long if-else-if ladder that is used to test a list of cases. A switch
statement contains one or more case labels that are tested against the switch expression. When the
expression match to a case then the associated statements with that case would be executed.
We have seen the way of using conditional statements such as if, if-else. if-else ladder, but the need for
an additional way of dealing with conditional statements may seem unnecessary but based on the certain
usage, switch case was defined to check for the single condition, and based on the multiple cases, code
can be executed.

4) condition statement used:
Condition statement
Switch statement
If statement

Where
Entering the options that he
wants after entering the IDs and
grades

In maxGrade, minGrade
findSameGrade, checkIdExist
functions

Purpose
To run the option what teacher
typed such as viewing all student
or viewing the highest grade
To check the condition in the
round brackets.
Finding max and min in
maxGrade, minGrade function.
Comparing the typed ID and
current ID in checkIdExist
function. Finding the same grades
in findSameGrade function.

5) Loop statement:
While
This loop executes a statement or a block of statements until the specified boolean expression evaluates
to false.


Do-While Loop
This loop executes a statement or a block of statements until the specified boolean expression evaluates
to false.
For Loop
This loop has three sections - index declaration, condition (boolean expression) and updation section. In
each for loop iteration, the index is updated (incremented/decremented) by updating section and checked
with the condition. If the condition is matched, it continues execution until the specified boolean

expression evaluates to false.
While, Do..While and For Loop
A program can be build by using one of the loop statement. For example, we can print numbers from 1 to
100 using all loops.

6) Loop statements used:
Loop statements
Do while
For

Location
When entering including
number of students, IDs,
Grades, and options
When starting a loop such as
entering the ID and grades,
checking typed ID and current
ID, finding min and max

Purposes
Teacher has to enter something
before checking the condition in
round brackets
To repeat many times and save
time


III. DESIGN
P3


MENU

1) Work breakdown structure:
Option 1: enter the
students information

Enter student IDs

Enter student grades

Option 2: View all
students information

Print student IDs

Print students grades

Option 3: View student
with highest grade

Compare grades with
others

Print student with
highest grade

Option 4: View student
with lowest grade

Compare grade with

others

Print student with
lowest grade

Option 5: Exit the
program

Exit program

End program

3.1 Work breakdown structure
Explain: the menu has 5 options or 4 options depending on coder, if I want to combine option 3 and option
4, then the total option number is 4.
Option 1 will be used to enter all the information including IDs, and grades.
Option 2 will print the information typed in option 1. IDs and grades in turn.
Option 3 and 4 will compare and then print students with highest and lowest grades.
Option 5 will be used to exit the program and then end the program completely.

2) Flowcharts:

3.2 Flowchart of menu


3.3 Flowchart of entering the students IDs and grades


3.4 Flowchart of printing the students IDs and grades



3.5 Flowchart of printing the students with the highest grade

3.6 Flowchart of printing the students with the lowest grade


M2
1) Review flowcharts:

 This program no longer has a syntactic or logical error after finishing the basic
procedure.
 In essence, the current version will feature a fully operation and stable
framework that all requirements.
 User, can manage their student information through a single interface
2) finding ID with the grade given before:

3.7 Flowchart of finding ID with the grade given before

3) Althernative solution:
Struct
-

Firstly, property ID and grades belong to one object that is student
We will access the information of students more clearlier without accessing two arrays of
ID and grades.
Solution: we will only create an array which is student and then, when we have a grade,
we just need to access the student array and we will have ID that is corresponding.


IV. EVALUATION

D1
1) Evaluate my solution:
The solutions I did are not optimal and impractical because of duplicate IDs (because IDs should be unique)
so for optimal I have to check for duplicate IDs.

3.8 Flowchart of checking ID typed before


2) Evaluate how procedural programming is applied to my problem:
Pros






Adopted by many general-purpose programming languages.
Enhances the reusability of the code.
Makes the Implementation of algorithms easy.
Simplifies the code and makes it easy to understand.
Tracking the program flow is easy as the same flow linearly.



Data is vulnerable (if we declare lists of ID and grade, they are easy to be accessed and
modified by other functions).
Not much practical for solving real-world problems (Using lists of ID and grade, we do
not know the object of them. For example, we can use struct).

Cons





References
Anon., n.d. What Is The Best Programming Language In The World? With Code Examples. [Online]
Available at: />Ravikian, A. S. 10 Most Important Features of C Language You Must be Aware Of. [Online]
Available at: />Anon., n.d. Computer Science: Sequences, Selections, and Loops. [Online]
Available at: />Anon., n.d. Features of C programming Language. [Online]
Available at: />Bhadwal, A. Procedural Programming. [Online]
Available at: />Anon., n.d. Data Types in C. [Online]
Available at: />Thompson, B. C Conditional Statement. [Online]
Available at: />20by%20the%20condition.



×