0% found this document useful (0 votes)
2 views

Xi Cs Unit-2 Introduction to Problem Solving

The document provides an introduction to problem-solving in computational thinking and programming, outlining the steps involved such as analyzing the problem, developing algorithms, coding, testing, and debugging. It covers the representation of algorithms using flowcharts and pseudocode, as well as the concepts of testing, debugging, and decomposition. Key definitions, examples, and types of errors in programming are also discussed to enhance understanding of the problem-solving process.

Uploaded by

himankd61
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Xi Cs Unit-2 Introduction to Problem Solving

The document provides an introduction to problem-solving in computational thinking and programming, outlining the steps involved such as analyzing the problem, developing algorithms, coding, testing, and debugging. It covers the representation of algorithms using flowcharts and pseudocode, as well as the concepts of testing, debugging, and decomposition. Key definitions, examples, and types of errors in programming are also discussed to enhance understanding of the problem-solving process.

Uploaded by

himankd61
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

XI CS

Unit-2: Computational Thinking & Programming


Topic: Introduction to Problem Solving

Prepared By:
Mukesh Chourasia
PGT (Computer Science)
PM Shri Kendriya Vidyalaya No.2 CPE Itarsi
Syllabus

 Introduction to Problem-solving: Steps for Problem-solving


(Analyzing the problem, developing an algorithm, coding, testing,
and debugging),
 Representation of algorithms
 Using flowchart and pseudocode
 Testing and Debugging
 Decomposition
Introduction to Problem-solving

Q. What is Problem Solving?


Ans. Precise step-by-step instructions given by us to solve a problem is
known as Problem Solving.

Q. What are the steps involved in problem solving cycle?


There are 3 basic steps involved in problem solving cycle
I. Identify the problem
II. Design Solution (developing an algorithm for the identified problem)
III. Implement the algorithm to develop a computer program to solve the
problem.
Introduction to Problem-solving

Q. Explain various steps involved in Problem Solving.


Ans. Step 1. Understand the problem well
Step 2. Analyze the problem to
a) Identify minimum number of inputs required for output
b) Identify processing components.
Step 3. design the program by
a) Deciding step by step solution.
b) Breaking down solution into simple steps.
Step 4. Code the program by
a) Identifying arithmetic and logical operation required for solution.
b) Using appropriate control structure such as conditional or looping
control structure.
Step 5. Test and Debug your program by
a) Finding error in it.
b) Rectifying the error.
Step 6. Complete your documentation.
Step 7. Maintain your program.
Representation of algorithms

Q. What is an Algorithm?
Ans. An Algorithm is a step-by-step process of solving a well-defined
computational problem. It consists of a finite number of well-defined
instructions written in simple English language which will eventually produce
output, when executed.

Q. What are the characteristics of an Algorithm?


Ans. 1. Each and every instruction should be short and clear.
2. Each instruction should be such that it can be performed in a finite
time.
3. One or more instructions should not be repeated infinitely.
4. After performing the instructions the desired result must be obtained.
Representation of algorithms

Q. What are the different types of flow of control in a program?


Ans. Flow of execution of statements/ Flow of control/ control structure
The way of execution of the program shall be categorized into three ways:
(i) sequence statements (normal flow);
(ii) selection statements (decision making); and
(iii) iteration statements(looping).

Q. Explain sequence flow of control.


Ans. In sequence flow, all the instructions are executed one after
another. It is the default flow of control in a program.

Q. Write an algorithm to calculate sum of two given numbers.


Step 1: Start.
Step 2: Input A, B.
Step 3: Sum=A+B.
Step 4: Print Sum.
Step 5: Stop.
Representation of algorithms

Q. Write an algorithm to find area of a rectangle.


Step 1: Start
Step 2: Take length and breadth and store them as L and B
Step 3: Multiply by L and B and store it in area
Step 4: Print area
Step 5: Stop

Q. Explain selection flow of control?


Ans. In this flow of control, some portion of the program is executed
based upon a test condition. The outcome of the test condition leads to
branching i.e. if the test condition evaluates to true, the program control
follows one course of action otherwise other course of action is followed.
The test condition is used to make a decision hence it is also called
decision making or branching.
if, if-else and if-elif-else statements are used for implementing selection
flow of control (decision making) in a program
Representation of algorithms

