You are on page 1of 50

CSC – 103

Fundamentals of Computers and Application

Chapter - 6
Programming Concept
Lecture – (week – 5)

Md. Mortuza Hossain


Lecturer, Department of CSE
IUBAT
17 September 2019 CSC 103 1
Outline’s
 Introduction
 Basic of Programming Language
 The Programming Process
 Different Phases of Programming Task
 Problem Solving Phase
 The Steps of Problem solving phase
 Implementation Phase
 Maintenance Phase

17 September 2019 CSC 103 2


Importance of chapter
This chapter gives you a overview of Programming
Concepts such as:
- Algorithm,
- Flowcharts,
- Program Structure,
- Variable Declaration
- Data Types
used in programming language.

17 September 2019 CSC 103 3


Outcomes of chapter
 Understand the basics and usefulness of an
algorithm.
 Analyze various algorithms.
 Understand a flowchart and its advantage and
limitations.
 Steps involved in designing a program.
 Understand basics of Programming Concept.

17 September 2019 CSC 103 4


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 used “Machine


Language” to give instruction.

It hard to understand for human. It called “lower level


language”. Only contains sequences of ‘0’ and ‘1’

Higher level language: C, C++, COBOL, Java etc.


17 September 2019 CSC 103 5
Basic of Programming Language
Formal Definition:
A set of rules that provides a way of telling a computer
what operations to perform is called a programming
language.

By using which software we give instructions to solve


different types of problems are called programming
software.

Different types software use different kinds of rules to


solve problem, are called Programming language.

17 September 2019 CSC 103 6


What Programmers Do?
 Normally a programmer convert problem
solutions into instructions for the computer.
 They prepares the instructions for computer and
runs those instructions on the computer.
 Tests the program to find out error and correct
program.
 According to user requirements programmer
write program to solve their problem.
 Programmers have to coordinate meetings with
users, managers, systems analyst, and who evaluate
their work.
17 September 2019 CSC 103 7
Why Programming?
There are at least 3 good reasons for learning programming:

 Programming helps us to understand computers. The


computer is only a tool. If we learn how to write simple
programs, we will gain more knowledge about how a
computer works.

 Writing a few simple programs increases our confidence


level. It may be give you self satisfaction and motivate to
solve many other problems.

 Learning programming helps us to find out quick solution. It


increases our thinking power, makes our logic more strong,
find out effective and efficient solution.
17 September 2019 CSC 103 8
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.

17 September 2019 CSC 103 9


Different Phases of Programming Task.
 A typical programming task can be divided into 3
phases:

1) Problem Solving Phase


2) Implementation Phase
3) Maintenance Phase

17 September 2019 CSC 103 10


Problem Solving Phase:
This phase produces an ordered sequence of steps
that describe solution of problem. This sequence of
steps is called an algorithm.
The tasks of this phase are-
1) Analysis and specification: Define problem and
what solution must do.
2) General solution (Algorithm): Develop logical
sequence of steps to solve problem.
3) Verify: Follow steps by hand.

17 September 2019 CSC 103 11


Problem Solving Phase:
The steps of problem solving phase are:

 First produce a general algorithm (pseudo code)

 Pseudo code is a simple way of writing


programming code in English. It is not actual
programming language.

17 September 2019 CSC 103 12


Implementation Phase
This phase implements the program in some
programming language (use: C,C++,Java etc)

The tasks of this phase are-


1) Specific solution (program): Translate algorithm to
code.
2) Test: Check computed results manually.

17 September 2019 CSC 103 13


Maintenance Phase
The tasks of this phase are-

1) Use the program.


2) Maintain: Modify to meet changed requirements
or to correct errors.

17 September 2019 CSC 103 14


Algorithms
Basic of Algorithm:
Formal Definition: Well-ordered collection of unambiguous and
effectively computable operations, that when executed, produces a
result and halts in a finite amount of time.

In mathematics, computer science, and related subjects, an


algorithm is a finite sequence of steps expressed for solving a
problem.

