You are on page 1of 76

Basic Programming Concept

1
Lesson Plan
Title: Basic Programming Concept Ref. No: Lecture
20, 21, 22
Target Population: CSC 103 Duration: 180 min
Aims: To introduce students to different programming concepts such as
algorithm, flow chart, variables, data types in C and program
structure
Learning outcomes: At the end of the lecture, the students will be able to
understand the basics and usefulness of algorithm, analyze various
algorithms, create flowcharts, have a knowledge of variables and data types.
Content Method/ Resource or Time
Technique Aid
Introduction: Attendance, Lecture; Q/A White board, 30 min
Rapport building, Summary Multimedia
of the previous lecture by a projector
student, Questions from the
previous class, Pre-
assessment, Lecture outline
Development: Lecture, Q/A, White board, 120 min
1.Introduction to Programming Discussion Multimedia
Language projector
2. Different Phases of
Programming Task
3. Algorithms and Flowcharts
4. Program
5. Variables
6. Data Types

Conclusion: Lecture, Q/A White board, 30 min


1. Recap of main points Multimedia
2. Students’ Feedback & Answer projector
3. Assessment of students’
Learning Outcomes
4. References/Suggested Reading
(Lecture notes and pages 77-94
of CSE book) and Forward
planning for next lecture

3
Introduction

Computer programs are collections of


instructions that tell a computer 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.

4
Computer Programs
 Computer programs, known as software,
are instructions to the computer. 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.
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.
Programming languages are what software
developers use to create computer
software, though they are themselves pieces of
software too.
Types of programming languages

Some types of programming languages are:

Machine languages
Assembly languages
High-level languages
Machine language
 The most basic (called low-level) computer
language is the machine language that uses binary ('1'
and '0') code which a computer can execute (run) very
fast. Programming language that can be understood and
obeyed by a computer without conversion (translation).

 Different for each type of CPU, it is the native binary


language (comprised of only two characters: 0 and 1) of
the computer and is difficult to be read and understood
by humans.
Machine language (cont)
Is the natural language of a computer.
Does not need to translate and is ready for immediate
execution.
Machine language instruction is a binary string of 0s
and 1s. For instance:
0101 1000 0001 0000 1100 0000 0001 0000
Is machine dependent. Each computer type has its
own machine language.
Programs written in machine languages are not
portable because programs written for one type of
computer cannot be run on another type.
Assembly language
Consists of English-like abbreviations.
Easier to understand.
Program written in assembly languages
cannot be directly processed by a computer.
Must use language translators, called
assemblers, to convert them to machine
code.
Assembly language (cont.)
Disadvantages:
- in general, each assembly language
instruction corresponds to one machine
language instruction. Therefore, the
programs written in them are lengthy.
Because of variations in assembly
languages, programs written using them are
not portable.
High-level languages
Instructions are quite English-like, and
easier to learn and use than machine or
assembly languages.
Have to be converted to machine languages
before they can be executed using
compilers, system software that translates a
source program into an almost executable
object program.
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).
Pascal (named after Blaise Pascal). General use and as a
teaching tool.
Examples of High-Level Languages (cont.)
C – 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.
Examples of High-Level Languages (cont.)
PROLOG - is a language for PROgramming in LOGic.
Artificial Intelligence.
Ada (named after Ada Lovelace)
Delphi (Pascal –like visual language developed by
Borland)
Python - is a general purpose programming language
that's used to do things from testing microchips at
Intel, to powering Instagram, to building video games.
Software is generally created (written) in a
high-level programming language, one that
is (more or less) readable by people. These
high-level instructions are converted into
"machine language" instructions,
represented in binary code, before the
hardware can "run the code".
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.

17
Why Programming?
There are at least three good reasons for learning
programming: 
Programming helps us to understand computers.
Writing a few simple programs increases our
confidence level.
Learning programming helps us to find out
quickly whether we like programming and
whether we have the analytical turn of mind
programmers need.
18
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.
19
Pre-Programming Phase
This phase requires four steps:
Analyzing the problem.
Developing the Input-Process-Output
(IPO) Chart.
Writing the algorithm.
Drawing the flowchart.
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.
All these requirements can be presented in a Problem
Analysis Chart (PAC) as follows:

Data Processing Output


Given in List of Output
the processing requirement
problem or required or
provided by procedures
the user
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
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 x area
radius x
radius
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
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.
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
What is algorithm?
Algorithm is a solution to a
problem written in a step by
step manner.
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.
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.
The essential properties of
Algorithm are:
 It should be simple
 It should be clear with no ambiguity
 It should lead to a unique solution of the problem.
 It should involve a finite number of steps to arrive at a
solution.
 It should have the capability to handle some-
unexpected situations.
Steps to Algorithm
1. First produce a general algorithm (one can use
pseudocode or Program Design Language)
2. Refine the algorithm successively to get step by step
detailed algorithm that is very close to a computer
language.

Definition : Pseudocode is an artificial and informal


language that helps programmers develop
algorithms. Pseudocode is very similar to everyday
English.

32
Steps for Developing an Algorithm
1. Define the problem: State the problem you are
trying to solve in clear and concise terms.
2. List the inputs (information needed to solve the
problem) and the outputs (what the algorithm
will produce as a result)
3. Describe the steps needed to convert or
manipulate the inputs to produce the outputs.
Start at a high level first, and keep refining the
steps until they are effectively computable
operations.
4. Test the algorithm: choose data sets and 33
FLOWCHART

A flowchart is a graphical representation of an


