Assignment 1 1649 (m), Assignments of Information Technology

Assignment 1 1649 who want to merit

Typology: Assignments

2022/2023

Uploaded on 11/17/2023

unknown user
unknown user 🇻🇳

16 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT 1 FRONT SHEET
Qualification
BTEC Level 5 HND Diploma in Computing
Unit number and title
Unit 19: Data Structures and Algorithms
Submission date
02-11-2023
Date Received 1st
submission
02-11-2023
Re-submission Date
Date Received 2nd
submission
Student Name
Nguyễn Văn Quang
Student ID
Gch211372
Class
Gch1107
Assessor name
Do Hong Quan
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
M3
D1
D2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download Assignment 1 1649 (m) and more Assignments Information Technology in PDF only on Docsity!

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 19 : Data Structures and Algorithms Submission date 02 - 11 - 2023 Date Received 1st submission 02 - 11 - 2023 Re-submission Date Date Received 2nd submission Student Name Nguyễn Văn Quang Student ID Gch Class Gch1107 Assessor name Do Hong Quan 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 M3 D1 D

Summative Feedback:Resubmission Feedback: Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:

  • the structures. P1 Create a design specification for data structures explaining the valid operations that can be carried out on
    • I.ADT
    • II.Stack
      • a, Overview stack operations
      • b, push(int data) function
      • c, pop() function....................................................................................................................................................
      • d, peek() fuction
      • e. isEmpty() function
      • f. size() function
      • g. search(int data) function
  • P1 + M1 Illustrate, with an example, a concrete data structure for a First In First out (FIFO) queue. - a, endQueue(int number) function - b. deQueue()........................................................................................................................................................ - c. isEmpty()......................................................................................................................................................... - d, getFront() - e.search(int data)
  • P3 Using an imperative definition, specify the abstract data type for a software stack.
    • VDM). I. Introduction to formal specification, types of formal specification languages (E.g., Axiomatic specifications,
      • a, formal specification.........................................................................................................................................
      • b, types of formal specification languages
    • II. Describe what are pre-condition, post-condition, error-condition.
    • III. Specify Stack’s operations using this formal specification language.
  • M2 Sorting Algorithms
    • I. Introduction
    • II.selection
    • III. quick sort
  • M3 Examine the advantages of encapsulation and information hiding when using an ADT.
    • I.Introduction of encapsulation
  • Reference
  • Figure 1: Abstract data type (Javatpoint, 2023)
  • Figure 2:for example of stack (Javatpoint, 2023)
  • Figure 3: push method of stack
  • Figure 4: pop method of stack
  • Figure 5: peek method of stack
  • Figure 6: isEmpty method of stack
  • Figure 7: size method of stack
  • Figure 8:search method of stack
  • Figure 9:queue image (Geeksforgeeks, 2023)
  • Figure 10:enQueue method of stack
  • Figure 11: deQueue method of stack
  • Figure 12: isEmpty method of stack
  • Figure 13: getFont method of stack
  • Figure 14:search method of stack
  • Figure 15: stack memory (Sciencedirect, 2023)..........................................................................................................
  • Figure 16:function calls in a computer
  • Figure 17: Sourse code of recursively calculate the sum............................................................................................
  • Figure 18: Caculate example above
  • Figure 19: Memory stack example 1...........................................................................................................................
  • Figure 20:Memory stack example 2............................................................................................................................
  • Figure 21:Pseudocode of selection sort
  • Figure 22:Flowchart of selection sort
  • Figure 23:Example of selection sort (1)
  • Figure 24:Example of selection sort (2)
  • Figure 25:Example of selection sort (3)
  • Figure 26:Example of selection sort (4)
  • Figure 27:Source code of selection sort
  • Figure 28:Result of selection sort
  • Figure 29:Pseudocode of quick sort
  • Figure 30:Flowchart of quick sort
  • Figure 31:Flowchart main of quick sort
  • Figure 32:Example of quick sort (1)
  • Figure 33:Example of quick sort (2)
  • Figure 34:Example of quick sort (3)
  • Figure 35:Example of quick sort (4)
  • Figure 36:Source code of quick sort
  • Figure 37:Result of quick sort
  • Figure 38:Using Accessors and Mutators
  • Figure 39:Using Properties in Java
  • Figure 40:Read-Only Property in Java.........................................................................................................................
  • Figure 41:Write-Only Property in Java........................................................................................................................

Stack works on the LIFO pattern. As we can observe in the below figure there are five memory blocks in the stack; therefore, the size of the stack is 5. Suppose we want to store the elements in a stack and let's assume that stack is empty. We have taken the stack of size 5 as shown below in which we are pushing the elements one by one until the stack becomes full. Since our stack is full as the size of the stack is 5. In the above cases, we can observe that it goes from the top to the bottom when we were entering the new element in the stack. The stack gets filled up from the bottom to the top. When we perform the delete operation on the stack, there is only one way for entry and exit as the other end is closed. It follows the LIFO pattern, which means that the value entered first will be removed last. In the above case, the value 5 is entered first, so it will be removed only after the deletion of all the other elements. a, Overview stack operations

  • push (int data): Add an item of data type int to the top of the stack.
  • pop (): Remove an item from the top of the stack and return its value.
  • peek (): Get the value on the top of the stack.
  • isEmpty (): Checks if the stack has an item, returns true if true, returns false if false.
  • size() to return the current number of elements in the stack.
  • search(int data) to find the LAST OCCURRENCE of the given data in the stack. b, push(int data) function

Push(int data) :void

Purpose of function Add a new element into the top of the stack Parameters A new element with value is data Return Because the function is of type void, it does not return any value Adds a new element to the top of a stack implemented as a linked list. If the stack is empty, it sets the new element as the top. If the stack isn't empty, it links the new element to the current top and updates the top. The size keeps track of the number of elements.

Figure 3 : push method of stack Explanation of figure 3: Initially, we check if the top is null. Because there are two elements, it means we need to add to the beginning of the list with the parameter value of 7, and as a result, we get a new value of 7 at the top. As can be seen in the illustrated image. c, pop() function

pop() :Integer

Purpose of function Delete the element at the top of the stack Parameters No parameters Return After deleting, the function will return the deleted value The pop() function checks whether the stack is empty or not, if empty, returns null, if there is an element, deletes that element from the stack, the top element after deletion will be the element below. Returns the deleted value. Figure 4 : pop method of stack Explanation of figure 4 : First check if the list is empty which will return null. Currently the list has 3 elements, the first element will be deleted and the data returned is 2.

Figure 6 : isEmpty method of stack Explanation of figure 6 : The first image in the list has 2 elements so it will return false. In the next image, the list is null so it will return true f. size() function

size(): int

Purpose of function Returns the total number of elements in the list Parameters No parameters Return If the element does not have a list, return 0. If it does, return the number of elements counted by count Figure 7 : size method of stack

Explanation of figure 7 : The first image, when the list is empty, will return 0, and the next image with 3 elements will return the value 3, which can be seen through the illustration. g. search(int data) function

search(): int

Purpose of function When you have data and want to find its location Parameters The parameter is the value that I want to search for, the result is index Return Returns the index that corresponds to the input data The function will return - 1 if the corresponding data is not found or the list is empty. The function will return the index if there is valid search data by linear search running through each element. If equal, immediately return the index and stop the program. Figure 8 :search method of stack Explanation of figure 8 : At first check, if there are no elements or the list is null, return - 1. Currently the list has 3 elements and the argument passed to the search function is 1. We can see that with the value 1, the index is also 1 so it will return the value 1. Application of Stack Data Structure: (GeeksforGeeks, 2023)

  • Function calls and recursion: When a function is called, the current state of the program is pushed onto the stack. When the function returns, the state is popped from the stack to resume the previous function’s execution.
  • Undo/Redo operations: The undo-redo feature in various applications uses stacks to keep track of the previous actions. Each time an action is performed, it is pushed onto the stack. To undo the action, the top element of the stack is popped, and the reverse operation is performed.
  • Expression evaluation: Stack data structure is used to evaluate expressions in infix, postfix, and prefix notations. Operators and operands are pushed onto the stack, and operations are performed based on the stack’s top elements.

Figure 9 :queue image (Geeksforgeeks, 2023) Overview Queue operations enQueue(int data): Push an item of data type Element to the end of the Queue (rear). deQueue(): Removes the first element of the Queue (front) and returns that value if deletion succeeds, returns null if deletion fails. isEmpty(): Checks whether the Queue has an element, returns true if the Queue is empty, returns false if the Queue has an item. getFront(): Gets the front element of the Queue without removing it, returns front on success, null on failure. search(int data): Through value find the corresponding index. To illustrate Queue operations, a scenario will be shared for operations. Scenario (Practical example): In schools, bags are distributed to new students and connections are arranged according to student number. For example, a student who has finished retrieving goods, corresponding to the removal and addition of a new student at the same level, is placed last by and is considered as adding a new element. a, endQueue(int number) function enQueueint number) :void Purpose of function Add a new element into the rear of the stack Parameters A new element with value is number

