CS25C01 Computer
Programming: C
Handled By
Dr.R.VENKATESH,
ASP & Head
CSE (AIML)
To shine in life, you must first step out
of the shadows of shyness
Module I
Introduction to C
Problem Solving, Problem Analysis Chart, Developing an
Algorithm, Flowchart and Pseudocode, program structure,
Compilation & Execution process, Interactive and Script
mode, Comments, Indentation, Error messages, Primitive
data types, Constants, Variables, Reserved words,
Arithmetic, Relational, Logical, Bitwise, Assignment,
Conditional operators, Input / Output Functions, Built-in
Functions.
Practical: Create Problem Analysis Charts, Flowcharts and
Pseudocode for simple C programs (Minimum three).
Module I - Introduction to C
5
Problem Solving
• Problem Solving is the process of identifying a problem, analyzing it, and
finding an effective step-by-step solution to achieve the desired outcome.
• Problem-solving in C programming involves a systematic approach to
translate a real-world problem into a C program that provides a solution.
6
This process typically follows a series of steps:
• Problem Definition and Understanding
• Analysis
• Design (Algorithm and Flowchart)
• Coding (Implementation)
• Testing and Debugging
• Maintenance
7
PROBLEM
8
9
Problem Analysis Chart (PAC)
Problem Analysis Chart (PAC) is a structured table used in problem solving to break down a
given problem into four main parts:
• Input – what data is required – Given Data
• Process – the steps or logic to be applied
• Output – the expected result – Required Result
• Formula/Conditions – any equations or conditions involved – Solution
A tabular representation.
It is mainly used for understanding and analyzing the problem before writing an
algorithm.
10
Contd.,
11
Find the sum of two numbers
Input Process Output Formula / Condition
Number 1 (A) Read the two numbers Sum of two numbers SUM = A + B
Number 2 (B) Add the two numbers (A + B)
12
Largest of Two Numbers
Input Process Output Formula / Conditions
Two integer numbers
entered by the user (say a
and b).
1. Read two numbers from
the user.
2. Compare the first
number (a) with the
second number (b).
3. If a is greater than b,
then a is the largest.
4. Otherwise, if b is greater
than a, then b is the
largest.
5. If both numbers are
equal, display "Both are
equal."
The largest number among
the two inputs
 Condition 1: If a > b →
Largest = a
 Condition 2: If b > a →
Largest = b
 Condition 3: If a == b →
