Algorithm Design and Implementation: A Practical Guide with C# Examples, Assignments of Programming Languages

define an algorithm, build a program , SDLC, ...

Typology: Assignments

2020/2021

Uploaded on 09/02/2021

UnderLine07
UnderLine07 🇻🇳

4.7

(66)

16 documents

1 / 43

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Qualification
BTEC Level 5 HND Diploma in Computing
Unit number and title
Unit 1: Programming
Submission date
5/8/2021
Date Received 1st submission
Re-submission Date
Date Received 2nd submission
Student Name
Tran Doan Dung
Student ID
GCH200619
Class
GCH0908
Assessor name
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
Dzung
Grading grid
P1
M1
D1
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

Partial preview of the text

Download Algorithm Design and Implementation: A Practical Guide with C# Examples and more Assignments Programming Languages in PDF only on Docsity!

Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 1: Programming Submission date 5/8/2021 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Tran Doan Dung Student ID GCH Class GCH0908 Assessor name 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 Dzung Grading grid P1 M1 D

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date: Lecturer Signature:

An example of an algorithm: Find the maximum value of a 10 - element integer sequence entered from the keyboard. First of all, we need to understand the definition of Input and Output of the above problem.

  • Input: 10 integers entered from the keyboard
  • Output: The largest maximum value of the sequence of numbers. Algorithm: Step 1: START Step 2: DECLARE MAX=0, Array N [10] Step 3: SET variable i= Step 4: While i < length of array N, repeat step 5 Step 5: READ N [i] Step 6: SET variable i= Step 7: WHILE i < n is true, repeat step 5 Step 8: IF N[i] >MAX is true then Step 8.1: SET MAX= N[i] Step 9: PRINT Max Step 10: STOP After using the above algorithm, the answer to the main problem is the variable MAX. As seen above, in order to solve the problem, the algorithm needs to try a few factors as follows:
  • Definite
  • Finite
  • Precise and Effective
  • Implementation independent (only for problem not for programming languages) 2.2 Characteristics of Algorithm To fully understand the meaning of the concept of algorithms, I will introduce the following five characteristics of algorithms (D.E. Knuth [1968] The Art of Computer Programming, vol. I. Fundamental Algorithms). (D. E, 1968)
  1. Input: Each algorithm needs a price (which may or may not have an initial value) input data (input). Those are the values to enter when the algorithm starts working. For example, in the algorithm example I just showed above, the input is an array of N of 10 numbers entered from the keyboard.
  2. Output: Each algorithm needs one or more outputs also known as Outputs. Those are values that have a completely definite relationship with the input data and are the results of the algorithm after performing all the steps. In the algorithm above, Output is the largest number of those digits.
  3. Determination: Each step of the algorithm needs to be described in a short, easy to understand and unique way. Obviously, this is a step that requires absolute precision. Because, if a step can be interpreted in different ways, then different people implementing the algorithm can lead to different results. If we describe the algorithm in ordinary language, it can happen that the user of the algorithm does not understand the description of the algorithm writer. To ensure this requirement, the algorithm needs to be described in programming languages (machine language, assembly language or high-level language such as C, Python, C#, ...). In these languages, clauses are formed according to uniform syntactic rules and have only one meaning.
  4. Feasibility: All operations present in the steps of the algorithm should be simple enough. That means the operations must be at least in principle so that the user can perform them easily in a finite amount of time.

The following diagram will help us understand more about the requirements of the problem: Figure 2 - Operation diagram of the program To solve this problem, I will use C# language in this program. In addition, I will apply the experience I have gained from Procedural Programming to write programs that are easiest to understand. I will demonstrate how I include the libraries, variables, conditional expressions, and loops that I use to write the program.

3.1.1 Library

In this program I plan to use 2 libraries to meet the needs of the problem. That is:

1. using System;

Basic library of a Console Application, using commands like Console.WriteLine(), Console.ReadLine(), conditional structure, loops, etc.

2. using System.Collections.Generic;

Library to use List in C#

3.1.2. Variables and data types

To solve this problem, I plan to use 3 main data types string, integer and double. Where variables using integer data type will be used for the user's selection in the menu(choice), data type double will be used for the number of students (number), the total score of the students (sum), the total number of students passing the course (count), the intermediate variable to sort the student's score (compare) and the student's score (grade). And finally, the variables that will use the string data type are: the student's name (name), the student's ID (ID), the intermediate variable to change the student's ID according to the score arrangement (sid). To be clearer, I will create a table of the set of variables I use in this program. Data Type Variable Function Integer choice Get user selected silk Double number Declare the number of students sum Calculate the total score of the student count Calculate the number of students who pass the course compare The intermediate variable to sort the student's score grade Declare students’ grade String name Declare the name of the student

3.1.3 Conditional statements and loop statements

To be able to use the features without having to re-enter the data, I will put the features in the “Switch - Case” statement into a “do – while” loop for a repeatable Menu control. program functions for the user to choose from. In addition, I also designed an option to help users exit the program. If the user enters

incorrectly, the program will print the message: “Wrong choice” 3.1.3.1 Conditional statements

Figure 4 - Menu Control

During the execution of the program, in order to avoid the user entering the wrong number of students and the student's grades. I will use an additional if statement to check the data entered by the user. I plan to use the following: Figure 5 - Condition of number When the user enters the number of students less than or equal to 0, there will be no students initialized in the value domain of number, so I have set the condition for the number to be greater than 0, the program will not force people. use re-enter Figure 6 - Condition of grade

Figure 8 - Print Student’s Information (Don't Remove List) As in the two images above, the program mistakenly printed information about student Linda's name and ID for student David. Meanwhile, student David's name and ID information is pushed down to student Kate. If the student's ID and name are not deleted, they are still stored in the List. Therefore, when printing List to the screen, it will lead to the phenomenon that the number of times the information is pushed down and the amount of information lost will be equal to the number of incorrect entries of the user's score. You

can refer to the following chart: Diagram 1 - Operation error of the program Since the variable “-7” has been deleted, the program will print out the information in the area bounded by the black line. As can be seen, List name and List ID still receive user input, but that information will not be displayed because the program only prints to Index 2 in this case. Regarding loops in the program, here, I mainly use 2 types of loops, for loops and do-while loops. Here, I will explain about loops in the Main function of the program. As for the loops in Functions, when I come to any Function, I will explain in detail in that Function.

3.1.3.2 Loop statements

  • Input number students:

Flowchart: Diagram 2 - Input number student Algorithm (Pseudo code):

  1. START
  2. DECLARE the variables number
  3. REPEAT
  4. READ the valuables number
  5. IF number<=0 is true then 5.1 PRINT: Invalid number
  6. UNTIL number>
  7. SET the variables number
  8. STOP
    • Input student’s information:

Figure 10 - Input student's number Description: When starting to run, the program declares the variable 'i' with integer data type to start running the for loop. The program first checks the condition of the variable 'i'. If the variable 'i' is less than the variable 'number', the program will enter a do=while loop to let the user enter information. Here, the user needs to enter 3 types of information that are the name, ID, grade of each student and the variables respectively:

Algorithm (Pseudo code):

  1. START
  2. DECLARE the variables name, grade, ID and List names, IDs, grades
  3. WHILE i < number true, repeat from step 4 to step 7
  4. REPEAT
  5. READ variable name, grade, ID
  6. ADD name to List names, ID to list IDs, grade to list grades
  7. IF grades[i]<0 || grades[i]>10 is true then 7 .1 PRINT Invalid values 7 .2 REMOVE name to List names, ID to list IDs, grade to list grades
  8. UNTIL 0<=Grades[i]<= 9 .. STOP
  • Menu function control: Figure 11 - Menu function control Description: First, when running the program, it will print out a Menu for the user to choose the functions of the program. The program will then let the user enter a value into the variable "choice". If the value matches up to [1:5], the program will execute. If the user enters incorrectly, the program will run the default case