Return Because the function is of type void, it does not return any value The item of type int will be put to the back of the queue by the enQueue function, which will also push down the previous back position. The object being pushed is both front and back if the Queue is empty. Figure 10 :enQueue method of stack Explanation of figure 10 : Currently there are two students in the list and now there will be more students sorted and displayed according to each member's id number. With enQueue(2122) as argument representing the value to add to the end of the row. b. deQueue()

deQueue():Integer

Purpose of function Delete the element at the top of the stack Parameters No parameters Return After deleting, the function will return the deleted value The item at the front of the queue is deleted using the deQueue function, which also returns the item in the event that the deletion is successful. Null is returned if delete fails for any reason, such as an empty queue. The front position will be the item after deletion.

If successful, the getFront function will return the front value of the queue; if the queue is empty, it will return null. The Queue remains unchanged by the function. Figure 13 : getFont method of stack Explanation of figure 1 3: The getFront() function returns the student's top id number. If the list is empty, it returns null. In the illustration, you can see that when calling the function, it will return 2120. e.search(int data)

search(): int

Purpose of function When you have data and want to find its location Parameters The parameter is the value that I want to search for, the result is index Return Returns the index that corresponds to the input data The function will return - 1 if the corresponding data is not found or the list is empty. The function will return the index if there is valid search data by linear search running through each element. If equal, immediately return the index and stop the program. Figure 14 :search method of stack Here are some of the common application of queue in data structure: (Kochar, 2023)

  • Queues can be used to schedule jobs and ensure that they are executed in the correct order.
  • Queues can be used to manage the order in which print jobs are sent to the printer.
  • Queues are used to implement the breadth-first search algorithm.
  • Queues can be used to manage incoming calls and ensure that they are handled in the correct order.
  • Queues can be used to manage the order in which processes are executed on the CPU.
  • Queues can be used for buffering data while it is being transferred between two systems. When data is received, it is added to the back of the queue, and when data is sent, it is removed from the front of the queue. Application of Queue in Operating System: (Kochar, 2023)
  • They can be used for algorithms like First Come First Serve(FCFS).
  • It is used as a buffer for input devices like the Keyboard.
  • It is also used for CPU Scheduling.
  • It is widely used for spooling in Printers.
  • Queues are also used in Memory Management.

