0% found this document useful (0 votes)
22 views21 pages

Unit 1problem Solving Aspects

The document discusses problem-solving aspects, particularly through the lens of George Polya's four-step approach, which includes understanding the problem, devising a plan, carrying out the plan, and looking back. It elaborates on the use of algorithms, flowcharts, and pseudocode as tools for problem-solving in computer programming, highlighting their definitions, advantages, and disadvantages. Additionally, it covers programming languages and paradigms, explaining how they facilitate communication with computers and influence software design.

Uploaded by

omkawade09
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)
22 views21 pages

Unit 1problem Solving Aspects

The document discusses problem-solving aspects, particularly through the lens of George Polya's four-step approach, which includes understanding the problem, devising a plan, carrying out the plan, and looking back. It elaborates on the use of algorithms, flowcharts, and pseudocode as tools for problem-solving in computer programming, highlighting their definitions, advantages, and disadvantages. Additionally, it covers programming languages and paradigms, explaining how they facilitate communication with computers and influence software design.

Uploaded by

omkawade09
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/ 21

UNIT 1 : Problem Solving Aspects

Introduction
The famous Hungarian mathematician George Polya (1887 – 1985), published the
book “How to Solve it” in 1945 which describes methods of problem solving. In
his book, Polya has presented a four step approach to problem solving.
(1) Understand the problem
(2) Devise a plan
(3) Carry out the plan
(4) Look back

PROBLEM SOLVING USING COMPUTER


A computer is a machine that receives input, stores and manipulates data, and
provides output in a useful format. However, a computer is not intelligent. It
cannot analyze a problem and come up with a solution. The programmer must
analyze the problem, develop the procedure for solving the problem, and then
make computer carry out the instructions. Polya’s problem solving approach can
be applied to problem solving using computers.
1. Understand the problem: In this phase, requirements are first gathered i.e. we
have to understand what exactly has to be done. Who are the stakeholders? What
is the input, what is the expected output?
2. Develop the Solution: Once the problem has been fully understood, the solution
has to be developed. The design phase determines how the solution will be
developed. For this, various tools are used. Example: Diagrammatic tools like
flowcharts, structure charts, algorithm, pseudocode, Modeling language like UML
etc. This helps in showing program flow and detecting mistakes.
3. Implement the Solution: The solution is implemented in this stage. The
hardware and software is used for implementation. Programming languages are
used as tools. The entire code could be broken into modules and developed
independently, or it could be developed as a single program
. 4. Test the solution: Testing is an important phase of Program development. The
program has to be tested for all possible values of inputs to check if it gives the
desired output. In order to do this, test-cases are generated and used on the
programs. Debugging is done in this stage.
5. Refinement and Maintenance: Once the program is tested and okayed, the final
phase is its maintenance. This stage involves ensuring that the program runs
efficiently, making updates if necessary, adding new features as per requirements
etc.
ALGORITHM
A computer will do exactly as it is told. It has no intelligence of its own. It will not
do anything that it has not been instructed to do. It will not try to interpret your
instructions in different ways. Hence, it is important to give the solution in a clear
and step-wise manner. An algorithm is an important tool that is used to give the
solution to a problem in a step by step manner. The word ‘algorithm’ is derived
from the name of a Persian mathematician Al Khwarizmi (780-850), who gave step
by step procedures for arithmetic problems like dividing, solving linear equations,
etc.
Definition An algorithm is a finite set of instructions in a logical sequence,
which, if followed, accomplish a particular task. These instructions are written in
simple English. Every algorithm must satisfy the following criteria.
1.Finiteness: The algorithm should terminate after a finite number of steps.
2.Definiteness: Each instruction must be clear and unambiguous.
3.Effectiveness: Every instruction must be sufficiently basic, that it can be carried
out by a person using only pencil and paper.
4.Input: The algorithm must accept zero or more quantities, which are externally
supplied.
5.Output: It should produce at least one desired output.

Advantages
1.It is a general-purpose tool, which is language and hardware independent.
2.It makes it easy to understand the program logic.
3.Identification of errors is easier. A program can be easily written from an
algorithm.
4.Since it is written in simple English, it can be understood by everyone.
Disadvantages
1.It is time-consuming process.
2.It is difficult to show branching and repetitive tasks.
3.It is often unclear on how much detail to put in an algorithm.
4.For big tasks, an algorithm can become lengthy and complicated.

