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

C_Ch1_ProblemSolving

by KEC

Uploaded by

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

C_Ch1_ProblemSolving

by KEC

Uploaded by

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

Chapter 1: Problem Solving using Computer

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.

1. Read two numbers


2. Add both numbers to get the result
3. Print the result

9
Algorithm: Example
Problem: Write an algorithm to find the sum of two numbers.

1. Read first number A


2. Read second number B
3. Add A and B to get the result
4. Print the result

10
Algorithm: Example
Problem: Write an algorithm to find the sum of n numbers.

1. Read the value of n


2. Read n numbers
3. Add these n numbers
4. Print the sum

11
Algorithm: Example
Problem: Write an algorithm to find the sum of n numbers.

1. Read the value of n


2. Assign 0 to sum
3. Repeat n times
○ Read number
○ Add number to sum
4. End repeat
5. Print the sum

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

1. Read two numbers, num1 and num2.


2. Compare num1 with num2:
a. If num1 is greater than num2, print num1 is the larger number.
b. If num2 is greater than num1, print num2 is the larger number.

14
Algorithm Example
Write an algorithm to find the bigger number out of 3 distinct numbers

1. Read three distinct numbers, num1, num2, and num3.


2. Compare num1 with num2:
● If num1 is greater than num2,
i. Compare the num1 with num3
ii. If num1 is greater than num3, print the largest number is num1.
iii. Else print the largest number is num3.
● Otherwise
i. Compare the num2 with num3
ii. If num2 is greater than num3, print the largest number is num2.
iii. Else print the largest number is num3.
15
Algorithm Example
Write an algorithm to find the bigger number out of 4 distinct numbers

1. Read num1, num2, num3, num4


2. Assign largest = num1
3. If largest < num2, then assign largest = num2
4. If largest < num3, then assign largest = num3
5. If largest < num4, then assign largest = num4
6. Print largest

16
Algorithm Example

Write an algorithm to find the bigger number out of 4 distinct numbers

1. Read num1, num2, num3, and num4.


2. Compare num1 and num2:
● Assign largest1 = num1 if num1 > num2, otherwise assign largest1 = num2.
3. Compare num3 and num4:
● Assign largest2 = num3 if num3 > num4, otherwise assign largest2 = num4.
4. Compare largest1 and largest2:
● Assign largest = largest1 if largest1 > largest2, otherwise assign largest = largest2.
5. Print largest.

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.

Draw a flowchart for finding the sum of n numbers.


Flowchart
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.

The process of creating a program is called programming.

Programmers typically create programs by producing source code, which is a list of


commands typed into one or more text files.

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

● Designed to allow the programmer to write programs without having to be as


concerned about what kind of computer the program will be run on.
● Programs written in high level languages must be translated into a format the
computer can understand before they can be run.
● There are two primary ways this is done:
○ compiling
○ interpreting.

35
High Level Languages
Compiling

● A compiler reads source code and produces a stand-alone executable program


that can then be run.
● Once the code has been turned into an executable, the compiler is not needed to
run the program.

36
High Level Languages
Compiling

● Some of C Compilers are:


○ GCC
○ Clang
○ C++ Builder

37
High Level Languages
Interpreting

● An interpreter directly executes the instructions in the source code without


requiring them to be compiled into an executable first.
● The interpreter is needed every time the program is run.

38
Compilation and Execution

Compilation and Executable


Preprocessing Linking
Assembly program
Source
code
files Modifies the original Translates the source Links the object code
source code files code into object files with the libraries and
according to the containing machine produces a single
preprocessor language code. executable file.
directives (such as
#include, #define etc.)

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

Refer to the following doc file

https://docs.google.com/document/d/1c-4OB_MLqxfmuuVSTq_WWu4d-lo_VHlpY0m
wdvAnk0M/edit

46

You might also like