P2 Determine the operations of a memory stack and how it is used to implement

function calls in a computer.

In programming, whenever a function is called, the program generates a new stack memory block for the function to utilize. This type of memory is located in the stack section of a program’s memory space, which is a reserved restricted memory area. This section is usually located at the top of the memory space and grows downward as more data is added. Stack memory employs an automatic allocation and deallocation of memory that stores temporary data created by functions or procedures. Stack memory uses a “Last In, First Out” (LIFO) data structure, meaning that the most recently added item is the first to be removed. When the function or procedure is finished executing, the stack memory block is released automatically, and the program returns to the previous point of execution (Sciencedirect, 2023)

from the calling function, which allows the computer to know where to resume execution when the called function completes. Function Execution: The called function executes, using the data stored in its stack frame for local variables and parameters. It can also make its own function calls, which create additional stack frames for those functions. Popping Data After Function Call: After a function finishes its execution, the data associated with that function call is popped from the stack. This removal of the stack frame ensures that memory is properly managed and that control returns to the correct point in the calling function. Nested Function Calls: Function calls can be nested within one another, creating a stack of stack frames on the memory stack. This allows functions to call other functions, and when each function completes, its stack frame is removed from the top of the stack, allowing the program to return to the previous function in the call chain. Return to the Caller Function: The return address stored in the stack frame points to the instruction in the calling function where control should resume. This ensures that execution continues in the correct context. Figure 16 :function calls in a computer For example:

Figure 17 : Sourse code of recursively calculate the sum Figure 18 : Caculate example above Explanation of figure 17 and 18: sum(3) is called with num equal to 3 and starts a frame on the stack for the sum function with num as 3. In this frame, the function checks if num is equal to 1. Since num is 3, the function enters the else block and calls sum(2). Next, another frame for sum(2) is created on the stack with num as 2. The sum(2) function also checks if num is equal to 1. Since num is 2, the function enters the else block and calls sum(1). Another frame for sum(1) is created on the stack with num as 1. The sum(1) function does not make any further recursive calls and reaches the base case. It returns 1. The result from sum(1) (which is 1) is computed, and the frame of sum(1) is removed from the stack. Back to the frame of sum(2) with the result from step 7 (which is 1), it calculates 2 + 1 = 3, and the frame of sum(2) is also removed from the stack. Returning to the frame of sum(3) with the result from step 8 (which is 3), it calculates 3 + 3 = 6. Finally, the frame of sum(3) is also removed from the stack, and the final result is 6. During this process, each recursive call creates a new frame on the stack, and when it reaches the base case, the frames are removed from the stack in a Last-In-First-Out (LIFO) manner.