1. Calculate area of a circle by taking radius as input.


1. Start
2. Accept radius as input
3. Calculate area = 3.142 X radius X radius
4. Display area
5. Stop

2. Check if a number is odd or even


1. Start
2. Accept number n as input
3. if n is divisible by 2
Display “Number is Even”
Else
Display “Number is Odd”
5. Stop
Flowchart
An algorithm is a written form of the solution to a problem. However, it can get
very lengthy and complicated. A flowchart is a diagrammatic way of specifying the
solution. It is a design tool in which standard graphical symbols are used to
represent the logical flow of data.

Definition:
A flowchart is a diagrammatic or pictorial representation of an algorithm.

Flowchart Symbols
Symbol Name Use
Terminal Indicates start and stop of
the process.
Input/Output Indicates an input or
output task.
Process Contains Arithmetic or
Assignment task.
Decision Indicates a condition and
two or more alternatives.
Flow lines Shows direction of flow.

Module call Used for calling an


external module.
Connector It is used to connect
different flowlines or
used to show continuity
of flowchart on separate
pages.
Advantages of Flowchart
1.Since it is a pictorial representation, it can be understood very easily
2. It is very easy to identify errors.
3. Process and Data flow can be easily traced for all possible cases.
4. It is a general purpose tool and it is language independent.
5. It is very easy to write a program from the flowchart.
Disadvantages of Flowchart
1.Very often, a flowchart becomes very lengthy and runs into pages.
2.Modifying a flowchart is very difficult
3.It is a time-consuming process.

Difference between Algorithm and Flowchart

No. Algorithm Flowchart


1 Algorithm is a text based tool Flowchart is a
diagrammatic tool
2 The sequence of steps are The sequence of
written in plain English steps are denoted
using specific
symbols
3 Process and data flow cannot Process and data
be easily traced flow can be easily
traced
4 Difficult to show branching Easier to show
and iteration branching and
iteration
5 It is imprecise and verbose It is precise
6 Easier to make changes Changes cannot be
made easily in a
flowchart
Pseudocode
The algorithms are written using English statements. Sometimes it becomes
difficult to translate them into a program. Also, there are no specific rules or
structure to write an algorithm. Another tool used to specify the solution is
“Pseudocode”. Pseudocode is a mixture of program-like code and English
statements..It has a structure similar to a program but is more informal.

Definition A pseudocode is an informal, structured description of an algorithm or


program. The following conventions are used while writing pseudocode:
Syntax Example
Algorithm Outline Algorithm Algorithm
<name> CalculateArea
Begin Algorithm
<statements> FindMax(num1, num2)
End Algorithm <name>
(<elements>)
Begin
<statements>
End
Data elements Any logical name can be radius, sum, data[1], A[i]
given to an element
which stores a value. Can
also be a part of group of
elements identified using
an index.
Input from user Input <element name> Input radius
Expression operand1 operator area = 3.142 X radius X
operand2 operator can radius
be : marks ≥ 60 and marks <
arithmetic + - / mod 70
relational <, ≤ , >, ≥, ≠,
=
logical and or not
Assignment variable = value variable count = 0
= expression sum = num1 + num2
Decision Making if <condition> then if marks≥60 and
<statement> marks<70 then
OR Output “First Class”
if <condition> then if num≥0 then Output
< statement1> “Positive”
else else
<statement2> Output “Negative”
Repetition for variable=value1 to for num = 1 to 100 do
(Fixed times) value2 do Output num
<statements> for num = 100 downto 1
OR do
for variable=value2 Output num
downto value1 do
<statements>
Repetition (as long as 1.Top Tested while num>0 do
condition is true) while <condition> do num = num/10
<statements> repeat
2.Bottom Tested Input num
repeat <statements> until sum = sum+num
<condition> until sum<100

Advantages of Pseudocode
1. It is more structured compared to algorithm.
2. It is easier to convert pseudocode to program.
3. It can be understood by non-programmers.
4. Branching and looping can be easily written in pseudocode.
5. Program flow can be easily traced.
6. Errors can be identified

