0% found this document useful (0 votes)
9 views34 pages

PPS 123

The document outlines a programming course focused on problem-solving, teaching students to learn computer languages, coding, and the problem-solving process. It details the steps involved in problem-solving, the components of a computer system, and the roles of algorithms and programs. Additionally, it covers programming concepts such as flowcharts, pseudocode, data types, and rules for evaluating expressions.

Uploaded by

jalaj
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)
9 views34 pages

PPS 123

The document outlines a programming course focused on problem-solving, teaching students to learn computer languages, coding, and the problem-solving process. It details the steps involved in problem-solving, the components of a computer system, and the roles of algorithms and programs. Additionally, it covers programming concepts such as flowcharts, pseudocode, data types, and rules for evaluating expressions.

Uploaded by

jalaj
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/ 34

PROGRAMMING

FOR PROBLEM
SOLVING
Lecture – 1, 2 & 3

Dr. S.Kanungo
Dept. of CSE, BIT Mesra
Course Objectives
• This course enables the students:
➢ To learn computer language
➢ To learn coding for problems
➢ To learn the problem-solving process
through computer
➢ To know the limitations of system during
program execution
Problem Solving
• It is an intricate process requiring much
thought, careful planning, logical precision,
persistence, and attention to detail.
• It can be a challenging, exciting, and satisfying
experience with considerable room for personal
creativity and expression.
• It is about understanding the problem
• Focus on: What must be done rather than how to
do it.
• Problem definition phase: Preliminary
Investigation
• Note: “Sooner you start coding your program the
longer it is going to take”
• The success of a computer in solving a
problem depends on how correctly and
precisely we define the problem, design
a solution (algorithm) and implement
the solution (program) using a
programming language.
• It is the process of identifying a
problem, developing an algorithm for
the identified problem and finally
implementing the algorithm to develop
a computer program.
Steps for Problem Solving
• Analysing the problem: To read and analyse
the problem statement carefully to list the
principal components of the problem and decide
the core functionalities that our solution should
have.
• Developing an algorithm: The solution is
represented in natural language and is called an
algorithm.
• Coding: To convert the algorithm into the format
which can be understood by the computer to
generate the desired solution.
• Testing and debugging: Error handling
Elements of a Computer System
• Hardware
➢ Equipment used to perform the
necessary computations and includes
the central processing unit (CPU),
monitor, keyboard, mouse, printer and
speakers.
• Software
➢ Consists of the programs that enable us
to solve problems with a computer by
providing it with lists of instructions to
perform.
Components of a Computer
• Memory
➢Ordered sequence of storage locations
called memory cells.
➢Each memory cell has a unique address
that indicates its relative position in
memory.
➢Data stored in a memory cell are called the
contents of the cell.
➢The ability to store programs as well as data is
called the stored program concepts: A program’s
instructions must be stored in main memory
before they can be executed.
➢A memory cell is a grouping of smaller units
• Central Processing Unit (CPU)
➢Two roles: Coordinating all computer
operations and performing arithmetic and
logical operations on data.
➢To process a program stored in main
memory:
➢ It retrieves each instruction in sequence,
interprets the instruction to determine what
should be done, and then retrieves any data
needed to carry out that instruction.
➢Next, it performs the actual manipulation,
or processing of the data it retrieved.
➢It stores the results in main memory
Operating System
• The collection of computer programs that control
the interaction of the user and the computer
hardware.
• Some responsibilities
➢Communicating with the computer user: receiving commands
and carrying them out or rejecting them with an error message.
➢ Managing allocation of memory, of processor time, and of
other resources for various tasks.
➢ Collecting input from the keyboard, mouse, and other input
devices, and providing this data to the currently running
program.
➢Conveying program output to the screen, printer, or other
output device.
➢ Accessing data from secondary storage.
➢Writing data to secondary storage.
• Entering, Translating, and Running a HLL Program
• Flow of information during the execution of a
water bill program
Programs and Algorithms
• Program: A set of explicit and unambiguous
instructions expressed in a programming
language.
• Algorithm
➢Solution to a problem that is independent
of any programming language.
➢Consists of a set of explicit and
unambiguous finite steps which, when
carried out for a given set of initial
conditions, produce the corresponding
output and terminate in a finite time.
➢Example: Find Greatest Common Divisor
(GCD) of two numbers 45 and 54.
Note: GCD is the largest number that divides both
the given numbers.
Step 1: Find the numbers (divisors) which can
divide the given numbers
Divisors of 45 are: 1, 3, 5, 9, 15, and 45
Divisors of 54 are: 1, 2, 3, 6, 9, 18, 27, and 54
Step 2: Then find the largest common number from
these two lists.
Therefore, GCD of 45 and 54 is 9
➢ We need to follow a sequence of steps to
accomplish the task.
➢Algorithm has a definite beginning and a definite
end and consists of a finite number of steps.
➢The programmer first prepares a roadmap of the
program to be written, before writing the code.
➢Characteristics of a good algorithm
▪ Precision: the steps are precisely stated or
defined.
▪ Uniqueness: results of each step are uniquely
defined and only depend on the input and the
result of the preceding steps.
▪ Finiteness: the algorithm always stops after a
finite number of steps.
▪ Input: the algorithm receives some input.
▪ Output: the algorithm produces some output.
➢While writing an algorithm, it is required to clearly
identify the following:
▪ The input to be taken from the user
▪ Processing or computation to be performed to
get the desired result
▪ The output desired by the user
➢ Representation of Algorithms
▪ Flowchart and Pseudocode
• it showcases the logic of the problem solution,
excluding any implementational details
• it clearly reveals the flow of control during
execution of the program
• Flowchart
➢A visual representation of an algorithm
➢Shapes or symbols to draw flow charts:
Start/End: Also called “Terminator” symbol.
It indicates where the flow starts and ends.
Process: Also called “Action Symbol,” it
represents a process, action, or a single step.
Decision: A decision or branching point, usually a
yes/no or true/ false question is asked, and based
on the answer, the path gets split into two
branches.
Input/Output: Also called data symbol, this
parallelogram shape is used to input or output data
Arrow: Connector to show order of flow between
shapes.
• Write an algorithm to find the square of a number
➢Input: Number whose square is required
➢Process: Multiply the number by itself to get its
square
➢ Output: Square of the number