Finally algorithm is, “A process that performs some sequence


of operations in order to solve a given problem”.

Algorithms are used for calculation, data processing, programming


and many other fields.
17 September 2019 CSC 103 15
Characteristics of an Algorithm
An algorithm has following characteristics:

1. Each and every instruction should be precise and


unambiguous.

2. Each instruction should be performed in finite time.

3. One or more instructions should not be repeated


infinitely. It means that the algorithm must terminate
ultimately.

4. After the instructions are executed, the user should


get the required results.
17 September 2019 CSC 103 16
Advantages of an Algorithm
Three reasons for using algorithms are efficiency, abstraction and
reusability.

1. Efficiency: Efficient algorithms must be used to solve such


problems considering the time and cost factor involved in
each algorithm.

2. Abstraction: Algorithms provide a level of abstraction in


solving problems because many complicated problems can be
extracted into simpler ones for which well-known algorithms
exist.

3. Reusability: Algorithms are often reusable in many different


situations. Like – an admission form.Add – Calculator.

17 September 2019 CSC 103 17


Method for Developing an Algorithm
1. Define the problem: State the problem we 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 verify that


whether algorithm works or not.
17 September 2019 CSC 103 18
Variable
 A variable is a data name that may be used to
store a data value.
 Variable hold a space in the computer memory.
Variable “a” declaring means its hold a position in
the computer memory.
x = 10
where x is a variable, and hold value 10;
a=x+2
a=?
a = 12
17 September 2019 CSC 103 19
Different Examples of Algorithm:
Example 1: Write down Algorithm for Adding Two
Numbers.
Steps:
1) Start
2) Declare 3 variables a,b,c of int type
3) Input the values on a & b
4) Calculate c = a+b and store the result on c
5) Print c
6) Stop

17 September 2019 CSC 103 20


Different Examples of Algorithm:
Example 2: Write down Algorithm to find out
average of 3 numbers.
Steps:
1) Start
2) Declare 4 variables a, b, c, d of int type
3) Put the values on a, b, c (input)
4) Calculate d = (a + b + c)/3 and store the result on d
5) Print d (output)
6) Stop

17 September 2019 CSC 103 21


Different Examples of Algorithm:
Example 3: Write down Algorithm to convert days
to month.
Steps:
1) Start
2) Declare 2 variables days and months
3) Put the value on days
4) Calculate months = days/30
5) Store the result on months
6) Print months (output)
7) Stop
17 September 2019 CSC 103 22
Different Examples of Algorithm:
Example 4: Write down Algorithm to find out area
of a circle.
Steps:
1) Start
2) Declare 2 variables area and radius
3) Put the value on radius
4) Calculate area = 3.1416 * (radius)2
5) Store the result on area
6) Print area (output)
7) Stop
17 September 2019 CSC 103 23
Different Examples of Algorithm:

1) Write down Algorithm to find out area of a


square.
2) Write down Algorithm to find out area of a
rectangle.
3) Write down Algorithm to convert temperature
Celsius to Fahrenheit.
4) Write down Algorithm to convert years, months
and days to number of days.

17 September 2019 CSC 103 24


Flowchart:
A graphical tool that diagrammatically depicts the steps
and structure of an algorithm or program.

Formal Definition: A graphical representation of the


sequence of operations in an information system or
program.

A flowchart is a graphical depiction of decisions and


the results of those decisions. They are used to
analyze, design, manage a process or diagram to
understand visualize what is going on.
17 September 2019 CSC 103 25
Characteristics of a Flowchart
 Shows logic of an algorithm.

 Emphasizes individual steps and their


interconnections.

 Controls flow from one action to the next.

17 September 2019 CSC 103 26


Advantages of Using Flowcharts:
The benefits of flowcharts are as follows:

 Communication: Flowcharts 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.
17 September 2019 CSC 103 27
Limitations of Using Flowcharts:
Although a flowchart is a very useful tool, there are
a few limitations in using flowcharts which are
listed below:

 Complex logic: Sometimes, the program logic is


