L1. Basic Programming Concepts

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 66

Basic Programming Concept

▪ Computer programs are the collections of instructions that


Introduction

tell a computer what to do, how to interact with the user,


interact with the computer hardware and process data.

▪ The first programmable computers required the


programmers to write explicit (clear & exact) instructions to
directly manipulate the hardware of the computer.

2
Computer Programs
❖Computer programs, known as software. You tell a computer
what to do through programs.

❖Without programs, a computer is an empty machine.

❖Computers do not understand human languages, so you need to


use computer languages to communicate with them.

3
Programming Languages
⚫ Programs are written using programming
languages.
⚫ Programming language is an agreed upon format
of symbols that allow a programmer to instruct a
computer to perform certain predefined tasks.

4
Types of programming languages

Some types of programming languages are:

⚫ Machine languages
⚫ Assembly languages
⚫ High-level languages

5
Machine language
⚫ Low-Level language is the only language which can
be understood by the computer.
⚫ Low-level language is also known as Machine
Language.
⚫ Sometimes called machine code or object code
⚫ The machine language contains only two symbols 1 &
0. All the instructions of machine language are written
in the form of binary numbers 1's & 0's.
⚫ A computer can directly understand the machine
language.
6
Assembly language
⚫ Middle-level language in which the instructions
are created using symbols such as letters, digits
and special characters.
⚫ In assembly language, we use predefined words
called mnemonics. Binary code instructions in
low-level language are replaced with mnemonics
and operands in middle-level language.
⚫ But the computer cannot understand mnemonics,
so we use a translator called Assembler to
translate mnemonics into machine language.
7
Assembly language (cont.)
⚫ Assembler is a translator which takes assembly
code as input and produces machine code as
output.
⚫ That means, the computer cannot understand
middle-level language, so it needs to be
translated into a low-level language to make it
understandable by the computer.
⚫ Assembler is used to translate middle-level
language into low-level language.
8
High-level languages
⮚High-level language is a computer language which
can be understood by the users.
⮚ The high-level language is very similar to human
languages and has a set of grammar rules that are
used to make instructions more easily.
⮚Every high-level language has a set of predefined
words known as Keywords and a set of rules
known as Syntax to create instructions.

9
High-level languages (Contd..)
⮚The high-level language is easier to understand for
the users but the computer can not understand it.
⮚ High-level language needs to be converted into
the low-level language to make it understandable
by the computer.
⮚We use Compiler or interpreter to convert high-
level language to low-level language.

10
Examples of High-Level Languages
Java (An object oriented language). General purpose –
gaining popularity rapidly.
COBOL (Common Business Oriented Language).
Developed for business applications.
FORTRAN (FORmula TRANslation). Developed for
Engineering & Scientific Applications.
BASIC (Beginner All-purpose Symbolic Instructional
Code).

11
Examples of High-Level Languages (cont.)
⚫ C is designed to support only procedure-oriented
programming. Popular language for developing system
applications such as operating system and compilers.
⚫ C++ is extension of C programming language that
supports object-oriented programming and procedure
oriented approach. General purpose and most popular.
⚫ Visual Basic – Basic-like visual language developed by
Microsoft.

12
Examples of High-Level Languages (cont.)
⚫ PROLOG - is a language for PROgramming in LOGic.
Artificial Intelligence.
⚫ Ada (named after Ada Lovelace)
⚫ Python - is a general purpose programming language
that's used to do things from testing microchips at
Intel, to building video games.

13
The Programming Process
Programming involves five steps to develop a program.

1. Defining the problem: State the problem clearly.


2. Describe the input and output information.
3. Work the problem by hand (or with a calculator) for
a specific set of data.
4. Develop a solution that is general in nature.
5. Testing the program: Test the solution with a variety
of data sets.
14
Why Programming?
⚫ There are some good reasons for learning programming:
⚫ Computer programming can be used to create
innovative and functional software. Computer
programmers can use their creativity to design software
that will be useful to people, or that will improve the
way that people work.
⚫ Computers can also be programmed to perform specific
tasks using mathematical algorithms and logic.
⚫ It can also be used to create websites, games, and other
digital content.

15
Different Phases of Programming Task
A typical programming task can be divided into three
phases:

1) Pre-programming phase (or Problem Solving Phase)


