0% found this document useful (0 votes)
7 views113 pages

python moule 1

The document outlines the course CSE1021: Introduction to Problem Solving and Programming at VIT Bhopal University, detailing the first unit which covers problem-solving techniques, algorithm development, flowcharting, coding, and debugging. It emphasizes the importance of understanding problem analysis, algorithm design, and the differences between programming languages and their execution methods. The document also includes examples of algorithms, flowcharts, and pseudocode to illustrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views113 pages

python moule 1

The document outlines the course CSE1021: Introduction to Problem Solving and Programming at VIT Bhopal University, detailing the first unit which covers problem-solving techniques, algorithm development, flowcharting, coding, and debugging. It emphasizes the importance of understanding problem analysis, algorithm design, and the differences between programming languages and their execution methods. The document also includes examples of algorithms, flowcharts, and pseudocode to illustrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 113

VIT Bhopal University

Bhopal-Indore Highway, Kothrikalan, Sehore,


Madhya Pradesh – 466114.

Course Code
CSE1021
Introduction to Problem Solving and
Programming

Lecture -1 Credits 4
Unit -1
(8-Hours)

VIT Bhopal
01
Unit
Introduction to Computer Problem
Solving
Mapping Course outcome
ASSESMENT DETAILS
Unit -1
Introduction to Computer Problem Solving:

● Introduction – Problem Solving Aspect


● Top-Down Design
● Implementation of Algorithms (flowcharts, pseudo
code, programming language)
● Program Verification
● Efficiency of Algorithms
● Analysis of Algorithms.
Introduction
• Number of problems in our daily life.
• Suppose we have to calculate Simple Interest.
• Suppose we have to prepare a mark sheet.

❑ A computer is a DUMB machine.


❑ A computer cannot do anything alone without software
i.e. Program
Types of Computer
Software System Software
Example:-Operating
Overview system is the type of
system software
Describe here the topic of the
section Application Software
Example is Microsoft Office
Programming
Topic Features Languages
C, C++,java,
Python
Steps in problem solving
Problem Analysis

Algorithm Development

Flowcharting

Program Coding

Compilation and Execution

Debugging and Testing

Documentation
N1 = int(input("enter the first no:"))
N2 = int(input("enter the second no:"))
subtraction = N1 - N2
print("the sum is:",subtraction)
Problem Analysis
• Process of becoming familiar with the problem.

• We need to analyze and understand it well before solving.

• The user’s requirements cannot be fulfilled without clear


understanding of his/her problem in depth.

• Inadequate identification of problem may cause program less


useful and insufficient.

• Example: Banking Solution, Hospital Medical Study


Algorithm Development
● A procedure for solving a mathematical problem in a finite
number of steps that may frequently involves repetition of
an operation.
OR
● A step-by-step procedure for solving a problem or
accomplishing some end
OR
● Developing an algorithm is a step of program design.
Differences Between Program And
Algorithm
An Example of algorithm to find sum of two numbers
Step 1: Start

Step 2: Assume two numbers x and y and a variable sum=0

Step 3: Add two numbers x and y; store the value in variable sum

Step 4: If you want to try again with different numbers then goto step 2
else
goto step 5
Step 5: END

An algorithm to find sum of two
Step 1: Start
numbers:
Step 2: Declare variables num1, num2 and sum. Step 3: Read
values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum Step 6: Stop
Flowcharting
• Graphical representation of an algorithm using standard symbols.
• Includes a set of various standard shaped boxes that are
interconnected by flow lines.

• Flow lines have arrows(direction of flow).

• Activities are written within boxes in English.

• Communicates between programmers and business persons.


STAR
T Start
Read two
Numbers, A and B
Input

C=A+B Processing

Display C as Sum of A and B


Output
END
End
Coding
• The process of transforming the program logic design into computer
language format.

• An act of transforming operations in each box of the flowchart in terms of


the statement of the program.

• The code written using programming language is also known