Both are equal
13
Check Whether a Number is Even or Odd
Input Process Output Formula /
Conditions
14
15
Contd.,
16
Todays Task
Problem Statement- Check whether a person is eligible to
vote or not (minimum age = 18 years).
Problem Statement-Calculate the area of a circle given its
radius.
17
Check whether a person is eligible to vote or not (minimum age = 18
years).
Input Process Output Formula / Condition
Age of person
Read the age of the person
Compare the entered age
with 18:
• If age ≥ 18 → Eligible
• Else → Not eligible
Message: “Eligible to vote”
or “Not eligible to vote”
If Age ≥ 18 → Eligible
If Age < 18 → Not Eligible
18
Calculate the area of a circle given its radius.
Input Process Output Formula / Condition
Radius (r) 1. Read the radius Print Area of circle Area = π × r × r
2. Apply formula for area of
circle
Algorithms
Example
21
Take off
Check in baggage Immigration check
Boarding
Security check
Take off
22
Landing
Collect baggage
Immigration check
Landing
23
Algorithm
input: Get data from the keyboard, a file, the network, or some other device.
output: Display data on the screen, save it in a file, send it over the network, etc.
24
Program and Programming
25
Algorithm, Flow chart, Pseudo-code, Programming
• Algorithm - Step- by-step procedure for solving a task or a problem
• Flowchart – Diagrammatic representation of algorithm
• Pseudo code – Representation of the algorithm in a way that is in between a
program and a normal english
• Programming - Implementation of an algorithm
26
Algorithm
What is an algorithm?
• It is a step- by-step procedure for solving a task or a
problem.
• Algorithm is an ordered sequence of finite, well defined,
unambiguous instructions for completing a task.
• Algorithm is an English-like representation of the logic
which is used to solve the problem.
28
Need of Algorithm
• To understand the basic idea of the problem.
• To improve the efficiency of existing techniques.
• To understand the flow of the problem.
• To compare the performance of the algorithm with respect to other
techniques.
29
Characteristics of an Algorithm
• Precision: The instructions should be written in a precise manner.
• Uniqueness: The outputs of each step should be unambiguous, i.e., they should be unique and only depend
on the input and the output of the preceding steps.
• Finiteness: Not even a single instruction must be repeated infinitely.
• Effectiveness: The algorithm should designed in such a way that it should be the most effective among many
different ways to solve a problem.
• Input: The algorithm must receive an input.
• Output: After the algorithm gets terminated, the desired result must be obtained.
• Generality: The algorithm can be applied to various set of inputs.
30
Building Blocks of Algorithm
• An algorithm starts from an initial state with some input.
• The instructions/statements describe the processing that must be done on the input to
produce the final output (the final state).
• An instruction is a single operation which when executed converts one state to other.
• In the course of processing, data is read from an input device, stored in computer’s
memory for further processing, and then the result of the processing is written to an
output device.
31
Contd.,
• The data is stored in the computer’s memory in the form of variables or
constants.
• The state of an algorithm is defined as its condition regarding current
values or contents of the stored data.
• The flow of control specifies the order in which individual instructions of an
algorithm are executed.
32
Different Approaches of Designing an Algorithm
• For a complex problem, its algorithm is often divided into smaller units called functions(or
modules). This process of dividing an algorithm into modules/ functions is called
modularization.
Advantage of modularization:
• Complex algorithm simpler to design and implement
• Each function can be designed independently. While designing one function, the details of
other functions can be ignored, thereby enhancing clarity in design which in turn simplifies
implementation, debugging, testing and overall maintenance of algorithm
33
Approaches
• Top-down approach
• Bottom-up approach
Complex algorithm
Function 1 Function n
Function 2
Top down
approach
Bottom up
approach
Each function can be divided into one or more sub-functions
34
Top-down approach
• Divide the complex problem into one or more functions
• Those functions can be further decomposed into one or more sub-functions.
• This process of decomposition is iterated until the desired level of module
complexity is achieved.
• Step-wise refinement: Begin with top most module and incrementally add
function that it calls.
35
Bottom-up approach
• Reverse of top-down approach
• Start with designing the most basic function and then proceed towards
designing higher level functions
• Higher level functions are implemented by using the operations performed
by lower level functions
• All higher level functions are grouped together to form even a higher-level
function.
• This process is repeated until the design of the complete algorithm is
obtained
36
Top down vs bottom up approach
• Top-down approach is appreciated for ease in documenting the functions,
generation of test cases, implementation of code and debugging.
• It is criticized because the sub-functions are analyzed in isolation without
concentrating on their communication with other functions thereby ignoring
the concept of information hiding.
• Bottom up approach allows information hiding by identifying which has to be
encapsulated within a function but this is difficult to be done in all cases
37
Algorithmic Problem Solving Steps
• 1. Understanding the problem
• 2. Determining the capabilities of the computational device
• 3. Exact/approximate solution
• 4. Select the appropriate data structure
• 5. Algorithm design techniques
• 6. Methods of specifying an algorithm
• 7. Proving an algorithms correctness
• 8. Analyzing the performance of an algorithm
38
1. Understanding the Problem
• The problem given should be clearly and completely understood
• Compared with earlier problems that have already been solved to check if it
is similar and a known algorithm exists.
• If the algorithm is available, it is used otherwise a new one has to be
developed
39
2. Determining the capabilities of the computational device
• After understanding the problem, the capabilities of computing device should
be known.
• Type of architecture, speed, memory availability of the device are noted
40
3. Exact/approximate solution
• Develop an algorithm
• The algorithm must compute correct output for all possible and legitimate
inputs
• This solution can be an exact solution or approximate solution
41
4. Select the appropriate data structure
• Data structure is group of data elements that are put together, which defines
a particular way of storing and organizing data in computer so that it can be
used efficiently.
• The elementary data structures as follows
• List: Allows fast access of data
• Sets: Treats data as elements of set
• Dictionaries: Allows data to be stored as key-value pair
42
5. Algorithm design Techniques
• Developing an algorithm may never be fully automated.
• Using design techniques, we can develop new algorithms easily
• Example: Divide and Conquer
43
6. Methods of specifying an algorithm
• Algorithm is a sequence of steps or instructions to implement a solution.
• After writing an algorithm, it is specified either using a natural language or
pseudocode or flowcharts.
44
7. Proving algorithms correctness
• Writing an algorithm is not just enough
• Need to prove that it computes solutions for all possible inputs.
• This process is called as algorithm validation
• Algorithm validation ensures that algorithm will works correctly irrespective
of programming language
45
8. Analysing the performance of algorithms
• When an algorithm is executed, it uses the computer’s resources like central
processing unit (CPU) to perform its operations and to hold the program and
data respectively.
• An algorithm is analyzed to measure its performance in terms of CPU time
and memory space required to execute the algorithm.
• This is a challenging task and is often used to compare different algorithms
for a particular problem.
• The result of the comparison helps us to choose the best solution from all
possible solutions.
• Analysis of the algorithm also helps us to determine whether the algorithm
will be able to meet any efficiency constraint that exists or not.
46
Control Structures used in Algorithms
Main control structures are
•Sequence
•Decision
•Iteration/Repetition
•Recursion
47
Control Structures used in Algorithms
Control Structures used in Algorithms
48
Control Structures used in Algorithms
• A selection statement causes the program control to be
transferred to a specific part of the program based upon the
condition.
• If the conditional test is true, one part of the program will be
executed, otherwise it will execute the other part of the
program.
49
Contd.,
50
Control Structures used in Algorithms
51
Practice question
Write an algorithm for exchange the value of two variables
Step 1: Start
Step 2: Input a value for variable x
Step 3: Input a value for variable y
Step 4: perform temp=x
Step 5: x=y
Step 6: y= temp
Step 7: Print x and y
Step 8 : End
52
1. Algorithm: Check Odd or Even Number
Problem:
Given a number, check whether it is odd or even.
Algorithm:
Step 1: Start the Program
Step 2: Read the number n
Step 3: If n % 2 == 0
Print "Even Number"
Else
Print "Odd Number“
Step 4: Stop the Program
Control Structures used in Algorithms
53
1. Algorithm: Check Odd or Even Number
Problem:
Given a number, check whether it is odd or even.
Algorithm:
Step 1: Start the program.
Step 2: Prompt the user and read an integer n.
Step 3: Calculate the remainder when n is divided by 2 (i.e., compute n % 2).
Step 4: If the remainder equals 0, print “n is an even number.”
Step 5: Otherwise, print “n is an odd number.”
Step 6: End the program
Control Structures used in Algorithms
54
Algorithm — Check Voter Eligibility
Step 1: Start the program.
Step 2: Prompt the user and read the person’s age a.
Step 3: If age is greater than or equal to 18 (age >= 18), print “Eligible to
vote.”
Step 4: Otherwise, print “Not eligible to vote.”
Step 5: End the program.
55
• Example: Algorithm to find the greatest of three numbers
Step 1: Start
Step 2: Read the three numbers A,B,C
Step 3: Compare A and B. If A is greater perform step 4 else perform step 5
Step 4: Compare A and C. If A is greater, output “A is greatest” else output “c is
greatest”
Step 5: Compare B and C. If B is greater, output “B is greatest” else output “c is
greatest”
Step 6: End
56
Control Structures used in Algorithms
57
Iteration
• Iteration is the process of repeatedly executing a set of instructions or
statements until a certain condition is met.
• In programming, iteration is usually carried out using loops such as:
• for loop – repeats a block of code for a fixed number of times.
• while loop – repeats a block of code as long as a condition is true.
• do...while loop – executes the block of code at least once, and then
continues as long as the condition is true.
58
Algorithm: Print All Natural Numbers up to n
Step 1: Start the program.
Step 2: Read the value of n from the user.
Step 3: Initialize a variable i with the value 1.
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop
59
Algorithm: Find the Sum of First 10 Natural Numbers
Step 1: Start the program.
Step 2: Initialize a variable sum to 0.
Step 3: Initialize a variable i to 1.
Step 4: Repeat the following steps while i is less than or equal to 10: (i<=10)
a. Add the value of i to sum.
b. Increment i by 1.
Step 5: After the loop ends, print the value of sum.
Step 6: Stop the program.
61
Control Structures used in Algorithms
https://www.brainkart.com/article/Building-blocks-of-algorit
hms-(statements,-state,-control-flow,-functions)_35892
/
62
Control Structures used in Algorithms
63
Control Structures used in Algorithms
64
Flowcharts
 A flowchart is a graphical or symbolic representation of a process.
 When designing a flowchart, each step in the process is depicted by a different symbol and is
