1. Trang chủ
  2. » Công Nghệ Thông Tin

Assignment 1 Procedural Programming (PROG102)

23 11 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Procedural Programming
Tác giả Bui Quang Minh
Người hướng dẫn Phan Thanh Tra
Trường học Ha Noi University of Science and Technology
Chuyên ngành Computing
Thể loại assignment
Năm xuất bản 2023
Thành phố Hanoi
Định dạng
Số trang 23
Dung lượng 2,62 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

ASM 1 For Procedural Programming, this document is useful for BTEC students including FPT or Greenwich. When you submit this document, you will pass the course right away. If you want to own more documents about computing or all course in school, please contact me to get more. Hope it useful to you

Trang 1

ASSIGNMENT 1 FRONT SHEET

Unit number and title Prog102: Procedural Programming

Trang 2

 Summative Feedback:  Resubmission Feedback:

Lecturer Signature:

Trang 3

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

Trang 4

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 used 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:

Trang 5

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

Trang 6

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 pre-defined 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

Trang 7

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

by reference This method is efficient in both time and space

Trang 8

<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() {

Trang 9

- Mix the ingredients

- Pour the mix in a mold

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

Trang 10

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

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

Trang 11

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

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

Trang 12

2) Date types used:

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

Trang 13

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

Trang 14

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:

Switch statement Entering the options that he

wants after entering the IDs and

grades

To run the option what teacher typed such as viewing all student

or viewing the highest grade

Trang 15

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:

number of students, IDs, Grades, and options

Teacher has to enter something before checking the condition in

round brackets

entering the ID and grades, checking typed ID and current

ID, finding min and max

To repeat many times and save

time

Trang 16

III DESIGN P3 1) Work breakdown structure:

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

program Exit program End program

Trang 17

3.3 Flowchart of entering the students IDs and grades

Trang 18

3.4 Flowchart of printing the students IDs and grades

Trang 19

3.5 Flowchart of printing the students with the highest grade

3.6 Flowchart of printing the students with the lowest grade

Trang 20

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

-

Trang 21

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

Ngày đăng: 26/03/2023, 10:51

TỪ KHÓA LIÊN QUAN

w