● as source code.

• Coding isn’t the only task to be done to solve a problem using computer.

• Anyone can code. TRUST ME!!


Compilation
• Process of changing high level language into machine level
language.

• It is done by special software, COMPILER

• The compilation process tests the program whether it


● contains syntax errors or not.

• If syntax errors are present, compiler can not compile the


code.
Execution
• Once the compilation is completed then the program is
linked with other object programs needed for execution,
there by resulting in a binary program and then the program
is loaded in the memory for the purpose of execution and
finally it is executed.

• The program may ask user for inputs and generates outputs
after processing the inputs.
Debugging and Testing
• Testing ensures that program performs correctly the required task.

• Verification ensures that program does what the programmer


intends to do.

• Validation ensures that the program produces the correct results


for a set of test data.

• Test data are supplied to the program and output is observed.

• Expected output = Error free


Types or levels of Programming
Languages
• High Level

● Low-level
– Machine Level
– Assembly Level
Problem Solving
● Solving problems is the core of computer science.

● Programmers must first understand how a human solves a


problem.

● Then understand how to translate this "algorithm" into


something a computer can do.

● finally how to "write" the specific syntax (required by a


computer) to get the job done.
● It is sometimes the case that a machine will solve a problem in a
completely different way than a human.
Machine Language
ADVANTAGE DISADVANTAGE
Computer directly understands Difficult to use
machine instructions
Directly starts executing Machine dependent
Takes less execution time Difficult to Debug and modify

It is relatively easy for writing programs in assembly languages, but is


slow in execution as it has to be converted into machine language
before execution.
Some examples of instructions for
assembly languages are as follows:
CODE Meaning
ADD Addition
SUB Subtraction
INR Increase
DCR Decrease
CMP Compare
High Level Language
• User friendly, Similar to natural languages
• Platform independent
• Easy to write or remember
• Easy to learn and work
• While execution: translated into assembly language then to
machine language.
• Slow in execution but is efficient for developing programs.
• Ex: C, C++, Python, Java etc.
High Level Language

Advantages Disadvantages
Easy to use More execution time
Portability Needs own translator
Easy Debugging
Easy and Fast Development of
software
Compiler
• A high level source program must be translated into a form machine can
understand. This done by software called the compiler.

• Source code => Machine language code(Object code)

• During the process of translation, the compiler reads the source


● programs statement-wise and checks for syntax errors.

• In case of any error, the computer generates message about the error.

• Ex: C, C++, Java, FORTRAN, pascal etc.


Interpreter
• Like compiler, it is also a translator which translates high
level to machine level language.

• Translates and executes the program line by line.

• Each line is checked for syntax error and then converted to


● the equivalent machine code.

• Ex. QBASIC, PERL, PHP, ASP, PYTHON, RUBY


Compiler Interpreter

Difference between compiler and


Compiler scans the entire program before Interpreter translates and executes the
translating it into machine code program line by line
interpreter
Syntax errors are found only after the Syntax errors can be trapped after translations
compilation of complete programs of every line

It takes less amount of time to analyze the


It takes large amount of time to analyze the
source code but the overall execution time is
source code but the overall execution time is
slower.
comparatively faster.

No intermediate object code is generated,


Generates intermediate object code which
hence are memory efficient.
further requires linking, hence requires more
memory.
Top-Down Design

● If you have a big problem to solve, then a very


effective method of working towards a solution
is to break it down into smaller, more
manageable problems.
● This is the idea behind 'top-down design'.
Another term used is 'Stepwise refinement'.
Example