• Produce an ordered sequence of steps that describe
solution of problem.
• Algorithms as well as Flowcharts are implemented in
problem solving phase of programming.
2) Programming phase(or Implementation Phase)
⚫ implement the program in some programming language
3) Maintenance Phase
⚫ Maintain/modify to meet changed requirements or to
correct errors. 16
Pre-Programming Phase
This phase requires four steps:
⮚Analyzing the problem.
⮚Developing the Input-Process-Output
(IPO) Chart.
⮚Writing the algorithm.
⮚Drawing the flowchart.

17
Analyzing the problem

❖Understand and analyze the requirements of the


problem.
❖Identify the following:
⚫ Data requirement.
⚫ Processing requirement or procedures that will be needed to solve
the problem.
⚫ The output.

18
All these requirements can be presented in a Problem
Analysis Chart (PAC) as follows:

Data Processing Output


Given in List of Output/
the processing desired result
problem or required or
provided procedures
by the user

19
Problem 1: Calculate the salary of an employee who
works by hourly basis. The formula to be used is:
Salary = Hour works * Pay rate

Data Processing Output


Hours Salary = Salary
work, Hours works
Pay rate * pay rate

20
Problem 2: Draw a Problem Analysis Chart (PAC) to find
the area of circle where area = pi * radius * radius

Data Processing Output


radius area = 3.14 area
x radius x
radius

21
Problem 3. Draw a problem analysis chart to convert the
distance in miles to kilometers where 1.609 per mile.

Data Processing Output


Distance Kilometers Distance in
in miles = 1.609 x kilometers
miles

22
Problem 4. Draw a problem analysis chart to find the
area of a triangle.

Problem 5: Draw a problem analysis chart to


evaluate the following expression.
EXP= a²+2ab+b²

23
Developing the Input-Process-Output (IPO)
Chart
• Extends and organizes the information in the PAC.
• It shows in more details what items are input, what to
be processed and what will be the result or output.

24
Problem 1. Develop an IPO chart to convert the distance
in miles to kilometers where 1.609 kilometers per mile.

Input Processing Output


Distance in -Enter -Distance in
miles distance kilometers
-Kilometers
= 1.609 x
miles
-Display
kilometers

25
Constant and Variable
Constant: A constant can be defined as a fixed value,
which is used in algebraic expressions and equations. A
constant does not change over time and has a fixed
value. For example, the size of a shoe or cloth or any
apparel will not change at any point.

Variables: Variables are terms which can change or vary


over time. Its value does not remain constant, unlike
constants. For example, the height and weight of a
person do not always remain constant, and hence they
are variables.
26
What is algorithm?
❖In computer programming terms, an algorithm is a
set of well-defined instructions to solve a particular
problem.
❖It takes a set of input(s) and produces the desired
output.
❖Before writing a program, a programmer must start
with an algorithm of the program following with
flowchart and pseudo code.
❖Purpose: Algorithms provide a better understanding
towards programming and helps in building better
problem-solving logic. Algorithms help the
programmer to write code faster by following steps
in Algorithm. 27
What is algorithm? (contd..)

❖It is not the entire program or code; it is simple logic to a


problem represented as an informal description in the form of a
flowchart or pseudo code.

28
What is algorithm? (Contd..)
❖ Problem: A problem can be defined as a real-world problem for which you
need to develop a program or set of instructions. An algorithm is a set of
instructions.

❖ Algorithm: An algorithm is defined as a step-by-step process that will be


designed for a problem.

❖ Input: After designing an algorithm, the algorithm is given the necessary


and desired inputs.

❖ Processing unit: The input will be passed to the processing unit, producing
the desired output.

❖ Output: The outcome or result of the program is referred to as the output.

29
Examples of Algorithm

Problem 1. Write an algorithm to “Make


Tea”

Algorithm
Step1. Start.
Step2. Take water in pan
Step3. Boil the water
Step4. Add sugar and Tea leaves
Step5. Add milk
Step6. Boil it
Step7. Pour it into cup
Step8. Stop
30
There may be more than one way to solve a problem,
so there may be more than one algorithm for a
problem.

Before writing an algorithm for a problem, one should


find out what is/are the inputs to the algorithm and
what is/are expected output after running the
algorithm.

31
While writing algorithms we will use following symbols
for different operations:

