Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
81 views

ASM 1 Programing

Uploaded by

vucuong1024
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

ASM 1 Programing

Uploaded by

vucuong1024
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 1: Programming

Date Received 1st


Submission date
submission

Date Received 2nd


Re-submission Date
submission

Student Name Nguyen Vu Cuong Student ID BH00904

Class SE06302 Assessor name Ha Ngoc Linh

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 Cuong

Grading grid
P1 P2 M1 D1

1
❒ Summative Feedback: ❒ Resubmission Feedback:
Lecturer Signature:

Grade: Assessor Signature: Date:

2
Table of Contents
Introduction.........................................................................................................................................5
I. Content.....................................................................................................................................5
1. Define basic algorithms to carry out an operation and outline the process of programming an
application.........................................................................................................................................5
a. Problem Definition................................................................................................................7
b. Design....................................................................................................................................7
c. Coding...................................................................................................................................7
d. Debugging:............................................................................................................................8
e. Testing:.......................................................................................................................................8
f. Documentation:.....................................................................................................................8
g. Maintenance:.........................................................................................................................8
a. Tasks in defining a problem:..................................................................................................8
c. Specifying the processing requirements:................................................................................9
II. Determine the steps taken from writing code to execution.................................................10
1. Pseudo code concept:...............................................................................................................10
a. Running flow (execution sequence) of the pseudocode:......................................................10
b. Some common keywords when representing pseudocode:..................................................11
2. Creating a new app:.................................................................................................................11
3. The source code:......................................................................................................................12
4. Using Directive:.......................................................................................................................12
5. Namespace:..............................................................................................................................12
6. Class declaration:.....................................................................................................................13
7. C# Method and Statements:.....................................................................................................13
8. Statements/comments:.............................................................................................................13
9. Multiple Line Statement:.........................................................................................................13
10. Formatting Text:..................................................................................................................14
11. Displaying Output:...............................................................................................................14
12. User Input:...........................................................................................................................15
13. User Input:...........................................................................................................................15
14. Read the input:.....................................................................................................................16
III. Inconclusion...........................................................................................................................16
IV. Reference List........................................................................................................................17

3
Introduction
 A computer program consists of code that is executed on a computer to perform particular
tasks. This code is written by programmers. Programming is the process of giving
machines a set of instructions that describe how a program should be carried out.
Programmers will spend their whole careers learning a variety of programming languages
and tools so they can effectively build computer programs. Programmers will start by
using a code editor or IDE to write what is called source code. This is a collection of code
written in a programming language that other programmers can read. Source code needs
to be converted into machine language so machines can understand the instructions and
execute the program. This process of converting source code into machine language is
known as compiling. Examples of compiled programming languages would be C and C+
+. There are other languages that do not use compilers. Instead, these languages will use
an interpreter that will read and execute the code. Examples of interpreted programming
languages would be JavaScript and PHP. Once the code is executed, then the computer
program can run. The different types of computer programs include Word processors,
Database systems, video games, and websites. These computer programs allow us to
interactive with different software devices and services like phones, websites, and the
computers themselves.

I. Content

1. Define basic algorithms to carry out an operation and outline the process of
programming an application
2. Algorithm:
 An algorithm is the step-by-step unambiguous instructions to solve a given problem.
 In the traditional study of algorithms, there are two main criteria for judging the merits of