A software program is
needed to handle the lights in
a concert stadium. There are
hundreds of lights, all
producing complicated
sequences and effects.
Approach
Before a single line of code is
written, a structure chart is
drawn. Something like this:
Conti….
• A 'module' is ideally a self-contained block of
code.
• The code within has a clearly defined set of inputs
and outputs, this is called an 'interface'.
• Each module has a specific function to perform.
Its actions are controlled by a master program
loop.
• This is like the conductor in an orchestra, making
the whole thing work in harmony.
Conti..
● The control module is concerned with
controlling the hardware. It does this by
communicating and controlling a number
of sub-modules.
● These sub-modules are responsible for
certain types of light.
What is an ● It is a step-by-step
algorithm? instruction for solving a
▪ An algorithm defined as a particular task in finite
sequence of definite and amount of time.
effective instructions, which
terminates with production
of correct output with given
input. • Algorithm is an effective
method for solving a problem
expressed as finite sequence
of instructions.
Writing the
Algorithm
● Algorithm is generally ● Sometimes algorithms are
developed before the actual written using pseudocodes,
coding is done. i.e. a language similar to the
● It is written using English programming language to be
like language so that it is used.
easily understandable even
by non-programmers.
Writing algorithm for solving a
problem offers these advantages

● Promotes effective communication between team
members
● Enables analysis of problem at hand
● Acts as blueprint for coding
● Assists in debugging
● Becomes part of software documentation for future
reference during maintenance phase
characteristics of a good and
correct algorithm −
● Has a set of inputs
● Steps are uniquely defined
● Has finite number of steps
● Produces desired output
Here is the algorithm for going to
the market to purchase a pen.
Step 1:- Get dressed to go to market.
Step 2:- Check your wallet for money.
Step 3:- is there is no money in the wallet replenish it.
Step 4:- Go to the shop.
Step 5:- Ask for your favorite brand of pen.
Step 6:- If pen is not available, go to step 7else to step 10.
Step 7:- Give money to shopkeeper.
Step 8:- Keep the purchased pen safely.
Step 9:- Go back to home.
Step 10:- Ask for other brand of pen.
Step 11:- Go to Step 7.
Let us now create an algorithm to
check whether a number is positive
or negative.

1. Print “ Give any number”


2. Read num
3. if(num==0) print “You entered 0”
4. if (num>0) print “you entered a positive number”
5. if (num<0) print “You entered a negative number”
Questions
1. Write an Algorithm for calculating Square of a number.
2. Write an Algorithm to multiply two numbers.

Answer 1: solution Answer 2:- solution


1) Start 1) Start
2) Give input as a number 2) Accept Number one
3) Read the number 3) Accept Number two
4) Self Multiply of input 4) Multiply both the numbers
5) Print the result. 5) Print the result.
6) End 6) End
Algorithm Development Steps
What is Flowchart?

● A flowchart is a ● A flowchart can be


diagrammatic helpful for both writing
representation of an programs and explaining
algorithm, a step-by-step the program to others.
approach to solving a task.
FLOW CHARTS

A flow chart is a
step by step diagrammatic representation of the logic paths to

solve a given problem.


visual or graphical representation of an algorithm.

Pictorial representation of the methods to be used to solve a


given problem.
help a great deal to analyze the problem and plan its solution in

a systematic and orderly manner.


ADVANTAGES OF FLOW CHARTS

1. The flowchart shows the logic of a problem displayed in pictorial


fashion which felicitates easier checking of an algorithm.
2. The Flowchart is good means of communication to other users. It is
also a compact means of recording an algorithm solution to a problem.
3. The flowchart allows the problem solver to break the problem into
parts. These parts can be connected to make master chart.
4. The flowchart is a permanent record of the solution which can be
consulted at a later time.
EXAMPLE 3
A=10
Que: Draw flowchart to test a number for even or odd? A==10
SYMBOLS USED IN FLOW CHART

Oval:
Also called ‘Terminator’ symbol. Used to
indicate start and end of the flow.

Input and output indicators:


Also called ‘Data’ symbol. Statements like
INPUT, READ and PRINT are represented in
these Parallelograms.
SYMBOLS USED IN FLOW CHART

Process:
Also called ‘Action’ symbol. It indicates a
process action such as for storing
arithmetic operations.