quite complicated. In that case, flowchart becomes
complex and clumsy.
 Alterations and Modifications: If alterations
are required the flowchart may require re-drawing
completely.
17 September 2019 CSC 103 28
Flowchart Symbols
Different symbols are used to draw each type of flowchart.
Following are the basic symbols used generally.
Name Symbol Use of Flowchart

Oval Denote the beginning or end of the program.

Denotes an input operation.


Parallelogram

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made. The program


should continue along one of two routes (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation.

Flow Line Denotes the direction of logic flow in the program.


17 September 2019 CSC 103 29
The Basic Guidelines for flowchart
The basic guidelines necessary for drawing a
flowchart with the above symbols are that:

 In drawing a proper flowchart, all necessary


requirements should be listed out in logical order.
 The flowchart should be neat, clear and easy to
follow.
 The flowchart is to be read left to right or top to
bottom.
 A process symbol can have only one flow line
coming out of it.
17 September 2019 CSC 103 30
Different Examples of Flowchart
Example 1: Write an algorithm and draw a
flowchart to convert the length in feet to
centimeter.

Pseudocode:
 Input the length in feet (Lft)
 Calculate the length in cm (Lcm) by multiplying LFT
with 30
 Print length in cm (LCM)

17 September 2019 CSC 103 31


Different Examples of Flowchart
Example 1: Write an algorithm and draw a flowchart
to convert the length in feet to centimeter.

Algorithm: START

Input
1. Start Lft
2. Input Lft Lcm  Lft x 30
3. Lcm  Lft x 30
4. Print Lcm Print
Lcm
5. Stop
STOP

17 September 2019 CSC 103 32


Different Examples of Flowchart
Example 2: Write an algorithm that reads two
values, determines the largest value and prints the
largest value with an identifying message.

Pseudocode:
 Input two values value1 & value2
 if is value1 is greater than value2
Print “The largest value is value1”
else
Print “The largest value is value1”
17 September 2019 CSC 103 33
Different Examples of Flowchart
Example 2: START
Determines the largest value.
Input
VALUE1,VALUE2
Algorithm:
1. Start Y is N
VALUE1>VALUE2
2. Input VALUE1,VALUE2
3. if (VALUE1 > VALUE2) then
4. MAX  VALUE1 MAX  VALUE1 MAX  VALUE2

5. else
6. MAX  VALUE2
Print
7. endif “The largest value is”,
8. Print “The largest value is”, MAX MAX

9. Stop STOP

17 September 2019 CSC 103 34


Different Examples of Flowchart
Example 3: 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.

Pseudocode:
 Input a set of 4 marks
 Calculate their average by summing and dividing by 4
 if average is below 50
Print “FAIL”
else
Print “PASS”
17 September 2019 CSC 103 35
Different Examples of Flowchart
Example 3:
Determines Pass or Fail

Algorithm:
1. Start
2. Input M1,M2,M3,M4
3. GRADE  (M1+M2+M3+M4)/4
4. if (GRADE <50) then
Print “FAIL”
else
Print “PASS”
5. Stop

17 September 2019 CSC 103 36


Different Examples of Algorithm & Flowchart
1) Draw the flowchart to read a number and print
next & previous number.
2) Draw the flowchart to exchange the values of
two number. Suppose, a = 20, b = 15 then make it,
a = 15, b=20.
3) Draw the flowchart for finding largest number
among 3 numbers.
4) Write down Algorithm to convert years, months
and days to number of days.

17 September 2019 CSC 103 37


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 pseudo
code-or some other tool-to a programming language. As we have
already noted,

A programming language is a set of rules that provides a way of


instructing the computer what operations to perform. There are
many programming languages: BASIC, COBOL, Pascal, FORTRAN,
and C are some examples.

To describe, different topics of program, here we have considered a


high level language name “C” Programming Language.

17 September 2019 CSC 103 38


Programming C
C is a structured programming language.

It is considered a high-level language because it allows the