Q. Write an algorithm to check whether a person is eligible to vote or


not? (more than or equal to 18 years old).
Step 1: Start
Step 2: Enter the age
Step 3: if age >= 18 then go to step 4 else step 5
Step 4: Print “Eligible to vote” and go to step 6
Step 5: Print “Not eligible to vote”
Step 6: Stop
Q. Write an algorithm to find whether a given number is +ve, -ve or
zero.
Step 1: Start
Step 2: Enter the number
Step 3: if n > 0 then go to step 5 else go to step 4
Step 4: if n < 0 then go to step 6 else go to step 7
Step 5: Print “Given number is +ve” and go to step 8
Step 6: Print “Given number is -ve” and go to step 8
Step 7: Print “Given number is zero”
Step 8: Stop
Introduction to Problem-solving

Q. Explain Iteration Flow of Control.


Ans. In some programs, certain set of statements are executed again
and again based upon conditional test. i.e. executed more than one time.
This type of execution is called “looping or “iteration‟. The set of
statements are executed as long as the test condition evaluates to True.
As soon as the condition evaluates to False, the loop terminates.
If the condition does not eventually become False, the loop continues to
execute infinitely and is known as infinite loop.
for and while statements are used to implement iteration flow of control.
Q. Write an algorithm to print all natural numbers up to “n‟.
Step 1: Start
Step 2: Take any number and store it in n.
Step 3: Store 1 in I
Step 4: Check I value, if I<=n then go to step 5 else go to step 8
Step 5: Print I
Step 6: Increment I value by 1
Step 5: Go to step 4
Step 8: Stop
Using flowchart and pseudocode

Q. What is a flowchart? What are the symbols used in a flowchart?


Ans. A flowchart is a pictorial representation of an algorithm or a process,
that uses symbols and arrows to depict various steps involved and the flow
of control between various steps. The flowchart is then converted into
program code.
Flowchart Symbols
Using flowchart and pseudocode

Flowchart Symbols
Using flowchart and pseudocode

Q. Explain the role of different symbols used in a flowchart.


1. Terminal Box: This is also called start/stop box. It is oval shaped.
When it is used in beginning it is called START box and when used at end it
is called STOP box.
2. Input/ Output Box: This parallelogram shaped box is used for
input/output and display operations.
3. Process Box: This rectangle shaped box is used for any type of
computation.
4. Decision Box: This diamond box is used to represent decisions based on
a condition.
5. Flow Lines: These arrowhead line depicts direction of flow of data and
information, in addition to connecting different boxes of the flowchart.
Using flowchart and pseudocode

Q. Draw a flowchart to find the simple interest. (Sequence flow)


Solution:
Using flowchart and pseudocode

Q. Draw a flowchart to find bigger number among two numbers


(selection flow)
Solution:
Using flowchart and pseudocode

Q. Draw a flow chart to find factorial of any number. (Iteration flow)


Solution:
Using flowchart and pseudocode

Q. Draw a flow chart to find biggest number among “n‟ numbers.


(Iteration flow)
Solution:
Using flowchart and pseudocode

Q. What is Pseudocode?
Ans. Pseudocode is a simple way of describing a set of instructions that
does not have to use specific syntax. It is not a programming language.

Q. What are the notations used to write Pseudocode?


Ans. There is no strict set of standard notations for pseudocode, but
some of the most widely recognised are:
1. INPUT/READ–indicates a user will be inputting something
2. OUTPUT/WRITE–indicates that an output will appear on the screen
3. WHILE–a loop (iteration that has a condition at the beginning)
4. FOR–a counting loop (iteration)
5. REPEAT–UNTIL –a loop (iteration) that has a condition at the end
6. IF –THEN –ELSE –a decision (selection) in which a choice is made
any instructions that occur inside a selection or iteration are usually
indented
Using flowchart and pseudocode

Q. What is the difference between an algorithm and pseudo-code