algorithms: correctness (does the algorithm give solution to the problem in a finite
number of steps?) and efficiency (how much resources (in terms of memory and time)
does it take to execute.
Example of an algorithm:

4
3. Computers and Algorithms:
 To make a computer do anything, we have to write a computer
program.
 To write a computer program, we have to tell the computer, step by
step, what we want it to do.
 The computer then "executes" the program, following each step
mechanically, to accomplish the desired goal.
 Data structure + algorithms = program.

 When we are telling the computer what to do, we also get to choosehow it's going to do it.
 That's where computer Algorithms come in to picture.
 The algorithm is the basic technique used to get the job/work done.
 When we say that algorithms are ubiquitous, we mean... Algorithms are found
everywhere. Algorithms can be used to solve many different types of problems.
Algorithms are a daily occurrence.
4. Algorithms Characteristics:
 Finiteness: An algorithm must always terminate after a finite number of steps.
 Definiteness: Each step of an algorithm must be precisely defined; the actions to be
carried out must be rigorously and unambiguously specified for each case.
 Input: An algorithm has zero or more inputs
 Output: An algorithm has one or more outputs
 Effectiveness: the algorithms is based on the available resources. It should be effective,
exact and suitable for programs.

5
 Language Independent: The Algorithm designed must be language-independent. It has to
be general resources. We can apply to all programming languages.
5. Steps in Program Development

 The various steps involved are


• Defining or Analyzing the problem
• Design (Algorithm)
• Coding
• Documenting the program
• Compiling and running the program
• Testing and Debugging
• Maintenance

a. Problem Definition
 The first step in the process of program development is the thorough understanding and
identification of the problem for which is the program or software is to be developed.
 In this step the problem has to be defined formally.
 All the factors like Input/output, processing requirement, memory requirements, error
handling, interfacing with other programs have to be taken into consideration in this
stage.
b. Design
 The next stage is the program design. The software developer makes use of tools like
algorithms and flowcharts to develop the design of the program.

Algorithm

Flowchart

c. Coding
 Once the design process is complete, the actual computer program is written, i.e. the
instructions are written in a computer language.
 Coding is generally a very small part of the entire program development process and also
a less time consuming activity in reality.
 In this process all the syntax errors i.e. errors related to spelling, missing commas,
undefined labels etc. are eliminated.
 For effective coding some of the guide lines which are applied are :
o Use of meaningful names and labels of variables,
o Simple and clear expressions,
o Modularity with emphasis on making modules generalized,
o Making use of comments and indenting the code properly,
o Avoiding jumps in the program to transfer control.
6
d. Debugging:
 At this stage the errors in the programs are detected and corrected.
 This stage of program development is an important process. Debugging is also known as
program validation.
 Some common errors which might occur in the programs include:
o Un initialization of variables.
o Reversing of order of operands.
o Confusion of numbers and characters.
o Inverting of conditions eg jumping on zero instead of on not zero.:

e. Testing:

 The program is tested on a number of suitable test cases.


 A test plan of the program has to be done at the stage of the program design itself.
 This ensures a thorough understanding of the specifications.
 The most trivial and the most special cases should be identified and tested.
 It is always useful to include the maximum and minimum values of all variables as test
data.
f. Documentation:
 Documentation is a very essential step in the program development.
 Documentation help the users and the people who maintain the software.
 This ensures that future modification if required can be done easily. Also it is required
during redesigning and maintenance.
g. Maintenance:
 Updating and correction of the program for changed conditions and field experience is
accounted for in maintenance.
 Maintenance becomes essential in following situations:
o Change in specification,
o Change in equipment,
o Errors which are found during the actual execution of the program.

6. Steps in Program Development: Analyzing or Defining the Problem:


 The problem is defined by doing a preliminary investigation
 Defining a problem helps us to understand problem clearly
 It is also known as Program Analysis

a. Tasks in defining a problem:


 Followings are the tasks in order to define a problem
 Specifying the input requirements
 Specifying the output requirements
 Specifying the processing requirements

7
Figure 3:
b. Specifying the input requirements:
 The input specification is obtained by answering following questions
 What specific values will be provided as input to the program?
 What format will the values be?
 For each input item, what is the valid range of values that it may assume?
 What restrictions are placed on the use of these values?

c. Specifying the processing requirements:


 The processing requirement is obtained by answering following questions
 What is the method (technique) required in producing the desired output?
 What are the validation checks that need to be applied to the input data?
 What calculations are needed?
-Basic algorithms are step-by-step procedures that outline the process of solving a problem or
executing an operation
 Understand the problem: Clearly define the problem you want to solve or the operation
you want to perform. Identify the input data, desired output, and any specific
requirements or constraints.
 Design an algorithm: Break down the problem into smaller, manageable tasks. Decide on
the appropriate data structures and algorithms to use. Create a flowchart or write
pseudocode to represent the logical steps of the solution.
 Choose a programming language: Select a programming language that is suitable for the
application you want to build. Consider factors such as performance requirements,
platform compatibility, and personal familiarity.
 Set up the development environment: Install the necessary software tools and libraries to
support the chosen programming language. This may include Integrated Development
Environments (IDEs), compilers, or interpreters
 Test and debug: Test the application with various inputs and scenarios to ensure it behaves
as expected. Identify and fix any errors or bugs through debugging techniques. Use
testing frameworks or methodologies to automate and streamline the testing process.
 Optimize and refine: Review the code for potential improvements in terms of efficiency,
readability, and maintainability. Consider algorithmic optimizations, code refactoring, and

8
adherence to best practices. Seek feedback from others to gain insights and make
necessary adjustments.
 Document and deploy: Document the code, including comments, explanations, and
instructions for future reference or collaboration. Prepare the application for deployment
by packaging it appropriately for the target platform or environment.
 Maintain and update: Regularly maintain and update the application based on user
feedback, bug reports, or changing requirements. Continuously monitor for performance
issues, security vulnerabilities, or compatibility problems.

II. Determine the steps taken from writing code to execution


1. Pseudo code concept:
This is a source code concept but not the actual source code of any programming language.
To make it easier to imagine, we will understand as follows: Imagine in real life, with a real
language, a word please. Hello in Vietnamese can be translated into different languages, for
example, English is hello, French is bonjour..., together to describe a greeting. In
programming, too, each programming language will understand and compile the same job,
but there are syntactic differences depending on the characteristics and structure of each
language. Therefore, when approaching programming, we can use the concept of
pseudocode to describe a general work content in a coding style but more concise and easier
to understand. And this language cannot be compiled by a computer compiler, but is mainly
used for communication between programmers. (In reality, when exchanging programming
expertise, programmers rarely write pseudocode to communicate, but mainly talk to describe
and understand the problem, however in some cases the problem is complex. or for new
programmers, writing pseudocode to describe the problem also applies).
 Pseudocode example:
BEGIN
DISPLAY "hello world"
END
 This is a basic example of giving commands to the computer but with the style of using
pseudo code, this writing will be concise, easy to understand and the programmer's task
will be to follow the syntax of the code. each specific programming language to write a
command line that requests the computer to print the words "hello world".
a. Running flow (execution sequence) of the pseudocode:
o BEGIN : start the program
o DISPLAY “hello world”: prints the text “hello world” to the screen
o END: End of the program
 The keywords of the pseudocode can change flexibly depending on each person's taste,
but we can understand START or BEGIN or can even be translated into BATDAU
which is also pseudocode to represent the syntax of starting a program. just submit.

9
Similar to END/STOP/KETTHUC, and this pair of keywords often comes together
because in programming, where there is a beginning, there must be an end.

b. Some common keywords when representing pseudocode:
 BEGIN/START => Start the program
 END/STOP => End the program
 INPUT => Requires user input and saves to memory (interacts with keyboard)
 DISPLAY/WRITE => Print to screen
 IF/ELSE => If/then used to branch the flow (sequentially execute the program based on
a calculated condition)
 BEGIN LOOP/END LOOP => start and end of the loop (a concept in programming that
will be mentioned later in the article)

 Basic example of pseudocode with a problem of calculating the sum of two numbers
entered by the user:
BEGIN
INPUT A
INPUT B
C=A+B
DISPLAY C
END
 The above pseudocode asks the computer to execute the following tasks sequentially:

 Start the program


 Requires the user to input 2 numbers and name 2 storage resources A and B to store the
user input value,
 In addition, a resource C is needed, which is calculated by summing A and B
 then display the value of the resource named C to the screen.
 End program.

2. Creating a new app:

10
Figure 4:

3. The source code:

Figure 5:
4. Using Directive:
 From line 1 to 5, we have a few statements that start with the word using. These
statements are known as directives. They tell the compiler that our program uses a
certain namespace
 For instance, the first line
using System;
 tells the compiler that our program uses the System namespace.
5. Namespace:

11
 A namespace is simply a grouping of related code elements. These elements include
classes, interfaces, enums and structs etc
 C# comes with a large amount of pre-written code that are organised into different
namespaces.
 The System namespace contains code for methods that allow us to interact with our
users. We use two of these methods in our program
- the WriteLine() and Read() methods.
6. Class declaration:
 Every app consists of at least one class declaration that defined by the programmer
• user-defined class
• E.g., public class Welcome1
 Class Name convention (upper camel casing)
• Begin with a capital letter
• Capitalize the first letter of each word included
• Contains letters, digits, and underscore
• Doesn’t start with digit, doesn’t contain spaces
 C# is case sensitive
• So be careful because Myname is different from MyName.
7. C# Method and Statements:
 Main method
• public static void Main(string[] args)
• It’s the starting point of every app
 Statements
• Statements end with a semicolon (;)
8. Statements/comments:
 Statement is instruction that programmer tells computer to do
• Every C# statement is terminated with semicolon ‘;’
 Comment is explanation / guide that programmer explain to reader some block of
code and comments will not be executed
• Comment a single line with //
• Comment a block with /*..........*/

9. Multiple Line Statement:

12
Figure 6:

10. Formatting Text:

13
Figure 7:
11. Displaying Output:

 Most applications require some input from the user and give output as a result. To
display text to the console window you use the Console. Write or Console.WriteLine
methods.
 The difference between these two is that Console.WriteLine is followed by a line
terminator, which moves the cursor to the next line after the text output.
 The program below will display Hello World! to the console window:

12. User Input:

 You can also prompt the user to enter data and then use the Console.ReadLine method
to assign the input to a string variable. The following example asks the user for a
name and then displays a message that includes the input:

Figure 8:

13. User Input:


 The Console.ReadLine() method returns a string value. If you are expecting another
type of value (such as int or double), the entered data must be converted to that type.
 This can be done using the Convert.ToXXX methods, where XXX is the .NET name
of the type that we want to convert to. For example, methods include
Convert.ToDouble and Convert.ToBoolean.
 For integer conversion, there are three alternatives available based on the bit size of
the integer: Convert.ToInt16, Convert.ToInt32 and Convert.ToInt64. The default int
type in C# is 32-bit.

14
14. Read the input:
 For standard input we use Console.ReadLine()
• String s = Console.ReadLine();
• int n = Convert.ToInt32(Console.ReadLine());
 Possible erroneous input
• User can input a string which is not an integer
• In this case an Exception is raised
• We will learn about handling exception later.

-The steps taken from writing code to execution


 Writing the code: Firstly, you need to write your code using a text editor or integrated
development environment (IDE) such as Visual Studio. C# code is typically written in
a .cs file.
 Compiling the code: After writing the code, you need to compile it using a compiler such
as the C# compiler (csc.exe). The compiler translates your human-readable code into
binary instructions that the computer can understand. It also checks for syntax errors and
performs static type checking.
 Generating an executable file: The compiler generates an executable file (with a .exe
extension) from your compiled code. This file contains the machine-readable instructions
that the computer can directly execute.
 Linking: If your code depends on external libraries or frameworks, the compiler also
performs a linking step. During this step, the compiler links your code with the required
external libraries to ensure that all necessary dependencies are resolved.
 Execution: Finally, you can run the executable file. You can either double-click on it, use
the command line to launch it, or run it from within an IDE. The computer reads and
executes the instructions in the executable file, resulting in the desired behavior of your
program.
It's worth mentioning that some IDEs, such as Visual Studio, combine multiple steps into
a single integrated process. For example, when you hit the "Run" button in Visual Studio,
it automatically compiles, links, and executes your code.

III. Inconclusion

 Computer programming can be a very rewarding career both intellectually and


financially. Programmers need to be willing to learn new things and solve
complex problems.
 There are many different fields of programming to explore like Web, mobile,
game development or systems engineer. Some fields will require formal
education while others are open to self taught programmers.
 I hope you enjoyed this article and best of luck on your programming journey.

15
IV. Reference List
Karumanchi, N. (2011). Data Structures and Algorithms Made Easy: Data Structure and
Algorithmic Puzzles.
Nagel, C. (2018). Professional C# 7 and . NET Core 2. 0. Wiley & Sons, Incorporated, John.

Class 1+2 - KJWQBFHIOERHOIHDNSF - LO1: Define basic algorithms to carry out an


operation and outline (no date) Studocu. Available at:
https://www.studocu.com/en-gb/document/teesside-university/food-product-development/
class-12-kjwqbfhioerhoihdnsf/36969978 (Accessed: 08 October 2023).

Wilkins, J. (2021) What is computer programming?, freeCodeCamp.org. Available at:


https://www.freecodecamp.org/news/what-is-programming/ (Accessed: 08 October
2023).