Limitations of Pseudocode
1. It is text based and not diagrammatic. Hence if the logic is complex,
pseudocode becomes difficult to understand.
2. There are no standard rules or guidelines for writing pseudocode.
3. It is more difficult to express the solution in the form of pseudocode
compared to flowchart.
Pseudocode Examples
1 Calculate area of a circle by taking radius as input.

Algorithm CalculateArea
Begin
Input radius
area = 3.142 * radius*radius
Output area
End

2. Check if a number is odd or even


Algorithm CheckPositive
Begin
Input num
if num mod 2 = 0 then
Output “num is even”
else
Output “num is odd”
End

3. Calculate sum and average of N numbers


Algorithm SumAverage
Begin
Input N
sum = 0
for count=1 to N
Input num
sum = sum+num
average = sum/N
Output sum
Output average
End

Programing languages as Tools


Algorithms,flowchart and pseudocode are used to specify the solution
informally.To make the computer solve the problem,the solution has to be given to
the computer in the form of instructions which the computer can understand.For
this ,we have to communicate with the computer .
A programming Language is a tool for communicating with the computer .It allows
us write programs for the computer .Aprogram consistsof a set of instructions to
the computer.
Computer language have evolved over the years from the earliest machin language
to the recent natural languages.The various stages in the evolution of computer
languages is

Computer Language

Low Level High Level

Machine Language Assembly Language


Low Level Languages
These languages were the earliest languages developed.Under this category,we
have Machin and Assembly languages.
Features of Low Level Language
1.These languagea are hardware dependant.
2.Programs written for one machine will not run on another.
3.Programmers are required to have knowledge about the computer hardware.

