Uploaded by Nhật Quang Nguyễn

Assignment Programing 1

advertisement
ASSIGNMENT 1 FRONT SHEET
Qualification
BTEC Level 5 HND Diploma in Computing
Unit number and title
Unit 1: Programming
Submission date
18/6/2023
Re-submission Date
Date Received 1st submission
Date Received 2nd submission
Student Name
Nguyen Nhat Quang
Student ID
BC00224
Class
SE06201
Assessor name
Nguyen Thanh Dat
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
M1
D1
❒ Summative Feedback:
Grade:
Lecturer Signature:
❒ Resubmission Feedback:
Assessor Signature:
Date:
Table of Content
Introduction ................................................................................................................................................................... 4
State your simple business problems to be solved ................................................................................................... 4
What is the algorithm? .......................................................................................................................................... 4
Example of algorithm .............................................................................................................................................5
Analyse the problem and design the solutions by the use of suitable method ........................................................ 6
Problem ..................................................................................................................................................................6
Request .................................................................................................................................................................. 6
Problems arise ....................................................................................................................................................... 6
Programs flowchart ................................................................................................................................................7
Explain flowchart ................................................................................................................................................... 7
Demonstrate the compilation and running of a program ......................................................................................... 8
The life cycle of a program .....................................................................................................................................8
The process from code to programming language ................................................................................................8
Programs .............................................................................................................................................................. 10
Evaluate how the problem is solved from the designed algorithm to the execution program written by a specific
programming language. ...........................................................................................................................................13
Case 1: ......................................................................................................................................................................13
Case 2: ......................................................................................................................................................................14
Conclusion ....................................................................................................................................................................15
References ................................................................................................................................................................... 16
Table of figures
Figure 1: What is the algorithm ..................................................................................................................................... 5
Figure 2: Flowchart ........................................................................................................................................................ 7
Figure 3: SDLC ................................................................................................................................................................ 8
Figure 4: The process of converting MSIL code to native code. ...................................................................................9
Figure 5: Case 1 ............................................................................................................................................................13
Figure 6: Case 2 ............................................................................................................................................................14
Introduction
An algorithm is a specific set of steps designed to solve a problem or perform a specific task. It is an
instructive process to perform a series of calculations or transform data to achieve a desired result.
There are different types of algorithms and they can be applied in many different fields like computer
science, math, engineering, economics, etc.
State your simple business problems to be solved
What is the algorithm?
An algorithm is a procedure used for solving a problem or performing a computation. Algorithms act as
an exact list of instructions that conduct specified actions step by step in either hardware- or softwarebased routines
Algorithms are widely used throughout all areas of IT. In mathematics and computer science, an
algorithm usually refers to a small procedure that solves a recurrent problem. Algorithms are also used
as specifications for performing data processing and play a major role in automated systems.
An algorithm could be used for sorting sets of numbers or for more complicated tasks, like
recommending user content on social media. Algorithms typically start with initial input and instructions
that describe a specific computation. When the computation is executed, the process produces an
output.
Figure 1: What is the algorithm
Example of algorithm
Example: Write down the steps for making peanut butter and jelly sandwich.
Steps for making a peanut butter and jelly sandwich:
Step 1: Take 2 slices of bread.
Step 2: Apply peanut butter on one side of a slice.
Step 3: Apply jelly on one side of the other slice.
Step 4: Press both the slices of bread together.
One of the simplest examples of an algorithm is to find a matching element from the input data.
For example, in stores, when an employee enters a product number, the computer returns the name and
price of that product.
Analyse the problem and design the solutions by the use of suitable
method
Problem
In convenience stores, finding products sometimes takes a lot of time, so we can make software to
search by code for employees using algorithms, this will save time searching for employees. staff and
create comfort for customers.
Request
First need to know the item code of each product, because each different product will have a different
item code, so we need to know the exact item code of each product for the computer to give the most
accurate results.
Problems arise
Problems that cause program crashes or incorrect results are caused by incorrect item codes or by
programmer error.
Programs flowchart
Figure 2: Flowchart
Explain flowchart
According to the diagram, the employee will enter the item code from the keyboard, then the program
will compare the employee's input with the information available in the machine to return the product
name and unit price.
Demonstrate the compilation and running of a program
The life cycle of a program
SDLC is a process followed for a software project, within a software organization. It consists of a detailed
plan describing how to develop, maintain, replace and alter or enhance specific software. The life cycle
defines a methodology for improving the quality of software and the overall development process.
Figure 3: SDLC
The process from code to programming language
C# (or C sharp) is a simple programming language. C# is a modern object-oriented programming
language and is built on the foundation of two of the most powerful languages, C++ and Java.
In traditional Windows applications, the program source code is compiled directly into the operating
system's executable code.
In applications that use the .NET Framework, program source code (C#, VB.NET) is compiled into
Microsoft intermediate language (MSIL) code.
This code is then compiled by the Common Language Runtime (CLR) to become the operating system's
executable code.
Figure 4: The process of converting MSIL code to native code.
C# with strong support of .NET Framework makes it very easy to create a Windows Forms or WPF
(Windows Presentation Foundation) application, game development, Web application, and Mobile
application.
Step 1: Write a C# code.
Step 2: Compile the code using a C# compiler.
Step 3: Now compiler checks if the code contains an error or not. If no error is found then the compiler
move to the next step. Or if the compiler found errors, then it will immediately tell the developer that an
error is found in the given line, so that the developer can correct them. After correcting the error when
you again run the code the compiler again check for the errors, if no error found then it will move to the
next step or if an error is found then the compiler gives a message to the developer. In C# there are two
types of errors:
Compiler error: Errors that occur when the developer violates the rules of writing syntax are known as
Compile-Time errors. This compiler error indicates something that must be fixed before the code can be
compiled. All these errors are generally detected by the compiler and thus are known as compile-time
errors. For example, missing semicolon, missing parenthesis, etc.
Runtime error: Errors that occur during program execution(run-time) after successful compilation is
called run-time errors. One of the most common run-time errors is division by zero also known as
Division error. These types of errors are hard to find as the compiler doesn’t point to the line at which
the error occurs
Step 4: Languages such as Java or C# are not directly converted or compiled into machine-level language
or machine instructions. These languages need to be converted to an intermediate code first, which is a
partially half compiled code. For C#, the source code is converted to an intermediate code which is
known as Common Intermediate Language (CIL) or Intermediate Language Code (ILC or IL code). This CIL
or IL Code can run on any operating system because C# is a Platform Independent Language.
Step 5: After converting the C# source code to Common Intermediate Language (CIL) or Intermediate
Language Code (ILC or IL code, the intermediate code needs to be converted to machine understandable
code. C# uses the .NET Framework and as part of this .NET Framework, the Virtual Machine component
manages the execution of programs written in any language that uses the . NET Framework. This virtual
machine component is known as Common Language Runtime (CLR) which translates the CIL or IL code to
native code or machine understandable code or machine instructions. This process is called th e Just-InTime (JIT) Compilation or Dynamic Compilation which is the way of compiling code during the execution
of a program at run time only.
Step 6: Once the C# programs are compiled, they’re physically packaged into Assemblies. An assembly is
a file that contains one or more namespaces and classes. As the number of classes and namespaces in
program grows, it is physically separated by related namespaces into separate assemblies. Assemblies
typically have the file extension .exe or .dll, depending on whether they implement applications or
libraries respectively, where EXE stands for Executable and DLL stands for Dynamic Link Library. An EXE
(Executable) file represents a program that can be executed and a DLL (Dynamic Link Library) file includes
code (Eg: Library) that can be reused across different programs.
Step 7: Now, the C# compiler returns the output of the given c# code.
Programs
namespace Quang
{
class Quang
{
public static void Main()
{
int item;
Console.WriteLine("Search items by item code");
Console.WriteLine("-------------------------");
Console.Write("Enter item code (0-9): ");
item = Convert.ToInt32(Console.ReadLine());
switch (item)
{
case 0:
Console.WriteLine("[Mirinda]");
Console.Write("Unit price: 15K");
break;
case 1:
Console.WriteLine("[Snack Oishi]");
Console.Write("Unit price: 10K");
break;
case 2:
Console.WriteLine("[Kit Kat]");
Console.Write("Unit price: 25K");
break;
case 3:
Console.WriteLine("[Oreo]");
Console.Write("Unit price: 20K");
break;
case 4:
Console.WriteLine("[Sting]");
Console.Write("Unit price: 15K");
break;
case 5:
Console.WriteLine("[Khoai Tay Lays]");
Console.Write("Unit price: 18K");
break;
case 6:
Console.WriteLine("[Cool Air]");
Console.Write("Unit price: 24K");
break;
case 7:
Console.WriteLine("[Omachi]");
Console.Write("Unit price: 12K");
break;
case 8:
Console.WriteLine("[Cocacola]");
Console.Write("Unit price: 15K");
break;
case 9:
Console.WriteLine("[Snack Rong Bien]");
Console.Write("Unit price: 40K");
break;
default:
Console.Write("Invalid item code. Please re-enter!!!");
}
}
}
}
break;
Console.ReadKey();
Evaluate how the problem is solved from the designed algorithm to
the execution program written by a specific programming language.
Now we will run the program
Case 1:
Figure 5: Case 1
In this case, when we enter the correct item code, the computer will output the item name and unit price
Case 2:
Figure 6: Case 2
In this case, when we enter the wrong item code, the machine will report invalid and ask to re-enter
Conclusion
In short, this is just a small example of a problem to see the application of the algorithm program and its
practicality in business.
If used well, it will save a lot of time, money and effort.
References
1. Alexander S. Gillis (???). What is an algorithm?.
https://www.techtarget.com/whatis/definition/algorithm
2. Unknown (???). SDLC Overview. https://www.tutorialspoint.com/sdlc/sdlc_overview.htm
3. Unknown (2020). What is C#? Learn about C# programming language.
https://codegym.vn/blog/2020/06/29/c-la-gi-tim-hieu-ve-ngon-ngu-lap-trinh-c/
Download