+ for Addition
- for Subtraction
* for Multiplication
/ for Division and
for assignment. For example

A X*3 means A will have a value of X*3.

32
The essential properties of
Algorithm are:
⮚ Input: An algorithm requires some input values. An algorithm can be given a value
other than 0 as input.

⮚ Output: At the end of an algorithm, you will have one or more outcomes.

⮚ Unambiguity: A perfect algorithm is defined as unambiguous, which means that its


instructions should be clear and straightforward.

⮚ Finiteness: An algorithm must be finite. Finiteness in this context means that the
algorithm should have a limited number of instructions, i.e., the instructions should be
countable.

⮚ Effectiveness: Because each instruction in an algorithm affects the overall process, it


should be acceptable.

⮚ Language independence: An algorithm must be language-independent, which means


that its instructions can be implemented in any language and produce the same results.
33
Steps for Developing an Algorithm
✔ Define the problem: State the problem you are trying to solve
in clear and concise terms.

✔ List the inputs (information needed to solve the problem) and


the outputs (what the algorithm will produce as a result)

✔ Describe the steps needed to convert or manipulate the inputs


to produce the outputs. keep refining the steps until they are
effectively computable operations.

✔ Test the algorithm: choose data sets and verify that your
algorithm works!
34
Examples of Algorithm

Problem 2: Find the area of a Circle of radius r.


Algorithm:
Step1: Start
Step2: Read\input the Radius r of the Circle
Step3: Area PI*r*r // calculation of area
Step4: Print Area
Step5: Stop

35
Problem 3: Write an algorithm and draw a flow chart to
read two numbers and find their sum.

Algorithm:

Step 1 : Start
Step 2 : Input first number A
Step 3 : Input second number B
Step 4 : Total = A+B
Step 5 : Print Total
Step 6 : Stop

36
Practice Algorithm
✔ Algorithm 1: Find the largest number between
two numbers

✔ Algorithm 2: Find the odd and even number


from a given input value.

37
Flowchart
◆ A flowchart is simply a graphical representation of steps. It shows steps
in sequential order and is widely used in presenting the flow of
algorithms, workflow or processes.

◆ Typically, a flowchart shows the steps as boxes of various kinds, and


their order by connecting them with arrows.

◆ Flowcharts are very helpful in writing programs and explaining them to


others.

◆ Flowcharts are quite helpful in understanding the logic of complicated


and lengthy problems.

◆ Once the flowchart is drawn, it becomes easy to write the program in


any programming language.
38
When to use a flowchart?
★ It is most importantly used when the programmers make
projects. As a flowchart is a basic step to make the design
of projects.

★ When the flowcharts of a process are drawn, the


programmer understands the non-useful parts of the
process. So flowcharts are used to separate useful logic
from the unwanted parts.

★ Since the rules and procedure of drawing a flowchart are


universal, flowchart serves as a communication channel to
the people who are working on the same project for better
understanding.
39
BASIC FLOWCHART SYMBOLS ARE GIVEN
IN THE NEXT SLIDE

40
Symbol Name Use in flowcharts

Start/Stop Denotes the beginning or end of


(Oval) the program

Flow line Show the direction of data flow


or logical solution

Process Indicates a process to be carried


(Rectangle) out such as addition, subtraction,
multiplication and division.

Indicates input to the program or


Input/Output output from the program
(Parallelogram)

41
Symbol Name Use in flowcharts

Question, Used for making a decision. The


Decision program should continue along one of
(Diamond) two routes; either true or false based
on certain conditions.
Connector Connection of flowchart on the same
(Circle) page.

Connector Connection of flowchart from one


page to another.

42
General Rules for flowcharting

1. Use conventional flowchart symbols. Each symbol should have one


exit point except the decision symbol. As far as possible each symbol
should also have one entry point and in the case of decision symbol, it
is a must.

2. The flow of control in the problem can be shown with the help of
arrows. But, the flow lines should not cross each other.

3. The processing logic within the flowchart should flow from top to
bottom and from left to right.

4. The instructions written within the various flowchart symbols should


be independent of programming languages.
43
General Rules for flowcharting
(contd..)
5. The flow lines coming out of the decision symbol should
be properly labelled.

6. If the flowchart becomes large and complex (on same


page) then make use of ordinary connector symbols to
avoid crossing of flow lines.