Algorithm to find square of a number.


Step 1: Input a number and store it to num
Step 2: Compute num * num and store it in square
Step 3: Print square
• Flowchart to calculate square of a number
• Pseudocode
➢ It is considered as a non-formal language that
helps programmers to write algorithm.
➢It is a detailed description of instructions that a
computer must follow in a particular order.
➢Frequently used keywords while writing
pseudocode:
• INPUT • COMPUTE • PRINT • INCREMENT • DECREMENT
• IF/ELSE • WHILE • TRUE/FALSE
➢ Pseudocode for the sum of two numbers:
INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result
• C Language Elements in Miles-to-Kilometers Conversion Program
• Libraries
➢Collections of useful functions and symbols
➢Each library has a standard header file whose
name ends with the symbol .h
• Preprocessor directives
➢Commands that give instructions to the C
preprocessor, whose job is to modify the text of a
C program before it is compiled.
➢#include directive gives a program access to a
library
➢#include <stdio.h> directive notifies the preprocessor
that scanf and printf are found in the standard header
file <stdio.h>.
➢#define KMS_PER_MILE 1.609 associates the constant
macro KMS_PER_MILE with the meaning 1.609.
• Comment
➢Starting with /* and ending with */
➢Ignored by the C preprocessor and compiler
• #include directives tell the preprocessor where to
find the meanings of standard identifiers used in
the program
• The header file stdio.h contains information about
standard input and output functions such as scanf
and printf.
• Every C program has a main function. The
remaining lines of the program form the body of
the function which is enclosed in braces {, }
• A function body has two parts: declarations and
executable statements.
• Reserved Words

• User defined identifier


➢Own identifiers to name memory cells that will hold
data and program results and to name operations that
we define
• An identifier must consist only of letters, digits, and
underscores.
• An identifier cannot begin with a digit.
• A C reserved word cannot be used as an identifier.
• An identifier defined in a C standard library should not
be redefined.
• Reserved Words and Identifiers
• Example: Area of Circle (Version 1)
• Example: Area of Circle (Version 2)
Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void


• Placeholders in Format Strings

• printf()
• scanf()

• return()

• Arithmetic operators
• Mathematical Formulas as C Expressions

• Rules for Evaluating Expressions


➢Parentheses rule: All expressions in parentheses must be
evaluated separately. Nested parenthesized expressions
must be evaluated from the inside out, with the innermost
expression evaluated first.
➢ Operator precedence rule: Operators in the same
expression are evaluated in the following order:
unary +, - first
*, /, % next
binary +, - last
➢ Associativity rule: Unary operators in the same
subexpression and at the same precedence level (such as
+ and − ) are evaluated right to left ( right associativity ).
Binary operators in the same subexpression and at the
same precedence level (such as + and − ) are evaluated
left to right ( left associativity ).
➢ Example
z - (a + b / 2) + w * -y
• Evaluation Tree and Evaluation for z − (a + b / 2) + w * −y

You might also like