Problem Solving and Programming using C 22CSC01N
• Objectives
• Outcomes
• Syllabus
• Text and Reference Books
• e-Resources
• Introduction to PSPC
Dr. T. Satyanarayana Murthy, B.Tech, M.Tech, Ph.D., (National Institute of
Technology, Tiruchirapalli)
Associate Professor, Department of Information Technology
Chaitanya Bharathi Institute of Technology, Hyderabad, Telangana, INDIA
www.cbit.ac.in
Problem Solving and Programming using C
Course Objectives:
1. Understanding the steps in problem solving and formulation of
algorithms to problems.
2. Develop programming skills as a means of implementing an algorithmic
solution with appropriate control and data structures.
3. Develop intuition to enable students to come up with creative
approaches to problems.
2
Problem Solving and Programming using C
Course Outcomes:
Formulate solutions to problems and represent those using algorithms/
1.
Flowcharts.
2. Choose proper control statements and data structures to implement the algorithms.
3. Decompose a problem into modules and use functions to implement the modules.
4. Develop programs using arrays, pointers and structures.
5. Develop applications using file I/O.
3
Problem Solving and Programming using C
SYLLABUS:
UNIT I
Introduction: Introduction to Programming, Idea of Algorithm, Representation of
Algorithm, Flowchart, from algorithms to programs, source code.
Basics of C: Background, Structure of a C Program, Datatypes, Tokens, Operators
and Expressions- Evaluating Expressions, Precedence and Associativity of
Operators, Type Conversions, Input and Output Functions.
4
Problem Solving and Programming using C
SYLLABUS:
UNIT II
Control Statements: Conditional Execution -Selection Statements, Conditional
Operator, Switch statement. Iteration Execution - While Construct, For Construct,
do-while Construct Goto Statement, Special Control Statements, Nested Loops.
Arrays: One-Dimensional Arrays-Declaration, Initialization, internal
representation. Multidimensional Arrays.
5
Problem Solving and Programming using C
SYLLABUS:
UNIT III
Strings: Strings: One-dimensional Character Arrays, Arrays of Strings: Two-
dimensional Character Array
Functions: Concept, Uses, Prototype, Declaration, Parameter passing techniques,
Passing Arrays to Functions, Storage Classes, Recursion.
6
Problem Solving and Programming using C
SYLLABUS:
UNIT IV
Search and Sorting: searching algorithms-linear, binary. sorting algorithms-bubble
sort, selection sort. Pointers: Declaring a Pointer, Initializing Pointers, Indirection
Operator and Dereferencing, Arrays and Pointers, Pointers and Strings, Pointers
to Pointers, Array of Pointers, Pointers to an Array, Two-dimensional Arrays and
Pointers, Pointers to Functions and Dynamic Memory Allocation.
7
Problem Solving and Programming using C
SYLLABUS:
UNIT V
User defined Datatypes: Structures- Declaring Structures and Structure Variables,
Accessing the Members of a Structure, Initialization of Structures, Typedef,
Nesting of Structures, Arrays and Structures, Structures and Pointers, Structures
and Functions, Union, Enumeration Types.
Files: Using Files in C, Declaration of File Pointer, Working with Text Files,
Character Input and Output, Working with Binary Files, Sequential Versus Random
File Access, File Record. 8
Problem Solving and Programming using C
Text Books and References:
1. PradipDey and Manas Ghosh ―Programming in C 2/e‖ Oxford
University Press , 2nd Edition 2012.
Suggested Reading:
1.B. W. Kernighan and D.M. Ritchie, "The 'C' Programming Language‖
Prentice Hall India, 2nd Edition. 1990.
2.B.A.Forouzan and R.F. Gilberg A Structured Programming Approach in
C, Cengage Learning,2007.
3.Byron Gottfried, Schaum‟s‖Outline of Programming with C‖, McGraw-
Hill.
4.E. Balaguruswamy, Programming in ANSI C, Tata McGraw-Hill
9
Problem Solving and Programming using C
Problem Solving and Programming
using C
10
CHAPTE Introduction to
Programming,
R1 Algorithms & Flow
Charts
© Oxford University
Press 2013. All rights
Program & Programming
• A program is a set of logically related instructions that is arranged in a
sequence that directs the computer in solving a problem.
• The process of writing a program is called programming.
• Software is a collection of computer programs and related data that
provides the instructions for telling a computer what to do and how to do
it.
• Computer software can be broadly classified into two categories :
(a) system software: OS, Device Drivers, Bios, Firmware1
&
(b) application software: word processor, presentation software, etc..
© Oxford University Press 2013. All
rights reserved.
SYSTEM SOFTWARE
• System software is a collection of programs that
interfaces with the hardware.
• Categories of system software :
© Oxford University Press 2013. All
rights reserved.
APPLICATION SOFTWARE
• Application software is written to enable the computer to
solve a specific data processing task.
• Categories of application software :
© Oxford University Press 2013. All
rights reserved.
PROGRAMMING LANGUAGE
• A programming language is composed of a set of
instructions in a language understandable to the
programmer and recognizable by a computer.
• Programming languages can be classified as
(a) High-level language – C,BASIC, COBOL &
FORTRAN(application programs).
(b) Assembly level language –Intel 8086, 8088
(c) Low level language -0,1
© Oxford University Press 2013. All
rights reserved.
COMPILER
• For executing a program written in a high-level language, it
must be first translated into a form the machine can
understand. This is done by a software called the compiler.
• The compiling process consists of two steps:
a . The analysis of the source program and
b . The synthesis of the object program in the machine
language of the specified machine.
© Oxford University
Press 2013. All rights
INTERPRETER
• During the process of translation
There is another type of
software that also does
translation. This is called an
interpreter.
• Differences between compiler
and interpreter :
© Oxford University Press 2013. All
rights reserved.
INTERPRETER
© Oxford University Press 2013. All
rights reserved.
COMPILING & EXECUTING HIGH LEVEL
LANGUAGE
• The compiling process consists of two steps: the analysis of the source program
and the synthesis of the object program in the machine language of the specified
machine.
• The analysis phase uses the precise description of the source programming
language.
• A source language is described using (a) lexical rules, (b)syntax rules, and
(c)semantic rules.
© Oxford University Press 2013. All
rights reserved.
THE PROCESS OF
COMPILATION
© Oxford University Press 2013. All
rights reserved.
EXECUTION STEPS OF A PROGRAM
• Steps :
1. Translation of the program resulting in the object program.
2. Linking of the translated program with other object programs needed for
execution, thereby resulting in a binary program.
3. Relocation of the program to execute from the specific memory area allocated to
it.
4. Loading of the program in the memory for the purpose of execution.
© Oxford University Press 2013. All
rights reserved.
LINKER
• Linking resolves symbolic references between object
programs. It makes object programs known to each other.
• Linking makes the addresses of programs known to each
other so that transfer of control from one subprogram to
another or a main program takes place during execution.
• In FORTRAN/COBOL , all program units are translated
separately.
© Oxford University Press 2013. All
rights reserved.
RELOCATION
• Relocation is more than simply moving a program from one area to another in the
main memory.
• Relocation means adjustment of all address-dependent locations, such as address
constant, to correspond to the allocated space, which means simple modification
of the object program so that it can be loaded at an address different from the
location originally specified.
© Oxford University Press 2013. All
rights reserved.
LOADER
• Loading means physically placing the machine instructions
and data into main memory, also known as primary
storage area.
• The functions performed by the loader are :
a. Assignment of load-time storage area to the program
b. Loading of program into assigned area
c. Relocation of program to execute properly from its load
time storage area
d. Linking of programs with one another
© Oxford University Press 2013. All
rights reserved.
PROGRAM EXECUTION
• When a program is compiled and linked, each instruction and each item of data is
assigned an address.
• At execution time, the CPU finds instructions and data from these addresses.
• The program counter, is a CPU register that holds the address of the next
instruction to be executed in a program.
• The CPU has random access capability to any and all words of the memory, no
matter what their addresses.
© Oxford University Press 2013. All
rights reserved.
BLOCK DIAGRAM OF PROGRAM
EXECUTION
© Oxford University Press 2013. All
rights reserved.
THIRD,FORTH & FIFTH GENERATION
LANGUAGE
• Third generation programming language specifies how to perform a task using a
large number of procedural instructions and is file oriented.
• Fourth generation programming language specifies what task has to be performed
using fewer instructions and is database oriented.
• Fifth generation programming language resembles human speech and eliminates
the need for the user or programmer to learn a specific vocabulary, grammar ,or
syntax.
© Oxford University Press 2013. All
rights reserved.
CLASSIFICATION OF PROGRAMMING
LANGUAGES
© Oxford University Press 2013. All
rights reserved.
STUCTURED PROGRAMMING
• Structured programming involves top–down analysis for
program solving, modularization of program structure and
organizing structured code for individual module.
• Top-down analysis breaks the whole problem into smaller
logical tasks and defines the hierarchical link between the
tasks.
• Modularization of program structure means making the
small logical tasks into independent program modules that
carries out the desired tasks.
• Structured coding is structured programming which consists
of writing a program that produces a well organized module.
© Oxford University Press 2013. All
rights reserved.
ALGORITHM
• An algorithm is ‘an effective procedure for solving a problem
in a finite number of steps’.
• A well-designed algorithm has termination and correctness
properties.
• The four common ways of representing an algorithm are the
Step-form, Pseudo-code, Flowchart and Nassi-Schneiderman
.
• algorithms show these three features:
a. Sequence (also known as process)
b. Decision (also known as selection)
c. Repetition (also known as iteration or looping)
© Oxford University Press 2013. All
rights reserved.
5 main features
• Input: An algorithm has zero or more inputs. Each that contains a fundamental
operator must accept zero or more inputs.
• Output: An algorithm produces at least one output. Every instruction that
contains a fundamental operator must accept zero or more inputs.
• Definiteness: All instructions in an algorithm must be unambiguous, precise, and
easy to interpret. By referring to any of the instructions in an algorithm one can
clearly understand what is to be done. Every fundamental operator in instruction
must be defined without any ambiguity.
• Finiteness: An algorithm must terminate after a finite number of steps in all test
cases. Every instruction which contains a fundamental operator must be
terminated within a finite amount of time. Infinite loops or recursive functions
without base conditions do not possess finiteness.
• Effectiveness: An algorithm must be developed by using very basic, simple, and
feasible operations so that one can trace it out by using just paper and pencil.
© Oxford University Press 2013. All
rights reserved.
VARIABLE & SUBROUTINE
• A variable, which has a name, is a container for a value that
may vary during the execution of the program.
• A subroutine is a logical collection of instructions that is
invoked from within a larger program to perform a specific
task.
• The subroutine is relatively independent of the remaining
statements of the program that invokes it & can be invoked
several times from several places during a single execution.
• After completing the specific task, a subroutine returns to
the point of invocation in the larger program.
© Oxford University Press 2013. All
rights reserved.
PSEUDO CODE & FLOW
CHART
• Like step-form, Pseudo-code is a written statement of an algorithm using a
restricted and well-defined vocabulary.
• A flowchart comprises of a set of standard shaped boxes that are interconnected
by flow lines to represent an algorithm.
• There should be a logical start and stop to the flowchart.
• The usual direction of the flow of a procedure or system is from left to right or top
to bottom.
• The intersection of flow lines should be avoided.
• Flowcharts facilitate communication between programmers and users.
© Oxford University Press 2013. All
rights reserved.
EXAMPLE: PSEUDO CODE
• Problem:
Write an algorithm to find
out whether a given number
is a prime number or not.
• Solution:
The algorithm for checking
whether a given number is a
prime number or not is as
follows:
© Oxford University Press 2013. All
rights reserved.
FLOW CHARTS : SYMBOLIC
REPRESENTATION
• The START and STOP are represented by an ellipse-like
figure :
• Decisions construct by the rhombus-like figure :
• The processes by rectangles :
• Input / Output by parallelograms :
• Lines and arrows connect these blocks.
© Oxford University Press 2013. All
rights reserved.
FLOW CHARTS:SYMBOLIC
REPRESENTATION
© Oxford University Press 2013. All
rights reserved.
FLOW-CHART ADVANTAGES
• Communication
• Effective analysis
• Proper documentation
• Efficient coding
• Proper debugging
• Efficient program maintenance
© Oxford University Press 2013. All
rights reserved.
FLOW-CHARTS
LIMITATIONS
• Complex logic
• Alterations and modifications
• Reproduction
• Loss of objective
© Oxford University Press 2013. All
rights reserved.
FLOWCHART EXAMPLE
• Problem:
Draw a flowchart to find the
roots of a quadratic
equation.
© Oxford University Press 2013. All
rights reserved.
FLOW-CHART EXAMPLE
• Problem:
Prepare a flowchart to read
the marks of a student and
classify them into different
grades. If the marks secured
are greater than or equal to
90, the student is awarded
Grade A; if they are greater
than or equal to 80 but less
than 90, Grade B is awarded; if
they are greater than or equal
to 65 but less than 80, Grade C
is awarded; otherwise Grade
D is awarded.
© Oxford University Press 2013. All
rights reserved.
ALGORITHM : DESIGNING STATEGY
1. Identify the outputs needed.
2. Identify the input variables available.
3. Identify the major decisions and conditions.
4. Identify the processes required to transform inputs into required outputs.
5. Identify the environment available.
© Oxford University Press 2013. All
rights reserved.
TOP DOWN DEVELOPMENT
STEP
• The top-down development phase plans out the way the solution has to be done
by breaking it into smaller modules and establishing a logical connection among
them.
• Stepwise refinement :
a. Work out each and every detail for each small piece of manageable solution
procedure.
b. Decompose any solution procedure into further smaller pieces and iterate until the
desired level of detail is achieved.
© Oxford University Press 2013. All
rights reserved.
Cont.
c. Group processes together which have some commonality.
d. Group variables together which have some appropriate commonality.
e. Test each small procedure for its detail and correctness and its interfacing with the
other small procedures.
© Oxford University Press 2013. All
rights reserved.
Tracing an Algorithm to Depict logic
• An algorithm can be traced by verifying every procedure one
by one to determine and confirm the corresponding result
that is to be obtained.
• Example:
© Oxford University Press 2013. All
rights reserved.
CONVERSION
• Specification for Converting Algorithms into Programs:
The general procedure to convert an algorithm into a program is to code the
algorithm using a suitable programming language, check the program code by
employing the desk-check method and finally evaluate and modify the program, if
needed.
© Oxford University Press 2013. All
rights reserved.
Structure of C Language
© Oxford University Press 2013. All
rights reserved.
Structure of C Language
© Oxford University Press 2013. All
rights reserved.
Structure of C Language
© Oxford University Press 2013. All
rights reserved.
Contact Details
Associate Professor
Department of Information Technology
Dr. T.Satyanarayana Murthy
B.Tech (IT), M.Tech (AI&R), Ph.D (CA)
9025887845
Member of SCRS
Profile Links:
E -Mail:
[email protected]Linkedin: Mobile: +91 9025887845
https://www.linkedin.com/in/dr-t-satyanarayana-murthy-71b83820/
Scopus:
https://www.scopus.com/authid/detail.uri?authorId=56041063200
https://www.scopus.com/authid/detail.uri?authorId=57212305084
https://www.scopus.com/authid/detail.uri?authorId=57848207000
Google scholar:
https://scholar.google.co.in/citations?user=pQuC-yIAAAAJ&hl=en
Web of science:
https://www.webofscience.com/wos/author/record/ABF-9290-2021
Orcid:
http://orcid.org/0000-0002-6320-8496
- Happy Learning
49