associated with a short description.
 A flowchart is a diagrammatic representation that illustrates the sequence of steps that must be
performed to solve a problem.
 It is usually drawn in the early stages of formulating computer solutions.
 It facilitates communication between programmers and users.
 Once a flowchart is drawn, programmers can make users understand the solution easily and clearly.
 Top Down approach
65
Refer Book Page No 8
Problem Solving and
Programming with
Python
Reema Thareja
Rules for drawing a flowchart
1. The flowchart should be clear, neat and easy to follow.
2. The flowchart must have a logical start and finish.
3. Only one flow line should come out from a process symbol.
4. Only one flow line should enter a decision symbol. However, two or three
flow lines may leave the decision symbol.
5. Only one flow line is used with a terminal symbol.
6. Within standard symbols, write briefly and precisely.
7. Intersection of flow lines should be avoided.
69
Flowcharts
70
Flowcharts
• Advantages
• They are very good communication tools to explain the logic of a system to all
concerned. They help to analyse the problem in a more effective manner.
• They are also used for program documentation.
• They direct the programmers to go from the starting point of the program to the
ending point without missing any step in between. This results in error-free programs.
• They help the programmers to easily detect, locate, and remove mistakes in the
program in a systematic manner.
71
Contd.,
• Limitations
• Drawing flowcharts is a laborious and a time-consuming activity.
• Many a times, the flowchart of a complex program becomes complex and clumsy.
• At times, a little bit of alteration in the solution may require complete redrawing of
the flowchart.
• The essentials of what is done may get lost in the technical details of how it is done.
• There are no well-defined standards that limit the details that must be incorporated
into a flowchart.
72
Pseudocodes
• Pseudocode is a compact and informal high-level description of an algorithm that uses the
structural conventions of a programming language.
• It facilitates designers to focus on the logic of the algorithm without getting bogged down by
the details of language syntax.
• An ideal pseudocode must be complete, describing the entire logic of the algorithm, so that it
can be translated straightaway into a programming language.
• It consist of short English phrases that explain specific tasks within a program’s algorithm. They
should not include keywords in any specific computer language.
• The sole purpose of pseudocodes is to enhance human understandability of the solution.
73
Pseudocodes
74
Programming Language
75
Programming
• Implementation of an algorithm
• A program is a set of instructions in a computer understandable language to
perform a certain task.
• Program on the other hand allows us to write a code in a particular
programming language.
76
Programming Languages
 A programming language is a language specifically designed to express computations that