programmer to concentrate on the problem at hand and
not worry about the machine that the program will be
using.

That is another reason why it is used by software developers


whose applications have to run on many different
hardware platforms.

17 September 2019 CSC 103 39


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)

17 September 2019 CSC 103 40


Program (Example)
The following is a simple example of a program written in C
programming language. We call this the “Hello World” example as it
prints “Hello World” on the computer screen.

Programming Language: C

Example:

#include <stdio.h>
void main()
{
printf("Hello World");
}

17 September 2019 CSC 103 41


Variable Declaration
 A variable is a data name that may be used to store a data value.

 A variable is a named location in memory that is used to hold a


value that can be modified by the program.

Formal Definition: 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.

Variables are named memory locations that have a type, such as integer
or character, which is inherited from their type. The type determines
the values that a variable may contain and the operations that may
be used with its values.
17 September 2019 CSC 103 42
Variable Declaration
To Declare one variable:
type variable_name;
Example: int id;
To Declare more than one variables:
type variable_list;
Example: char gender, section;
type must be a valid C data type.
variable_list may consist of one or more identifier names separated by
commas.
Examples:
char gender , section;
int id;
float cgpa;
17 September 2019
id = 112233; CSC 103 43
Rules for writing variable name in C:
The name of a variable can be composed of letters, digits, and the
underscore character.
Rules for writing variable name in C are-

1) Variable name can be composed of letters (both uppercase and


lowercase letters), digits and underscore '_' only. Example: Ab_1
2) The first letter of a variable should be either a letter or an underscore.
But, it is discouraged to start variable name with an underscore though
it is legal. It is because, variable name that starts with underscore can
conflict with system names and compiler may complain. Example:
num
3) There is no rule for the length of length of a variable. However, try to
give name within 31 character.
4) Can not be the same as a C keywords & should not have the same
name as functions that are in C library.
5) Must not contain white space.
17 September 2019 CSC 103 44
Data Types in C
Variables can store different types of data including numeric values, single characters,
and text strings.Variables typically store values of a given type.

In order to use a variable within a program, the compiler needs to know in advance
the type of data that will be stored in it.
For this reason, we declare the variables at the start of the program.
Variable declaration consists of giving a new name and a data type for the variable.
This is normally done at the very start of the program.

A data type is used to


 Identify the type of a variable when the variable is declared
 Identify the type of the return value of a function
 Identify the type of a parameter expected by a function

17 September 2019 CSC 103 45


Data Types in C
Two Classifications of Data Types
There are two types of data type
1) Built-in data types
2) Programmer-defined data types

Built-in data types


1) Fundamental data types (int, char, double, float,
void, pointer)
2) Derived data types (array, string, structure)

17 September 2019 CSC 103 46


Fundamental data types
Data Types Description Storage Size Example
void Used to denote the type with no values
Example: 10,-10 etc.
Used to denote an integer type. It stores integer { int Count;
int
or “whole” numbers 2 bytes Count = 5;
}
Used to denote a character type. A single
character such as a letter of the alphabet or
punctuation. Character variables are letters of Example: ‘A’ , ’b’
the alphabet, ASCII characters or numbers 0-9. {
char If we declare a character variable we must char Letter;
1 byte
always put the character between single quotes Letter = 'x';
(like so ‘A’ ). So remember a number without }

single quotes is not the same as a character


with single quotes.
Used to denote a floating point type. It stores Example: 1.23,-20.1
{
real or fractional numbers (also called float to
float float Miles;
indicate a floating point number).Real (float) 4 bytes
Miles = 5.6;
values can have a decimal point in them. }

17 September 2019 CSC 103 47


Derived Data Types

Data Types Description

A finite sequence (or table) of variables of the same data


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

A collection of related variables of the same and/or


Structure different data types. The structure is called a record and
the variables in the record are called members or fields

17 September 2019 CSC 103 48


17 September
9/17/2019
2019 CSC 103 49
Thank You All

17 September
9/17/2019
2019 CSC 103 50