Unit 1problem Solving Aspects
Unit 1problem 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
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.
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.
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
Computer Language
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.
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
1.Imperative Programming
Key Characteristics: Code consists of statements that change the program's state
through variable assignments and control flow (like loops, conditionals).
Procedural Programming
Key Characteristics:
2.Declarative Programming
Functional Programming
Key Characteristics:
Logic Programming
Focus: Based on formal logic, programs consist of a set of facts and rules
that describe relationships between entities.
Mathematical:
Language:Mathematica,Coq etc
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);
Compiler:
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).
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.
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.
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.
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 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:
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.
Using the wrong data types (e.g., adding a string and an integer in a
language that doesn’t allow this).
Best Practices:
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:
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.