can be performed by a computer.
 Programming languages have a vocabulary of syntax and semantics for instructing a
computer to perform specific tasks.
 Though high-level programming languages are easy for humans to read and understand,
the computer can understand only machine language, which consists of only numbers.
77
 In between machine languages and high-level languages, there is another
type of language known as assembly language.
 Irrespective of the language that a programmer uses, a program written
using any programming language has to be converted into machine
language so that the computer can understand it.
 There are two ways to do this: compile the program or interpret the
program.
78
79
Selecting a Particular Programming Language
• The type of computer hardware and software on which the program is to be
executed.
• The type of program.
• The expertise and availability of the programmers.
• Features to write the application.
• The built-in features that support the development of software that are
reliable and less prone to crash.
80
• Lower development and maintenance costs. Stability and capability to support
even more than the expected simultaneous users.
• Elasticity of a language that implies the ease with which new features or can be
added to the existing program.
• Portability.
• Better speed of development that includes the time it takes to write a code,
time taken to find a solution to the problem at hand, time taken to find the bugs,
availability of development tools etc.
Contd.,

CS25C01 Computer Programming C Chapter 1

  • 1.
    CS25C01 Computer Programming: C HandledBy Dr.R.VENKATESH, ASP & Head CSE (AIML) To shine in life, you must first step out of the shadows of shyness
  • 3.
  • 4.
    Problem Solving, ProblemAnalysis Chart, Developing an Algorithm, Flowchart and Pseudocode, program structure, Compilation & Execution process, Interactive and Script mode, Comments, Indentation, Error messages, Primitive data types, Constants, Variables, Reserved words, Arithmetic, Relational, Logical, Bitwise, Assignment, Conditional operators, Input / Output Functions, Built-in Functions. Practical: Create Problem Analysis Charts, Flowcharts and Pseudocode for simple C programs (Minimum three). Module I - Introduction to C
  • 5.
    5 Problem Solving • ProblemSolving is the process of identifying a problem, analyzing it, and finding an effective step-by-step solution to achieve the desired outcome. • Problem-solving in C programming involves a systematic approach to translate a real-world problem into a C program that provides a solution.
  • 6.
    6 This process typicallyfollows a series of steps: • Problem Definition and Understanding • Analysis • Design (Algorithm and Flowchart) • Coding (Implementation) • Testing and Debugging • Maintenance
  • 7.
  • 8.
  • 9.
    9 Problem Analysis Chart(PAC) Problem Analysis Chart (PAC) is a structured table used in problem solving to break down a given problem into four main parts: • Input – what data is required – Given Data • Process – the steps or logic to be applied • Output – the expected result – Required Result • Formula/Conditions – any equations or conditions involved – Solution A tabular representation. It is mainly used for understanding and analyzing the problem before writing an algorithm.
  • 10.
  • 11.
    11 Find the sumof two numbers Input Process Output Formula / Condition Number 1 (A) Read the two numbers Sum of two numbers SUM = A + B Number 2 (B) Add the two numbers (A + B)
  • 12.
    12 Largest of TwoNumbers Input Process Output Formula / Conditions Two integer numbers entered by the user (say a and b). 1. Read two numbers from the user. 2. Compare the first number (a) with the second number (b). 3. If a is greater than b, then a is the largest. 4. Otherwise, if b is greater than a, then b is the largest. 5. If both numbers are equal, display "Both are equal." The largest number among the two inputs  Condition 1: If a > b → Largest = a  Condition 2: If b > a → Largest = b  Condition 3: If a == b → Both are equal
  • 13.
    13 Check Whether aNumber is Even or Odd Input Process Output Formula / Conditions
  • 14.
  • 15.
  • 16.
    16 Todays Task Problem Statement-Check whether a person is eligible to vote or not (minimum age = 18 years). Problem Statement-Calculate the area of a circle given its radius.
  • 17.
    17 Check whether aperson is eligible to vote or not (minimum age = 18 years). Input Process Output Formula / Condition Age of person Read the age of the person Compare the entered age with 18: • If age ≥ 18 → Eligible • Else → Not eligible Message: “Eligible to vote” or “Not eligible to vote” If Age ≥ 18 → Eligible If Age < 18 → Not Eligible
  • 18.
    18 Calculate the areaof a circle given its radius. Input Process Output Formula / Condition Radius (r) 1. Read the radius Print Area of circle Area = π × r × r 2. Apply formula for area of circle
  • 19.
  • 20.
  • 21.
    21 Take off Check inbaggage Immigration check Boarding Security check Take off
  • 22.
  • 23.
    23 Algorithm input: Get datafrom the keyboard, a file, the network, or some other device. output: Display data on the screen, save it in a file, send it over the network, etc.
  • 24.
  • 25.
    25 Algorithm, Flow chart,Pseudo-code, Programming • Algorithm - Step- by-step procedure for solving a task or a problem • Flowchart – Diagrammatic representation of algorithm • Pseudo code – Representation of the algorithm in a way that is in between a program and a normal english • Programming - Implementation of an algorithm
  • 26.
  • 27.
    What is analgorithm? • It is a step- by-step procedure for solving a task or a problem. • Algorithm is an ordered sequence of finite, well defined, unambiguous instructions for completing a task. • Algorithm is an English-like representation of the logic which is used to solve the problem.
  • 28.
    28 Need of Algorithm •To understand the basic idea of the problem. • To improve the efficiency of existing techniques. • To understand the flow of the problem. • To compare the performance of the algorithm with respect to other techniques.
  • 29.
    29 Characteristics of anAlgorithm • Precision: The instructions should be written in a precise manner. • Uniqueness: The outputs of each step should be unambiguous, i.e., they should be unique and only depend on the input and the output of the preceding steps. • Finiteness: Not even a single instruction must be repeated infinitely. • Effectiveness: The algorithm should designed in such a way that it should be the most effective among many different ways to solve a problem. • Input: The algorithm must receive an input. • Output: After the algorithm gets terminated, the desired result must be obtained. • Generality: The algorithm can be applied to various set of inputs.
  • 30.
    30 Building Blocks ofAlgorithm • An algorithm starts from an initial state with some input. • The instructions/statements describe the processing that must be done on the input to produce the final output (the final state). • An instruction is a single operation which when executed converts one state to other. • In the course of processing, data is read from an input device, stored in computer’s memory for further processing, and then the result of the processing is written to an output device.
  • 31.
    31 Contd., • The datais stored in the computer’s memory in the form of variables or constants. • The state of an algorithm is defined as its condition regarding current values or contents of the stored data. • The flow of control specifies the order in which individual instructions of an algorithm are executed.
  • 32.
    32 Different Approaches ofDesigning an Algorithm • For a complex problem, its algorithm is often divided into smaller units called functions(or modules). This process of dividing an algorithm into modules/ functions is called modularization. Advantage of modularization: • Complex algorithm simpler to design and implement • Each function can be designed independently. While designing one function, the details of other functions can be ignored, thereby enhancing clarity in design which in turn simplifies implementation, debugging, testing and overall maintenance of algorithm
  • 33.
    33 Approaches • Top-down approach •Bottom-up approach Complex algorithm Function 1 Function n Function 2 Top down approach Bottom up approach Each function can be divided into one or more sub-functions
  • 34.
    34 Top-down approach • Dividethe complex problem into one or more functions • Those functions can be further decomposed into one or more sub-functions. • This process of decomposition is iterated until the desired level of module complexity is achieved. • Step-wise refinement: Begin with top most module and incrementally add function that it calls.
  • 35.
    35 Bottom-up approach • Reverseof top-down approach • Start with designing the most basic function and then proceed towards designing higher level functions • Higher level functions are implemented by using the operations performed by lower level functions • All higher level functions are grouped together to form even a higher-level function. • This process is repeated until the design of the complete algorithm is obtained
  • 36.
    36 Top down vsbottom up approach • Top-down approach is appreciated for ease in documenting the functions, generation of test cases, implementation of code and debugging. • It is criticized because the sub-functions are analyzed in isolation without concentrating on their communication with other functions thereby ignoring the concept of information hiding. • Bottom up approach allows information hiding by identifying which has to be encapsulated within a function but this is difficult to be done in all cases
  • 37.
    37 Algorithmic Problem SolvingSteps • 1. Understanding the problem • 2. Determining the capabilities of the computational device • 3. Exact/approximate solution • 4. Select the appropriate data structure • 5. Algorithm design techniques • 6. Methods of specifying an algorithm • 7. Proving an algorithms correctness • 8. Analyzing the performance of an algorithm
  • 38.
    38 1. Understanding theProblem • The problem given should be clearly and completely understood • Compared with earlier problems that have already been solved to check if it is similar and a known algorithm exists. • If the algorithm is available, it is used otherwise a new one has to be developed
  • 39.
    39 2. Determining thecapabilities of the computational device • After understanding the problem, the capabilities of computing device should be known. • Type of architecture, speed, memory availability of the device are noted
  • 40.
    40 3. Exact/approximate solution •Develop an algorithm • The algorithm must compute correct output for all possible and legitimate inputs • This solution can be an exact solution or approximate solution
  • 41.
    41 4. Select theappropriate data structure • Data structure is group of data elements that are put together, which defines a particular way of storing and organizing data in computer so that it can be used efficiently. • The elementary data structures as follows • List: Allows fast access of data • Sets: Treats data as elements of set • Dictionaries: Allows data to be stored as key-value pair
  • 42.
    42 5. Algorithm designTechniques • Developing an algorithm may never be fully automated. • Using design techniques, we can develop new algorithms easily • Example: Divide and Conquer
  • 43.
    43 6. Methods ofspecifying an algorithm • Algorithm is a sequence of steps or instructions to implement a solution. • After writing an algorithm, it is specified either using a natural language or pseudocode or flowcharts.
  • 44.
    44 7. Proving algorithmscorrectness • Writing an algorithm is not just enough • Need to prove that it computes solutions for all possible inputs. • This process is called as algorithm validation • Algorithm validation ensures that algorithm will works correctly irrespective of programming language
  • 45.
    45 8. Analysing theperformance of algorithms • When an algorithm is executed, it uses the computer’s resources like central processing unit (CPU) to perform its operations and to hold the program and data respectively. • An algorithm is analyzed to measure its performance in terms of CPU time and memory space required to execute the algorithm. • This is a challenging task and is often used to compare different algorithms for a particular problem. • The result of the comparison helps us to choose the best solution from all possible solutions. • Analysis of the algorithm also helps us to determine whether the algorithm will be able to meet any efficiency constraint that exists or not.
  • 46.
    46 Control Structures usedin Algorithms Main control structures are •Sequence •Decision •Iteration/Repetition •Recursion
  • 47.
    47 Control Structures usedin Algorithms Control Structures used in Algorithms
  • 48.
    48 Control Structures usedin Algorithms • A selection statement causes the program control to be transferred to a specific part of the program based upon the condition. • If the conditional test is true, one part of the program will be executed, otherwise it will execute the other part of the program.
  • 49.
  • 50.
  • 51.
    51 Practice question Write analgorithm for exchange the value of two variables Step 1: Start Step 2: Input a value for variable x Step 3: Input a value for variable y Step 4: perform temp=x Step 5: x=y Step 6: y= temp Step 7: Print x and y Step 8 : End
  • 52.
    52 1. Algorithm: CheckOdd or Even Number Problem: Given a number, check whether it is odd or even. Algorithm: Step 1: Start the Program Step 2: Read the number n Step 3: If n % 2 == 0 Print "Even Number" Else Print "Odd Number“ Step 4: Stop the Program Control Structures used in Algorithms
  • 53.
    53 1. Algorithm: CheckOdd or Even Number Problem: Given a number, check whether it is odd or even. Algorithm: Step 1: Start the program. Step 2: Prompt the user and read an integer n. Step 3: Calculate the remainder when n is divided by 2 (i.e., compute n % 2). Step 4: If the remainder equals 0, print “n is an even number.” Step 5: Otherwise, print “n is an odd number.” Step 6: End the program Control Structures used in Algorithms
  • 54.
    54 Algorithm — CheckVoter Eligibility Step 1: Start the program. Step 2: Prompt the user and read the person’s age a. Step 3: If age is greater than or equal to 18 (age >= 18), print “Eligible to vote.” Step 4: Otherwise, print “Not eligible to vote.” Step 5: End the program.
  • 55.
    55 • Example: Algorithmto find the greatest of three numbers Step 1: Start Step 2: Read the three numbers A,B,C Step 3: Compare A and B. If A is greater perform step 4 else perform step 5 Step 4: Compare A and C. If A is greater, output “A is greatest” else output “c is greatest” Step 5: Compare B and C. If B is greater, output “B is greatest” else output “c is greatest” Step 6: End
  • 56.
  • 57.
    57 Iteration • Iteration isthe process of repeatedly executing a set of instructions or statements until a certain condition is met. • In programming, iteration is usually carried out using loops such as: • for loop – repeats a block of code for a fixed number of times. • while loop – repeats a block of code as long as a condition is true. • do...while loop – executes the block of code at least once, and then continues as long as the condition is true.
  • 58.
    58 Algorithm: Print AllNatural Numbers up to n Step 1: Start the program. Step 2: Read the value of n from the user. Step 3: Initialize a variable i with the value 1. Step 4: if (i<=n) go to step 5 else go to step 7 Step 5: Print i value and increment i value by 1 Step 6: go to step 4 Step 7: Stop
  • 59.
    59 Algorithm: Find theSum of First 10 Natural Numbers Step 1: Start the program. Step 2: Initialize a variable sum to 0. Step 3: Initialize a variable i to 1. Step 4: Repeat the following steps while i is less than or equal to 10: (i<=10) a. Add the value of i to sum. b. Increment i by 1. Step 5: After the loop ends, print the value of sum. Step 6: Stop the program.
  • 60.
    61 Control Structures usedin Algorithms https://www.brainkart.com/article/Building-blocks-of-algorit hms-(statements,-state,-control-flow,-functions)_35892 /
  • 61.
  • 62.
  • 63.
    64 Flowcharts  A flowchartis a graphical or symbolic representation of a process.  When designing a flowchart, each step in the process is depicted by a different symbol and is associated with a short description.  A flowchart is a diagrammatic representation that illustrates the sequence of steps that must be performed to solve a problem.  It is usually drawn in the early stages of formulating computer solutions.  It facilitates communication between programmers and users.  Once a flowchart is drawn, programmers can make users understand the solution easily and clearly.  Top Down approach
  • 64.
  • 65.
    Problem Solving and Programmingwith Python Reema Thareja
  • 66.
    Rules for drawinga flowchart 1. The flowchart should be clear, neat and easy to follow. 2. The flowchart must have a logical start and finish. 3. Only one flow line should come out from a process symbol. 4. Only one flow line should enter a decision symbol. However, two or three flow lines may leave the decision symbol.
  • 67.
    5. Only oneflow line is used with a terminal symbol. 6. Within standard symbols, write briefly and precisely. 7. Intersection of flow lines should be avoided.
  • 68.
  • 69.
    70 Flowcharts • Advantages • Theyare very good communication tools to explain the logic of a system to all concerned. They help to analyse the problem in a more effective manner. • They are also used for program documentation. • They direct the programmers to go from the starting point of the program to the ending point without missing any step in between. This results in error-free programs. • They help the programmers to easily detect, locate, and remove mistakes in the program in a systematic manner.
  • 70.
    71 Contd., • Limitations • Drawingflowcharts is a laborious and a time-consuming activity. • Many a times, the flowchart of a complex program becomes complex and clumsy. • At times, a little bit of alteration in the solution may require complete redrawing of the flowchart. • The essentials of what is done may get lost in the technical details of how it is done. • There are no well-defined standards that limit the details that must be incorporated into a flowchart.
  • 71.
    72 Pseudocodes • Pseudocode isa compact and informal high-level description of an algorithm that uses the structural conventions of a programming language. • It facilitates designers to focus on the logic of the algorithm without getting bogged down by the details of language syntax. • An ideal pseudocode must be complete, describing the entire logic of the algorithm, so that it can be translated straightaway into a programming language. • It consist of short English phrases that explain specific tasks within a program’s algorithm. They should not include keywords in any specific computer language. • The sole purpose of pseudocodes is to enhance human understandability of the solution.
  • 72.
  • 73.
  • 74.
    75 Programming • Implementation ofan algorithm • A program is a set of instructions in a computer understandable language to perform a certain task. • Program on the other hand allows us to write a code in a particular programming language.
  • 75.
    76 Programming Languages  Aprogramming language is a language specifically designed to express computations that can be performed by a computer.  Programming languages have a vocabulary of syntax and semantics for instructing a computer to perform specific tasks.  Though high-level programming languages are easy for humans to read and understand, the computer can understand only machine language, which consists of only numbers.
  • 76.
    77  In betweenmachine languages and high-level languages, there is another type of language known as assembly language.  Irrespective of the language that a programmer uses, a program written using any programming language has to be converted into machine language so that the computer can understand it.  There are two ways to do this: compile the program or interpret the program.
  • 77.
  • 78.
    79 Selecting a ParticularProgramming Language • The type of computer hardware and software on which the program is to be executed. • The type of program. • The expertise and availability of the programmers. • Features to write the application. • The built-in features that support the development of software that are reliable and less prone to crash.
  • 79.
    80 • Lower developmentand maintenance costs. Stability and capability to support even more than the expected simultaneous users. • Elasticity of a language that implies the ease with which new features or can be added to the existing program. • Portability. • Better speed of development that includes the time it takes to write a code, time taken to find a solution to the problem at hand, time taken to find the bugs, availability of development tools etc. Contd.,