Decision Maker:
A decision or branching point, usually a
yes/no or true/false question is asked, and
based on the answer, the path gets split
into two branches.
SYMBOLS USED IN FLOW CHART

Arrow or Flow Lines:


Connector to show order of flow between shapes, or
the direction being followed in the flowchart.
EXAMPLE

Draw a flowchart to solve the problem of a non-functioning lamp?


EXAMPLE 2

Que: Draw a flowchart to find out the biggest of the three unequal positive
numbers.
PseudoCode
PSEUDO CODE

• A pseudocode (pronounced Soo-doh-kohd, the word “pseudo” means “not


real,” so “pseudocode” means “not real code”. ) is another way of
representing an algorithm.
• Its is neither an algorithm nor a program. It is an abstract form of a
program.
• It consists of English like statements which perform the specific operations.
• It helps programmers to write algorithm.
• It is a detailed description of instructions that a computer must follow in a
particular order.
• Advantages: * Easy to read, * Easy to understand, * Easy to modify.
How to write a Pseudo-code?

• Elaborate everything which is going to happen in the


actual code. Don’t make the pseudo code abstract.

• Use standard programming structures such as ‘if-then’,


‘for’, ‘while’, ‘cases’ the way we use it in programming.
• Check whether all the sections of a pseudo code is
complete, finite and clear to understand and
comprehend.
• Don’t write the pseudo code in a complete
programmatic manner. It is necessary to be simple to
understand even for a layman or client, hence don’t
incorporate too many technical terms.
Advantages of Pseudocode

I. Improves the readability of any approach. It’s one of the


best approaches to start implementation of an algorithm.

II. Acts as a bridge between the program and the algorithm


or flowchart.
III. Also works as a rough documentation, so the program of
one developer can be understood easily when a pseudo
code is written out. In industries, the approach of
documentation is essential.
IV. The main goal of a pseudo code is to explain what
exactly each line of a program should do, hence making
the code construction phase easier for the programmer.
Disadvantages of Pseudocode

I. Pseudocode does not provide a visual representation


of the logic of programming.
II. There are no proper format for writing the for
pseudocode.
III. In Pseudocode their is extra need of maintain
documentation.
IV. In Pseudocode their is no proper standard very
company follow their own standard for writing the
pseudocode.
PSEUDO CODE EXAMPLE

Que: Write a pseudo code to perform the basic arithmetic operations.


Read n1, n2
Sum = n1 + n2
Diff = n1 – n2
Mult = n1 * n2
Quot = n1/n2
Print sum, diff, mult, quot
End.
PSEUDO CODE EXAMPLE

● INPUT
● COMPUTE
● PRINT
● INCREMENT
● DECREMENT
● IF/ELSE
● WHILE
● TRUE/FALSE
An imperative language uses a sequence of statements to determine how
to reach a certain goal.

Object Oriented programming (OOP) is a programming paradigm that


relies on the concept of classes and objects. It is used to structure a
software program into simple, reusable pieces of code blueprints (usually
called classes), which are used to create individual instances of objects.

Prolog is a logic programming language. It has important role in artificial


intelligence. Unlike many other programming languages, Prolog is intended
primarily as a declarative programming language. In prolog, logic is expressed
as relations (called as Facts and Rules).

Functional programming languages are specially designed to handle symbolic


computation and list processing applications. Functional programming is
based on mathematical functions.
1. JavaScript 3. HTML
2. Python
What this language is used for: What this language is used for:
What this language is used for:
Web development Web documents
Back end development
Game development Website development
Data science
Mobile apps Website maintenance
App development
Building web servers