algorithm.

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 high level
language.
BASIC FLOWCHART SYMBOLS ARE GIVEN
IN THE NEXT SLIDE
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)
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.
General Rules for flowcharting
1. All boxes of the flowchart are connected with Arrows.
(Not lines)
2. Flowchart symbols have an entry point on the top of
the symbol with no other entry points. The exit point
for all flowchart symbols is on the bottom except for
the Decision symbol.
3. The Decision symbol has two exit points; these can be
on the sides or the bottom and one side.
4. Generally a flowchart will flow from top to bottom.
However, an upward flow can be shown as long as it
does not exceed 3 symbols.
5. Connectors are used to connect breaks in the
flowchart. Examples are:
• From one page to another page.
• From the bottom of the page to the top of the same
page.
6. All flowcharts end with a terminal or a contentious
loop.
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.
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.
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
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
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
Flow chart
Problem 3: Convert temperature Fahrenheit to Celsius

Algorithm:

Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C = 5*(F - 32)/9
Step 4: Print Temperature in Celsius C
Step5: End
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
Flow Chart
Problem: Algorithm and flow chart 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
Flow Chart

,C
Problem: Algorithm and flow chart to find the largest of two
numbers.

Algorithm

Step1: Start
Step 2: Enter two numbers A and B
Step 3: Check if A is greater than B if yes go to Step 4 else go to Step 5
Step 4: Print A is greater than B
Step 5: Check if B is greater than A if yes go to Step 6 else go to Step 7
Step 6: Print B is greater than A
Step 7: Print A is equal to B
Step 8: Stop
Flow Chart
Problem: Write algorithm and draw flowchart to
“Find the Greater of two Numbers”
Step1. Start.
Step2. Input the two numbers
A and B.
Step3. Check if A>B. If yes,
go to step 4
else go to step 6
Step4. Display the number A
Step5. Go to step 7
Step6. Display the number B
Step7. Stop
3. Write algorithm and draw flowchart to
“Find the Area of a Rectangle”

Algorithm
Step1. Start.
Step2. Take the length (l) and breadth (b)
Step3. Find the Area as l x b
Step4. Give the result.
Step5. Stop
Write an algorithm to determine a student’s final grade
and indicate whether it is passing or failing. The final
grade is calculated as the average of four marks.
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0

PRINT PRINT
“PASS” “FAIL”

STOP
Write an algorithm and draw a flowchart to convert the length in feet to centimeter.

Flowchart
Algorithm START

• Step 1: Input Lft


Input

• Step 2: Lcm  Lft x 30 Lft

• Step 3: Print Lcm Lcm  Lft x 30

Print
Lcm

STOP
Algorithm and a flow chart to find the
factorial of a number.
Algorithm
Step 1: Start
Step 2: Read N
Step 3: [Initialize all counters] Set FACT= 1, i = 1
Step 4: Compute Fact = Fact * I Increment i
Step 5: Check if i < = N if true repeat step 4 if false go to
step 6
Step 6: Print fact
Step 7: Stop
Program
As a programmer, after problem solving phase, next step is to
code the program-that is, to express our solution in a
programming language. We will translate the logic from the
flowchart or pseudocode-or some other tool-to a programming
language.

Program Structure
Virtually all structured programs share a similar overall pattern:

Statements to establish the start of the program


Variable declaration
Program statements (blocks of code)

67
Variable & Variable Declaration
In computer programming, a variable is a storage
location and an associated symbolic name which
contains some known or unknown quantity or
information, a value. It is helpful to think of
variables as containers that hold information.
Every variable has a name, called
the variable name, and a data type. A variable's data
type indicates what sort
of value the variable represents, such as whether it is
an integer, a floating-point number, or a character.

68
Rules for writing variable names in C
1. Characters Allowed :
i. Underscore(_)
ii. Capital Letters ( A – Z )
iii. Small Letters ( a – z )
iv. Digits ( 0 – 9 )
2. Blanks & Commas are not allowed
3. No Special Symbols other than underscore(_) are
allowed
4. First Character should be alphabet or Underscore
5. Variable name Should not be Reserved Word
69
C keywords or reserved words, which
cannot be used as a variable name
Data Types
There are two types of data type
Built-in data types
Fundamental data types (int, char, float,
double, void, pointer)
Derived data types (array, string,
structure)
Programmer-defined data types (Structure,
Union, Enumeration)
72
Data Types
Data Description Storag Value Example
Type e size range
void Used to denote the type
with no values
int Used to denote an integer 2 bytes -32768 to Example: 10,
type. 32767 -10
{ int age;
age = 20;
}

char Used to denote a character 1 byte -128 to 127 or Example: ‘A’,


type. A single character 0 to 255 ‘b’
such as a letter of the
alphabet or punctuation. {
Character variables are char Letter;
letters of the alphabet, Letter = ‘H’
ASCII characters or
numbers 0-9. }
Data Description Storag Value Example
Type e size range
float Used to denote a floating 4 bytes 1.2E-38 to Example: 2.54,
point type. It stores real or 3.4E+38 -35.05
fractional numbers (also {
called float to indicate a float weight;
floating point number). weight = 65.50;
}

Array A finite sequence of variables of the same data type.

String An array of character variables. We can say , a collection of characters.

Structure A collection of related variables of the same and/or different data


types. The structure is called a record and the variables in the record
are called members or fields.
Simple example of a program
The following is a simple program in C programming language. We call
this a the “Hello World!” example as it prints Hello World! on the
computer screen.

#include <stdio.h>
void main()
{
printf(“Hello World!”);
}
Example program

#include <stdio.h>
void main()
{
int age;
float weight;
char sex;
age = 21;
weight = 51.5;
sex = “M";
printf("I am %d years old\n ", age);
printf(“My weight is %f\n’’, weight);
printf("My sex is %c ", sex);
}
76

You might also like