7. Every flow chart must have the start and End points.

44
Advantages of Using Flowcharts
➔ Communication: Flow charts are better way of
communicating the logic of a system to all
concerned.
➔ Effective analysis: With the help of flowchart,
problem can be analyzed in more effective way.
➔ Proper documentation: Program flowcharts serve
as a good program documentation, which is needed
for various purposes.
➔ Efficient Coding: The flowcharts act as a guide or
blueprint during the systems analysis and program
development phase.

45
Advantages of Using Flowcharts (contd)
➔ Proper Debugging: The flowchart helps in
debugging process.
➔ Efficient Program Maintenance: The maintenance
of operating program becomes easy with the help of
flowchart. It helps the programmer to put efforts
more efficiently on that part.

46
Flow chart

Example: Draw a flowchart to sum two numbers.

47
Problem: Algorithm and a flow chart to calculate
area of square.

Algorithm
Step 1 : Start
Step 2 : Read value for a side A
Step 3 : [Compute] Area A*A
Step 4 : Print Area
Step 5 : Stop

48
Flow Chart

49
Problem: Algorithm and flowchart to find the average of three
numbers.

Algorithm
Step1 : Start
Step 2 : Enter Three Numbers A, B and C
Step 3 : Compute Average = (A+B+C)/3
Step 4 : Print Average
Step 5 : Stop

50
Flow Chart

,C

51
Relationship between Celsius and Fahrenheit

52
53
Problem 3: Convert temperature Fahrenheit to
Celsius

Algorithm:
Step 1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C (F - 32)*(5/9)
Step 4: Print Temperature in Celsius C
Step5: End

54
Flow Chart

55
Problem 3: Convert temperature Celsius to
Fahrenheit

Algorithm:
Step 1: Start
Step 2: Read Temperature in Celsius C
Step 3: F ( 9 * C / 5 ) + 32
Step 4: Print Temperature in Fahrenheit F
Step5: End

56
1. Write an Algorithm and Draw a Flowchart to find
the third angle of a triangle if two angles are given.

2. Write an Algorithm and Draw a Flowchart to


enter Principal, Time, Rate and calculate the
simple interest

3. Write an Algorithm and Draw a Flowchart to


calculate a bike’s mileage from the given total
distance (integer value) traveled (in km) and spent
fuel (in liters, float number up to 2 decimal point).

57
Flow Chart

58
Write an algorithm to find out if a given number is Even or
Odd.

Step 1: Start
Step 2: Declare variable a
Step 3: Input a
Step 4: if a%2==0
go to step 5
else go to step 6
Step 5: Print Even
Step 6: Print Odd
Step 7: Stop

59
Problem: Write the algorithm and draw the
flowchart to compare two numbers?
Algorithm

Step 1: Start
Step 2: Declare variables A, B
Step 3: Enter two numbers A and B
Step 4: If A>B
Go to Step 6
else go to Step 5
Step 5: If B>A
Go to step 7
else go to step 8
Step 6: Print A is greater
Step 7: Print B is greater
Step 8: Print A and B are Equal
Step 9: Stop
60
Flow Chart

61
Write an algorithm to determine a student’s final grade
and indicate whether it is pass or fail. The final grade is
calculated as the average of four marks.

Step 1: Start
Step 2: Declare variables M1, M2, M3, M4, GRADE
Step 3: Input M1,M2,M3,M4
Step 4: GRADE ← (M1+M2+M3+M4)/4
Step 5: if (GRADE >=60)
then go to step 7
else go to step 6
Step 6: Print “FAIL”
Step 7: Print “PASS”
Step 8: Stop
62
Flow Chart

START

Input
M1,M2,M3,M4

GRADE←(M1+M2+M3+M4)/4

NO YES
IS
GRADE>=60

Print Fail Print Pass

STOP

63
Algorithm and a flow chart to calculate the
sum of N natural numbers.
Algorithm
Step 1: Start
Step 2: Declare sum, I, N
Step 2: Read number N
Step 3: Set sum = 0 and i = 1
Step 4: Repeat steps 5 and 6 until i<=N
Step 5: update sum as sum sum + i
Step 6: increment i
Step 7: Print sum
Step 8: Stop
64
65
Practice session:

Draw the flowchart to print sum of all even numbers


between 1 to n.

66

You might also like