FP - Topic 1 - Basic Programming
FP - Topic 1 - Basic Programming
Fundamentals of Programming
Programming and
Algorithm
Rev.240901
Ho Chi Minh City
Contents
• Basic concepts of programming and
algorithms
• Software development process
• Represent algorithm
• Basic C / C ++ program structure
– Keywords
– Integrated development environment
– Library
2
Programming
• Computer programming
– The art of implementing one or more abstract
algorithms is related to each other in a
programming language to create a computer
program.
• Algorithm
– A finite set (sequence) of clearly defined
instructions (actions) designed to solve a
specific problem.
3
Programming (cont)
• Why is programming necessary?
– Replace human tasks with automation
– Leverage computational power for efficiency
– Enable reuse of code across different
applications
– Create tools that help develop other tools
–…
4
Algorithm
• What are the steps to make omelet?
– Step 1:
– Step 2:
– Step 3: ...
5
How to create a program
• Why can’t we teach computers like we teach
humans?
– Computers follow strict, step-by-step
instructions
– Human language is complex and ambiguous
– Computers only understand binary (0 and 1)
– Programming languages need to be clear and
precise.
–…
6
Example
• Solving an equation: ax + b = 0
(where a, b are real numbers).
Input: a, b in R
Output: solution of the equation ax + b = 0
• If a = 0
• b = 0 then the equation has any solution.
• b ≠ 0 then the equation has no solution.
• If a ≠ 0
• The equation has a unique solution x = -b/a
7
Properties of the algorithm
• Includes the following 5 properties:
– Accuracy: calculations or operations must be
precise.
– Transparency: steps are clear and arranged in a
logical order.
– Objectivity: different people, different machines,
same result.
– Universality: applicable to similar input
problems.
– Finishing: The process completes after a finite
number of steps.
8
Programming Process
Descripted by:
Clarify the problem • Natural language
• Flow Chart
• Pseudo Code
Propose Ideas
Build Algorithm
Coding
Syntax Errors
Logic Errors
Debuging
Runtime Errors
Release
9
Representations of algorithms
• Machine code
– 00000011111101010101010
• Pseudo code
– Natural language
– Chart
– Use a programming language like C / C ++,
Java but not too strict about syntax
• Source code
– C, C++, Java, C#, PHP, …
• Discuss: pros/cons of each type?
10
Use natural language
Input: a, b ∈ R
Output: solution x of the equation ax + b = 0
1. Enter 2 real numbers (a and b)
2. If a = 0 then
2.1. If b = 0 then
2.1.1. Any solution
2.1.2. End
2.2. Otherwise
2.2.1. No solution
2.2.2. End
3. Otherwise
3.1. The equation has an solution
3.2. Its value is x = -b/a
3.3. End
11
Use FlowChart
Block of limitation
Instruction for begin and end of algorithm
Block of condition
Depending on condition, decide which of paths to take
Block of execution
Tasks need to be done.
Path
Show which direction to continue
12
Use FlowChart
Solving an equation: ax + b = 0
Begin
Enter a,b
Đ S
a=0
Đ S
Calculate:
b=0
x = -b/a
Output Output
Ouput x
“Any Solution” “No Solution”
End
13
Pseudo code
• Use Pascal language but not too strict about
syntax
Input: a, b ∈ R
Output: a solution of the equation ax + b = 0
If a = 0 Then
Begin
If b = 0 Then
Print “Any Solution”
Else
Print “No solution”
End
Else
Print “There is a solution: x = -b/a”
14
Source code in C/C++
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b;
printf(“Please enter a, b: ”);
scanf(“%d%d”, &a, &b);
if (a == 0)
if (b == 0)
printf(“Any solution”);
else
printf(“No solution”);
else
printf(“x = %.2f”, -float(b)/a);
}
15
Review exercises
1. What is the algorithm?
2. Presenting the important properties of an
algorithm
3. Steps to build program?
4. Ways to represent algorithms? Pros and
cons of each method? Give examples.
16
Flow chart and pseudo exercises
4. Enter a person's birth year. Calculate the age of
that person.
5. Enter 2 numbers a and b. Calculate the sum,
difference, product, and quotient of the two
numbers.
6. Enter the product name, quantity, and unit price.
Calculate the total money and VAT payable.
Formulas:
money = quantity * unit price
value added tax (VAT) = 10% of the money
17
Flow chart and pseudo exercises
7. Enter scores and weights of 3 courses (Math,
Physics, and Chemistry) of a student. Calculate
the grade average of that student.
8. Enter the radius of the circle. Calculate the
circumference and area of that circle.
9. Enter your vehicle number (5 digits). How many
score does your car have?
10.Enter 2 integers. Find the min and max value of
those two numbers.
18
Solution for the exercise 4
Begin
Enter Birth
Year
Calculate
Age = Current Year – Birth Year
Ouput Age
End
19
Solution for the exercise 5
Begin
Enter
a and b
Calculate
Sum = a + b
Difference = a – b
Product = a * b
Quotient = a / b
Output
Sum, Difference, Product, Quotient
End
20
Solution for the exercise 6
Begin
Enter
Product Name
Quantity
Price
Calculate
Money = Quantity * Price
VAT= Money * 0.1
Ouput
Money và VAT
End
21
Solution for the exercise 7
Begin
Enter
Grade of Courses (M, P, C)
Weight of Courses (Wm, Wp, Wc)
Calculate
Average = (M*Wm + P*Wp + C*Wc) / (Wm +
Wp + Wc)
Output
Average
End
22
Solution for the exercise 8
Begin
Enter
Radius R
Calculate
PI = 3.1415
Circumference = 2*PI*R
Area = PI*R*R
Xuất
Chu vi và Diện tích
End
23
Solution for the exercise 9
Begin
Enter
Car’s Number (5 digits)
Calculate
Digit 4: n4 = N % 10, N = N / 10
Digit 3: n3 = N % 10, N = N / 10
Digit 2: n2 = N % 10, N = N / 10
Digit 1: n1 = N
Score S = (n1 + n2+ n3 + n4) % 10
Ouput
Score S
End
24
Solution for the exercise 10
Begin
Enter a,b
False True
a>b
Output Output
a max, b min a min, b max
End
25
Compiler vs interpreter
• Purpose:
– Source code → machine code
• Compiler:
– Translating available machine code
– Execute machine code every run
• Interpreter:
– No pre-translation
– Translate each line of source code at runtime
• Disscuss:
– Pros / cons of each type
– Which programming languages use compilers and
which use interpreters?
26
Programming Languages
• First period:
– 1950s: UNIVAC, IBM 701.
– Low level languages:
• 1st generation language: machine code
• 2nd generation languages: assembly language
• Characteristics:
– Near machine language -> difficult to learn.
– Programming effort
– Compact, fast program
– Deep system access
27
Assembly Code
28
Programing Languages
• Development periods:
– 1960s - 1970s
– E.Dijkstra -> structural programming
– High level language:
• 3rd generation languages: FORTRAN, ALGOL, C,
Pascal, ...
• Characteristics:
– Near natural language -> easy to learn.
– Less programming effort
– Larger programs are often slower.
– Limited system access
29
Programing Languages
• Nowadays?
– Over 1,000 programming languages
– Development:
• Logic programming (4th generation): Prolog, lisp, SQL, ...
• Event programming: Visual Basic, ...
• GUI programming: HTML, Javascript, ...
• Object-oriented: C ++, Java, C #, ...
• Write once, run everywhere: Java, C #, ...
• Mobile devices: C ++, Java, C #, Objective-C, ...
• …
– Characteristics:
• Easy to learn, easy to use
• Big size program.
30
Contents
• Basic concepts of programming and
algorithms
• Software development process
• Represent algorithm
• Basic C / C ++ program structure
– Keywords
– Integrated development environment
– Library
31
Introduction about C language
• Created by Dennis Ritchie at Bell Telephone
in 1972.
• The precursor is the B language, created by
KenThompson, also at Bell Telephone.
• It is a structured and case-sensitive
programming language
• ANSI C
• C++
32
Introduction about C/C++ language
• Advantages of C/C++ language
– Very powerful and flexible, capable of
expressing any idea.
– Widely used by professional programmers.
– Flexibility, little change on different computer
systems.
– Clear and concise
– Single programming, reuse via function
33
Integrated Development Environment
• Integrated Development Environment (IDE)
is a software which includes:
– Editor
– Compiler
– Runner
– Debuger
34
Integrated Development Environment
• Some IDEs for C++:
– Visual Studio
– Dev-C++
– Eclipse CDT
– NetBeans
35
Keywords in C/C++
• Keywords are:
– Words reserved in language.
– Cannot use keywords to name variables,
functions, or subroutine names.
– Some common keywords:
• const, enum, signed, struct, typedef, unsigned…
• char, double, float, int, long, short, void
• case, default, else, if, switch
• do, for, while
• break, continue, goto, return
36
Valid Keywords in C/C++
• Keywords include characters:
– 26 Latin characters: A, B, C, …, Z, a, b, c, …, z
– Digits: 0, 1, 2, …, 9
– Math symbols: + – * / = < > ( )
– The special characters: . , : ; [ ] % \ # $ ‘
– Hyphen character _ and whitespace ‘ ’
37
Identifiers in C/C++
• Identifiers are:
– A character sequence refers to the name of a
constant, a character constant, a variable name,
a data type, a function, or a procedure.
– It cannot overlap with keywords and be made up
of letters and numbers but require the first letter
to be either a letter or _.
– The maximum number of characters in a name
is 255 characters and _ is allowed in the name
but no spaces are allowed.
38
Identifiers in C/C++
• For example:
– Valid: SolvingAnEquation, My_Equation_1
– Invalide: 1A, Solving An Equation
– It is case-sensitive, so the following names are
different :
• A, a
• Exercise, exercise, EXERCISE, …
39
Syntaxs in C/C++
• Semi-colon ;
– Used to separate statements.
– Example: printf(“Hello World!”); printf(“\n”);
• Comments
– Put between a pair of markers /* */ or // (C++)
– Example: /*Ho & Ten: NVA*/, // MSSV: 0712078
• Character and string constants
– Character constants: ‘A’, ‘a’, …
– String constants: “Hello World!”, “Nguyen Van A”
– Note: ‘A’ is different from “A”
40
A program in C/C++
41
Example
#include <stdio.h>
#include <conio.h>
void main()
{
int x, y, sum;
printf(“Enter two integer numbers: ”);
scanf(“%d%d”, &x, &y);
sum = x + y;
printf(“Sum of 2 numbers is %d”, tong);
getch();
}
42
Exercies
1. Which of the following names (identifiers) is
invalid and why?
– Intro To Program, 1Exercise
– PrAtiCe, InTro_To_Prog@m
2. What do comments used for? how do they
use? Give examples.
3. Describe the structure of a C program.
Explain the meaning of each part of the
structure.
43
Exercies
• Exercise 1:
– Install the Visual Studio IDE
– [Advanced] Install other IDE
• Exercise 2:
– Write, compile and test a basic program.
• Exercise 3:
– Understand basic operations in IDE: run, debug,
watch value in variable, shortcut keys, add
libraries, etc.
44
The End