Ans. Difference between algorithm and pseudo-code
Algorithm Pseudocode
It is a well-defined sequence of It is one of the methods that can
steps that provides a solution for be used to represent an algorithm.
a given problem
algorithms can be written in pseudocode is written in a format
natural language that is closely related to high
level programming language
structures.
Transforming an algorithm written Transforming an algorithm
in natural language to programming presented in pseudocode to
code could be more difficult programming code could be much
easier
Using flowchart and pseudocode

Q. Write an algorithm using Pseudo-code to find out largest of two


numbers
Ans.
Write "enter 2 numbers"
Read n1,n2
If(n1 > n2)
Set max to n1
Else
Set max to n2
Testing and Debugging

Q. What is Testing?
Ans. Testing refers to the process of identification of errors or bugs in a
program. After testing, debugging is done to remove errors.

Q. What is Debugging?
Ans. Debugging is the process of Identify Errors, locating the place of
error, cause of error and fixing bugs in a software. Bugs could be either an
error or an exception.

Q. What is an error in a program? What are the different types of


errors?
Ans. An Error is a bug in the code that causes irregular output or stops a
program from executing. It prevents the program from compiling and
running correctly. Errors in a program can be fixed by making corrections
in the code. Different types of errors are-
1) Compile time Errors- Syntax Errors, Semantic Errors
2) Runtime Errors
3) Logical Errors
Testing and Debugging

Q. Explain different types of errors in a program


Ans. 1. Compile Time Error- Occurs during compile time. They are
identified by the compiler or interpreter. These errors occur as a result of
violation of syntax or grammar rules or when the statements are
meaningless.
Compile-time Errors are of two types-
1. Syntax Error: Syntax error occurs whenever the syntax or
grammatical rules of the programming language are violated.
E.g. print x # instead of print (x)
x=> y # instead of x>=y
if a>b #colon : is not given after if condition
print(“is greater”) # statement after if condition is not properly indented

2. Semantic Error: it occurs when statements are not meaningful.


Semantics refers to the set of rules which give the meaning of a
statement.
E.g. x*y =z #instead of z=x*y
Testing and Debugging

2. Run-time Errors: occurs during the run-time (when the program is


executing). These errors are harder to detect since they cannot be
identified by a compiler or interpreter.
Some run time errors stop the execution of the program which is known as
program “crashed” or “abnormally terminated”.
E.g. An infinite loop or wrong input value (giving string instead of int as
required)
All exceptions are run-time errors like – file not found Error, division by
zero Error

3. Logical Errors: Logical Errors occurs due to wrong logic of a program.


No error is encountered during compile-time or run-time, but still program
does not provide the desired result. These type of errors occur due to the
programmer’s mistake.
E.g. Instead of using the * operator for multiplication of two numbers, the
programmer mistakenly uses the + operator in a statement.
Testing and Debugging

Q. What is an exception in a program? How are they fixed?


Ans. An Exception is an unexpected situation that occurring during
execution or run-time, on which programmer has no control. Fixing
exception is tougher than fixing errors.
Exceptions are fixed by writing the code in try and except blocks.

Q. Give some examples of built-in exceptions


Ans. 1.EOF Error –when a read function reaches the end of file
2.IOError – when a file is not found for reading or disk full for writing
3.NameError – when an identifier name is not found
4.IndexError - when an index for a sequence is out of range
5.TypeError – when an operation or function is applied to an object of
inappropriate type
6.ValueError – function receives an argument of inappropriate value
7.ZeroDivisionError – When division by zero is performed
8.KeyError – When a dictionary key is not found in the set of existing keys
Decomposition

Q. What is Decomposition? What is the need for Decomposition? Give


examples of Decomposition.
Ans. Decomposition also known as factoring, is breaking a complex problem
or system into parts that are easier to conceive, understand, program, and
maintain.
Need for decomposing a problem
It involves breaking down a complex problem or system into smaller parts
that are more manageable and easier to understand. The smaller parts can
then be examined and solved, or designed individually, as they are simpler
to work with.
Example of Decomposition
1.Banking Transaction System
Decomposition

2. Library Management System

3. Payroll System

You might also like