Machine Language
The earliest known computer is Machine Language(1940's).Since the computer is
made up of digital electronics circuits,
It can only understand binary logic.Hence in order to communicate with the
computer the programs have to be written in the form of 0's and 1's .Data also has
to be given in binary.
Advantages
1.Since the computer hardware can understand a program written in
binary,executionis very fast.
2.There is no neeed to translate the program
Disadvantages
1.Writing programs in binary is very difficult.
2.It is very easy to make errors during writing or data entry.
3.Debugging is very difficult.
4.There is no distinction between the instruction and data.
Symbolic/Assembly Language
These were developed in the 1950's to remove the disadvantages of machine
language .In these languages,small English like words,called mnemonics were
used for instruction(for ex.ADD,SUB,etc)and hexadecimal codes were used for
data.
Advantages
1.Writing program is easier than machine language.
2.Identification of errors is easy.
3.There is distinction between instructiona and data.
4.Programs can be easily understood.

Disadvantages
1.Because a computer does not understand symbolic language,it has to be
translated to machine language.A special software called Assembler is needed to
translate assembly code to machine code.
2.Execution becomes slower.

High Level Languages


High level languages were developed to overcome the limitations of low level
languages.The earliest high level languages were developed in the 1960's.These
languagesare more English like and contain a variety of instructions and data types
so that programming becomes easier.
Ex.'C',Pascal,FORTRAN,COBOL,etc.

Advantages
1.Use English like words for instructions.
2.Support multiple data types like characters,intergers,real-numbers.etc.
3.Hardware independent instruction set.Hence programs are portable.
4.Easierto write and debug programs in high level languages.

Disadvantages
1.Programs have to be converted from high level to machine language using a
special software like Cmpiler or Interpreter.

Programming Paradigms

A programming paradigm refers to a fundamental style or approach to


programming that influences how software is designed, written, and structured. It
provides a set of principles and practices for organizing and processing data, as
well as the way in which code is executed. Different paradigms emphasize
different aspects of programming, such as how state is managed, how control flow
is structured, and how to handle complexity in large systems.

Here are some of the most common programming paradigms:

1.Imperative Programming

Focus: Describes how to do something step by step.

Key Characteristics: Code consists of statements that change the program's state
through variable assignments and control flow (like loops, conditionals).

Languages: C, Java, Python (partially), Go, etc.

Procedural Programming

Focus: Organizes code into procedures or functions that operate on data.

Key Characteristics: Similar to imperative programming but with a


stronger emphasis on breaking down tasks into reusable procedures
(functions or subroutines).

Languages: C, Fortran, Pascal, Python (partially).

Object-Oriented Programming (OOP)

Focus: Organizes code into objects that represent real-world entities.

Key Characteristics:

1.Encapsulation (hiding data inside objects).


2.Inheritance (reusing code through subclassing).

3.Polymorphism (different behavior for different objects).

4.Abstraction (simplifying complex systems).

Languages: Java, Python, C++, Ruby, C#, etc.

2.Declarative Programming

Focus: Describes what to do, not how to do it.

Key Characteristics: The programmer specifies the desired outcome, and


the language implementation figures out how to achieve it.

Languages: SQL, HTML, CSS, Haskell (to some extent), Prolog.

Functional Programming

Focus: Treats computation as the evaluation of mathematical functions and


avoids changing state and mutable data.

Key Characteristics:

o First-class functions (functions can be passed as arguments, returned


as values).
o Immutability (data cannot be changed after it’s created).
o No side effects (functions don’t modify external states).

Languages: Haskell, Lisp, Scala, Elixir, F#, JavaScript (to an extent),


Python (partially).

Logic Programming

Focus: Based on formal logic, programs consist of a set of facts and rules
that describe relationships between entities.

Key Characteristics: Programs are written as a series of logical statements


or predicates. The system uses inference to derive conclusions based on the
provided facts and rules.
Languages: Prolog, Mercury, Datalog.

Mathematical:

In this paradigm,the desired result is declared as the solution of an optimization


problem

Language:Mathematica,Coq etc

Converting pseudocode to Program

The solution to a problem is expressed using tools like algorithm,flowchart or


pseudocode.Flowchart and pseudocode are preferred to writing lenghthy and
verbose algorithms.The structure os a pseudocode is similar to that of a
program.Even though it is informal,it uses data elements,operators and
expressions,and constructs like decision making and looping.This makes it very
easy to convert a pseudocode to a program in any programming language.

Following ex show how a pseudocode for calculating area of circle is converted to


a 'C' program.

Algorithm CalculateArea

Begin

Input radius

Area=3.142*radius*radius

Output area

End

void main()

float radius,area;
printf("Enter the radius");

scanf("%d",&radius);

area=3.142*radius*radius;

printf("Area=%f",area);

The Compilation Process

Compiler and Interpreter

Compiler:

A compiler is a program that translates the entire source code of a programming


language into machine code or an intermediate code all at once, before execution
begins. The translation happens in a single, separate phase (compilation), and then
the machine code or intermediate code is executed later.

Characteristics of a compiler:

Compilation Phase: The compiler reads the entire source code and
translates it into an executable file (e.g., .exe, .out) or bytecode (e.g., .class
files for Java).

Execution: The program is executed separately from the compilation


process. Once compiled, the program can be run multiple times without
needing to recompile.

Speed: Compilation happens once, so the execution of the compiled code is


generally faster.

Error Checking: A compiler checks for errors throughout the entire source
code before generating the output. If there is an error in the code, the
compiler will not produce any output until all errors are fixed.

Examples of compiled languages: C, C++, Rust, Go, Swift.


Interpreter:

An interpreter translates and executes the source code line-by-line or statement-


by-statement, without producing a separate executable file. It reads the source
code, translates it into machine code (or bytecode), and then executes it
immediately.

Characteristics of an interpreter:

Interpretation Phase: The interpreter does not create an intermediary file. Instead,
it directly interprets and executes the source code line by line.

Execution: Each time the program is run, the interpreter reads and executes the
source code from the beginning.

Speed: Interpretation is generally slower than compilation because the code is


translated on the fly.

Error Checking: The interpreter executes the program one step at a time, so errors
are typically reported one by one as the interpreter encounters them.

Examples of interpreted languages: Python, JavaScript, Ruby, PHP, MATLAB.

Key Differences
Linker and Loader
The linker and loader are two essential components in the process of converting a
program's source code into a running application. While they are both involved in
the execution of a program, their roles are distinct, and they come into play at
different stages of the program's lifecycle.
Linker:The role of a linker is to link the object code of the program with pre-
compiled code such as predefined functions and library files.It creates a single
executable file which is then given to the loader.
Loader:The loader is part of the operating system responsible for loading
executable files into memory when the program is executed. It prepares the
program for execution by allocating memory and setting up the runtime
environment.

Syntax and Semantics Errors

Feature Compiler Interpreter


Translates the entire Translates and executes
Translation
program at once line by line
Produces an executable Executes the code
Execution
file directly
Faster execution (after Slower execution (due to
Speed
compilation) real-time translation)
Errors are reported after Errors are reported
Error Detection
full compilation during execution
Requires memory for
Uses memory to store the
Memory Usage both source and compiled
current line of code
code
Errors are an integral part of programing .Two types of errors commonly occurs
during programming:

Syntax errors: A syntax error occurs when the programmer violates the rules of
the programming language's grammar. These errors happen during the parsing
phase when the compiler or interpreter attempts to process the source code. The
language's syntax dictates how statements should be structured (e.g., proper use of
keywords, punctuation, and code structure).
Common Causes of Syntax Errors:

Missing punctuation (e.g., semicolon, parentheses, braces).

Incorrect use of keywords (e.g., using a reserved word incorrectly).

Mismatched parentheses or curly braces.

Incorrect indentation (in languages like Python, indentation is syntactically


significant).

Typo in variable or function names.

Semantic error:A semantic error occurs when the program runs without syntax
errors but produces incorrect or unintended behavior. These errors are usually due
to logical mistakes in the program, where the code does not do what the
programmer intends. The program is syntactically correct, but the meaning of the
program (its logic) is incorrect.

Common Causes of Semantic Errors:

Incorrect logic or algorithms (e.g., wrong mathematical operations or


conditions).

Using the wrong data types (e.g., adding a string and an integer in a
language that doesn’t allow this).

Assigning incorrect values to variables or misusing variables.

Incorrect function calls (calling a function with wrong arguments).

Misunderstanding how a particular function or library behaves.


Testing:

Testing is an integral part of software development aimed at ensuring the


correctness, reliability, and functionality of a program or system. During
testing, various techniques are employed to identify bugs, verify that the
software behaves as expected, and ensure that it meets the specified
requirements.

In software testing, a test case is a specific set of conditions or inputs used


to determine whether a software application or system is working as
expected.Software testers carry out testing either manually or with the help
of special software.Any bugs that are detected are reported to the devlopers
who make the necessary code changes to correct the problem.

Good Programming Practices

1.Documentation: Documentation is essential for explaining the purpose,


behavior, and usage of your code, making it easier for others (or your future self)
to understand and maintain it.
Best Practices:

1.Write clear comments for complex logic, explaining why something is


done, not just what is done.

2.Use docstrings (or similar) to describe the purpose of functions, classes,


and modules, including parameters, return values, and side effects.

3.Keep the documentation up to date as the code evolves.

2.Indentation:Indentation is used to structure the code and visually represent the


logical hierarchy, improving readability and maintainability.

Best Practices:

1.Use consistent indentation throughout the code. In most languages, 2 or 4


spaces per indentation level is common.
2.Avoid mixing tabs and spaces. Stick to one (usually spaces).

3.In languages like Python, proper indentation is crucial because it directly


affects how the code runs.

3.Naming Convention: Consistent and descriptive naming helps make the code
more readable, and allows developers to quickly understand what each variable,
function, or class represents.

Best Practices:

1.Use meaningful, descriptive names (e.g., calculateTotal, isValid,


UserProfile).

2.Avoid single-letter names unless they are universally accepted (e.g., i for
loop indices)

4.Keep the code simple: Simple code is easier to understand, test, and
maintain. Overly complex solutions can lead to confusion and introduce
unnecessary bugs.The code may be modified by another programmer in the
future.Also,complicated code written by one programmer may not understood by
another.Hence ,it is important to keep the code simple.

5.Portability: Portability ensures that your code can run across different
environments (e.g., operating systems, hardware architectures) without
modification. It helps make the software more adaptable and scalable.

You might also like