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

Chapter 02 - Problem Solving Through Programs

Uploaded by

soumyadeepctry04
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)
24 views

Chapter 02 - Problem Solving Through Programs

Uploaded by

soumyadeepctry04
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/ 16

Problem Solving through

Programs

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 1


Acknowledgement
The contents (figures, concepts, graphics, texts etc.) of the slides are gathered
and utilized from the books mentioned and the corresponding PPTs available
online:

Books:
1. Let Us C, Yashawant Kanetkar, BPB Publications.
2. The C Programming Language, B. W. Kernighan, D. Ritchie, Pearson
Education India.

Web References:
1. Problem Solving through Programming in C, Anupam Basu, NPTEL Video
Lectures. Link: https://nptel.ac.in/courses/106/105/106105171/
2. Compile and Execute C Online (Link:
https://www.onlinegdb.com/)

Disclaimer: The study materials/presentations are solely meant for academic


purposes and they can be reused, reproduced, modified, and distributed by
others for academic purposes only with proper acknowledgements.
CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 2
Why Learn Programming ?
• As there exist a procedure of doing a thing, there also exist a proper
systematic approach to program a program. But first, what is
program?
• A computer program is a collection of instructions that can
be executed by a computer to perform a specific task.
• To perform different tasks of computation we need to learn
programming.
• A computer program is usually written by a computer
programmer in a programming language.
• There are various programming languages like , Java, Basic, C, C++
etc.
• We will learn the programming through the language C.

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 3


What is problem solving?
• Problem solving is the process of logically breaking down a problem
into three major phases
1. Stating clearly what the problem is
2. Defining how the problem can be solved in a logical sequence of
steps
3. Verifying that the solution really does solve the stated problem
• When humans solve problems by themselves, they use a vast array of
knowledge and understanding to perform these problem-solving
steps.
• When humans use a computer to solve a problem, the solution must
be coded in a programming language, which, is much simpler than
the natural language human use with one another.
• During problem solving using a computer, the human does these
following steps:

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 4


What is problem solving?
• The human understands the logics of the problem in a detailed
manner.
• Then outlines a solution in the form of an algorithm, which can
be thought of as a high-level program.
• An algorithm is a step-by step procedure to solve any problem.
• The human then encodes the algorithm in a computer
programming language.
• The computer compiles (i.e., translates) the program into an
electronic form that can be executed directly by the computer
hardware; this form is called the machine code.
• The computer runs the machine code to produce an answer.
• The human validates that the answer is correct.

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 5


Problem Solving
Learning to program requires two skills:
• Knowledge about how to make Algorithms – You must be able to
develop a plan for solving a particular problem. Such a plan is often
referred to as an algorithm. Algorithm is nothing but just a
plan/sequence when executed in the manner they are written, will
achieve the desired results.
• Knowledge of Language – A language basically consist of:
o Syntax – constitute grammatical portion.
o Semantic – provides meaning to language
o Keywords – reserved words and constants etc. that constitutes a
language.
• Everyone who starts programming finds it very hard in beginning. But
in later stages the moment you grasps the concepts of syntax and
semantics the burden becomes enjoyment. This is what known as
power of programming!

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 6


Problem Solving
• Essentially, a computer program takes some input from an input
stream, translates it into some application dependent output,
and sends this output to an output stream
• The term stream refers to any input source or output
destination.
• Input can be from the keyboard, from secondary storage or from
some other external device. Output can be to the screen, to
secondary storage or to some other external device.
• If there was no output there would be no point in having the
program --- it must do something to be useful!
• To carry out the required translation the computer program
comprises a series of instructions. An instruction tells the
computer that it should perform a particular task. Examples
included arithmetic instructions such as addition and subtraction, and
logic instructions, which evaluate to true or false.
CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 7
Problem Solving
• When a program is running in a computer, it is said to be executing. At
this time a copy is contained in primary memory where it occupies a
number of consecutive memory cells.
• Each instruction contained in the program is stored in a memory cell
(or a group of such cells) and comprises a number of bits.
• Different instructions are represented by different combinations of
bits in the cells. As a program executes the CU (Control Unit) reads
the instructions contained in primary memory and carries them out.
This will involve operations such as moving the contents of primary
memory cells to the CPU (Central Processing Unit) and, if appropriate,
invoking the ALU (Arithmetic Logic Unit) to, for example, add two
numbers together.
• The command to execute a program includes the name of the file in
which the program is stored. The command causes the operating
system to search for the required file in secondary memory and (if
found) copy it into primary memory where it is then executed under
the control of the CU. On completion control is returned to the
operating system.
CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 8
Basic Block Diagram of Computer

CPU

ALU

Input Output
Device Device
Control Unit

Memory Unit

CSC01: Introduction to Computing DEPT OF CSE, ODD SEM 2020


9
Let’s do it
Few simple guidelines to be a good programmer:
• Analyze the problem carefully, attempting to understand its
specific aspects and the requirements of an acceptable
solution.
• Attempt to break the problem in simpler, relatively
independent parts and then focus on the individual parts
separately.
• When breaking a problem into simpler parts, perform the
decomposition gradually in several stages.
• At every stage in it’s development, review and reconsider
your proposed solution to ensure that it is complete and
correct.

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 10


Let’s do it
When analyzing the problem you should identify the problem in
terms of:
• Inputs: the data you have to work upon
• Outputs: the information you are expecting
• Constraints: and limitations/requirements/checks you have to
incorporate while processing the data.
• After these aspects are identified the actual implementation of
the process is done to achieve the required outcome.
• For this you have to write step-by-step instructions/statements.
• This combination of step-by-step instructions or statements is
known as Algorithm.
• By definition: An algorithm is a complete, unambiguous
procedure for solving a specific problem, expressed as a finite
number of rules, guaranteed to terminate in a finite number of
applications of these rules.

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 11


Representation of Algorithms: Pseudo code
• Algorithms can be presented by natural languages, pseudocode,
and flowcharts, etc.
• Algorithm representation is the distinction between syntax and
semantics.
• The term syntax refers to the representation, whereas semantics
refers to the concept representation.
• A major goal in algorithm representation is to ensure that the
syntax accurately reflects the intended semantics.
• To design and represent algorithms, most computer programmers
use a notation called pseudocode.
• This is a special set of english language constructs, which can be
modeled for any computer-oriented language viz: C, C++, and Java
etc.

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 12


Representation of Algorithms: Flowchart
• A flowchart is the graphical or pictorial representation of an
algorithm with the help of different symbols, shapes, and arrows
to demonstrate a process or a program.
• The main purpose of using a flowchart is to analyze different
methods.
• Several standard symbols are applied in a flowchart:
Terminal box - Start/End
Input/Output
Process/Instruction
Decision
Connector/ Arrow

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 13


Example: Add two numbers Flowchart:

Start

Algorithm:
Step 1: Read 2 numbers a and b, Read
Step 2: Calculate sum with formula sum=a+b, a, b
Step 3: Print sum,

Sum=a+b

Print
sum

End
CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 14
Example: Convert Temperature from Fahrenheit
(℉) to Celsius (℃)
Flowchart:

Algorithm:
Step 1: Read temperature in Fahrenheit,
Step 2: Calculate temperature with formula
C=5/9*(F-32),
Step 3: Print C,

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 15


Example: Determine Whether A Student Passed
Algorithm:
the Exam or Not?
Step 1: Input grades of 4 courses M1, M2, M3 and M4,
Step 2: Calculate the average grade with formula "Grade=(M1+M2+M3+M4)/4"
Step 3: If the average grade is less than 60, print "FAIL", else print "PASS".

Flowchart:

CSC01: Introduction to Computing ODD SEM 2020, DEPT OF CSE 16

You might also like