What is algorithm: Introduction to algorithms (2023) GeeksforGeeks. Available at:


https://www.geeksforgeeks.org/introduction-to-algorithms/ (Accessed: 06 October 2023).

Stages of program development process (no date) Stages of program development process
(Program development steps) || CseWorld Online. Available at:
https://www.cseworldonline.com/articles/stages-of-program-development-process.php
(Accessed: 06 October 2023).

Wilkins, J. (2021, ngày 8 tháng 10). Lập trình máy tính là


gì? freeCodeCamp.org. https://www.freecodecamp.org/news/what-is-programming/

16
Flowchart

17
Console.Write("Tên khách hàng: ");
string tenKhachHang = Console.ReadLine();
Console.WriteLine("Nhập loại khách hàng: ");
string loaiKhachHang = Console.ReadLine();
Console.WriteLine("Nhập số người: ");
int soNguoi = int.Parse(Console.ReadLine());
Console.WriteLine("Nhập số mét khối: ");
int soMetKhoi = int.Parse(Console.ReadLine());
Console.Write("Chỉ số đồng hồ nước tháng trước: ");
int chiSoTruoc = int.Parse(Console.ReadLine());
Console.Write("Chỉ số đồng hồ nước tháng này: ");
int chiSoNay = int.Parse(Console.ReadLine());
int luongTieuThu = chiSoNay - chiSoTruoc;
Console.WriteLine("\nThông tin khách hàng:");
Console.WriteLine("Tên khách hàng: " + tenKhachHang);
Console.WriteLine("Chỉ số đồng hồ nước tháng trước: " + chiSoTruoc);
Console.WriteLine("Chỉ số đồng hồ nước tháng này: " + chiSoNay);
Console.WriteLine("Lượng tiêu thụ: " + luongTieuThu);

double price = TinhTienNuoc(soMetKhoi);


Console.WriteLine("Tổng tiền nước là: {0} VND", price);

static double TinhTienNuoc(int soMetKhoi)


{
double tienNuoc = 0;
double phiBaoVeMoiTruong;

if (soMetKhoi <= 10)


{
tienNuoc = soMetKhoi * 5300;
phiBaoVeMoiTruong = 597.30;
}
else if (soMetKhoi <= 20)
{
tienNuoc = 10 * 5300 + (soMetKhoi - 10) * 7052;
phiBaoVeMoiTruong = 705.20;
}
else if (soMetKhoi <= 30)
{
tienNuoc = 10 * 5300 + 10 * 7052 + (soMetKhoi - 20) * 8699;
phiBaoVeMoiTruong = 866.90;
}
else
{
tienNuoc = 10 * 5300 + 10 * 7052 + 10 * 8699 + (soMetKhoi - 30) * 15929;
phiBaoVeMoiTruong = 1592.90;
}

return tienNuoc + phiBaoVeMoiTruong;


}

18

You might also like