C_Ch1_ProblemSolving
C_Ch1_ProblemSolving
1
Steps involved in problem solving using computer
● Problem Analysis
● Program Design - Algorithm, Flowchart and Pseudocode
● Coding
● Compilation and Execution
● Debugging and testing
● Program Documentation
2
Problem Analysis
● Specify the objectives
● Specify the output
● Specify the input requirements
● Specify processing requirements
● Problem analysis documentation
3
Program Design
While designing the program, we design the following:
● Algorithm
● Flowchart
● Pseudocode
4
Algorithm
● A step-by-step representation of the solution of a program, represented in English
like language.
● Can be quite abstract or quite detailed.
5
Characteristics of an algorithm
● Clear and unambiguous
● Well-defined Inputs
● Well-defined Outputs
● Finiteness
● Language Independent
6
How to write an algorithm
● Writing algorithm does not have any well defined standards
● Algorithms are never created to support a specific programming language
● As we all know, basic code features such as loops (do, for, while all programming
language share), flow control(if-else), and so on
● We usually create an algorithm step by step: however, this isn’t always the case
● After the problem domain has been well-defined, algorithm writing is a procedure
that is carried out
7
Algorithm: Example
Problem: Write an algorithm to find the sum of two numbers.
8
Algorithm: Example
Problem: Write an algorithm to find the sum of two numbers.
9
Algorithm: Example
Problem: Write an algorithm to find the sum of two numbers.
10
Algorithm: Example
Problem: Write an algorithm to find the sum of n numbers.
11
Algorithm: Example
Problem: Write an algorithm to find the sum of n numbers.
12
Algorithm: Exercise
1. Write an algorithm to find the average of n numbers
2. Write an algorithm to find the bigger number out of two distinct numbers
3. Write an algorithm to find the bigger number out of three distinct numbers
13
Algorithm: Exercise
Write an algorithm to find the bigger number out of two distinct numbers
14
Algorithm Example
Write an algorithm to find the bigger number out of 3 distinct numbers
16
Algorithm Example
17
Pseudo-code
● Every step is represented in a formal way which is very close to the actual
programming language representation.
● Exact syntax of the programming language will not be followed.
18
Pseudocode: Example
Problem: Write a pseudocode to find the sum of two numbers.
1. START
2. Read number1, number2
3. Sum ← number1 + number2
4. Print Sum
5. STOP
19
Pseudocode: Example
Problem: Write a program to find the bigger number out of two distinct numbers.
1. START
2. Read number1, number2
3. If number1 > number2 then
a. Print number1
4. Else
a. Print number2
5. End if
6. STOP
20
Pseudocode: Exercise
Problem:
Write a pseudocode to find the bigger number out of three distinct numbers.
Write a pseudocode to find the sum of n numbers
21
Pseudocode: Example
Problem: Write a program to find the sum of n numbers
1. START
2. Read n
3. Sum ← 0
4. For i ← 1 to n do
a. Read numberi
b. Sum ← Sum + numberi
5. End for
6. Print Sum
7. STOP
22
Problem: Write a program to find the bigger out of three numbers
1. START
2. Read num1, num2, num3
3. If num1 > num2 then
a. If num1 > num3 then
i. Print num1
b. Else
i. Print num3
c. End if
4. Else
a. If num2 > num3 then
i. Print num2
b. Else
i. Print num3
c. End if
5. End If 23
Flowchart
● Uses graphical symbols to represent the steps of the solution.
24
Basic symbols used in Flowcharts
25
Flowchart: Example
Problem: Find the sum of two
numbers
26
Flowchart: Exercise
Draw a flowchart for finding bigger out of 3 numbers.
28
Flowchart
Flowchart for finding sum of n numbers
29
Coding
● In this stage, process of writing actual program takes place.
● A coded program is most popularly referred to as a source code.
● The coding process can be done in any programming language (high level and low
level).
30
Programming Languages
Computers only natively understand a limited set of commands, and must be told exactly
what to do.
A computer program is a set of instructions that the computer can perform in order to
perform some task.
31
Programming Languages
Machine Language
● The limited set of instructions that a CPU can understand directly is called machine
code (or machine language or an instruction set).
● Each instruction is composed of a sequence of 1’s and 0’s. Can be of fixed or
variable length.
● Portability issue: Different CPUs have different instruction sets ⇒ Programs
written for one type of system won’t work for a different type of system
32
Programming Languages
Assembly Language
● Back when computers were first invented, programmers had to write programs
directly in machine language.
● Because machine language is so hard for humans to read and understand, assembly
language was invented.
● However, the CPU cannot understand assembly language directly.
● Instead, the assembly program must be translated into machine language before it
can be executed by the computer. This is done by using a program called an
assembler.
● Downsides: Readability and Portability
33
Programming language
Assembly language
34
Programming Languages
High-level languages
35
High Level Languages
Compiling
36
High Level Languages
Compiling
37
High Level Languages
Interpreting
38
Compilation and Execution
39
Compilation and Execution
40
Debugging and Testing
● Debugging is the process of finding errors and removing them from a computer
program, otherwise they will lead to failure of the program.
● Even after taking full care during program design and coding, some errors may
remain in the program and these errors appear during compilation or linking or
execution.
● Debugging is generally done by program developer.
41
Debugging and Testing
● Testing is performed to verify that whether the completed software package
functions or works according to the expectations defined by the requirements.
● Testing is generally performed by testing team which repetitively executes program
with intent to find error.
● After testing, list of errors and related information is sent to program developer or
development team.
42
Program Documentation
● The program documentation is the process of collecting information about the
program.
● The documentation process starts from the problem analysis phase to debugging and
testing. Documentation consists two types of documentation.
○ Programmers Documentation
○ Users Documentation
43
Programmers Documentation
● Programmer’s documentation contains all the technical details.
● A programmer’s documentation contains the necessary information that a
programmer requires to update and maintain the program. These information
includes:
1. Program analysis document, with a concise statement of program’s objectives,
outputs and processing procedures.
2. Program design documents with appropriate flowcharts and diagrams.
3. Program verification documents for outlining, checking, testing and correction
procedures along with the list of sample data and results.
4. Log used to document future program revision and maintenance activity.
44
Users Documentation
● User documentation is required for the end user who installs and uses the program.
● It consists instructions for installation of the program and user manual.
45
Lab 1
Installation of C Compiler and VScode
https://docs.google.com/document/d/1c-4OB_MLqxfmuuVSTq_WWu4d-lo_VHlpY0m
wdvAnk0M/edit
46