Professional Documents
Culture Documents
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
3
Introduction
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
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).
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:
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
+ 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.
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
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
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
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:
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;
}
#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