4. CSS 5. Java
What this language is used for: 6. SQL
What this language is used for: What this language is used for:
Web documents E-commerce
Website development Database management
Finance Sales reports
Website design App development Business management
Program verification
● A formal proof system for proving programs correct.
● A proof can provide confidence of correctness in a situation where exhaustive
semantic checking is time-consuming or impossible
Errors Checked by Compiler:-
Syntax errors:
● Errors that occur when you violate the rules of writing C/C++ syntax are known as
syntax errors. This compiler error indicates something that must be fixed before the
code can be compiled. All these errors are detected by compiler and thus are known
as compile-time errors.
Most frequent syntax errors are:
○ Missing Parenthesis (})

○ Printing the value of variable without declaring it

○ Missing semicolon like this:


Run-time Errors :
● Errors which occur during program execution(run-time) after successful
compilation are called run-time errors.
● One of the most common run-time error is division by zero also known as
Division error.
● These types of error are hard to find as the compiler doesn’t point to the line at
which the error occurs.

● warning: division by zero [-Wdiv-by-zero] div = n/0;


Linker Errors:
● These error occurs when after compilation we link the different object files with
main’s object using Ctrl+F9 key(RUN).
● These are errors generated when the executable of the program cannot be generated.
● This may be due to wrong function prototyping, incorrect header files.
Logical Errors :

● On compilation and execution of a program, desired output is not obtained when


certain input values are given.
● These types of errors which provide incorrect output but appears to be error free
are called logical errors.
● These are one of the most common errors done by beginners of programming.
Semantic errors : This error occurs when the statements written in the program are
not meaningful to the compiler.
Algorithm analysis
● Analysing an algorithm means solving a problem
statement of an algorithm with different
approaches.

● It also means to check for the best algorithm.


Algorithm analysis
Analysis of an algorithm performed in two phases:
1. Prior estimate: It is concerned with
theoretical and mathematical calculation that
uses asymptotic notations for its representation.

2. Posterior estimate: It is concerned with


writing a program and therefore depends on
operating system, compiler, etc.
Algorithm analysis
● Algorithm analysis is determination of time,
amount of memory utilized and other resources by
the system to execute a particular task.

● The running time of an algorithm for particular


input based upon the number of operations executed
on that particular size of the input.

● The greater the number of operations performed,


the longer the running time of an algorithm.
Algorithm analysis
● Consider the input size denoted as n, and then the running time of that
problem is function f(n).
● Running time of an algorithm measured using following steps:
1. Computing primary functions: Primary functions are instructions with
constant implementation time.
(a) Assignment of variables.
(b) Performing arithmetic operations like addition of numbers,
subtraction of numbers, etc.
(c) Comparing two numbers.
(d) Defining a function and calling that function.
2. Computing operation of input size n: Growth rate represented in algorithm
as function f(n).
Comparing algorithms
● Algorithms are compared based on its time and space
complexity.
● Algorithms with less time complexity and less memory space
is said to be a better algorithm.
Performance analysis of an algorithm depends on two factors:
1. Space complexity: The amount of memory space required
by an algorithm to complete its execution.

2. Time complexity: The amount of time required by an


algorithm to complete its execution. Number of step
count taken by algorithm to complete its task.
Comparing algorithms
● There are three types of step count:
(a) Best case: The best-case step count is the minimum
number of steps executed for given parameters.
(b) Worst case: The worst-case step count is the
maximum number of steps executed for given
parameters.
(c) Average case: The average case step count is the
average number of steps executed for given
parameters
There are three different
Asymptotic Notation notations: big O, big Theta
(Θ), and big Omega (Ω).
Asymptotic Notation is used
to describe the running time •Big-Θ is used when the
of an algorithm - how much running time is the same for
time an algorithm takes all cases,
with a given input, n. •Big-O for the worst case
running time
•Big-Ω for the best case
running time.
109
Big-O Notation (O-notation) – Worst Case-Upper Limit
110
Omega Notation (Ω-notation) – Lower Bound-Best Case
111

Theta Notation (Θ-notation) – Upper & Lower Bound


112
Little o (o) and Little Omega (ω) – Loose Upper & Lower Bounds
Thanks!

Do you have
any
questions?

You might also like