KEMBAR78
Introductiontoalgoritms Unit 1 | PDF | Pointer (Computer Programming) | Control Flow
0% found this document useful (0 votes)
9 views35 pages

Introductiontoalgoritms Unit 1

The document outlines a course on Problem Solving and Programming in C, detailing objectives and learning outcomes focused on algorithm formulation and C programming constructs. It covers topics such as decision control, looping statements, functions, arrays, pointers, and file handling, along with practical applications and examples. Additionally, it includes an introduction to algorithms, flowcharts, and the differences between algorithms and programs.
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 views35 pages

Introductiontoalgoritms Unit 1

The document outlines a course on Problem Solving and Programming in C, detailing objectives and learning outcomes focused on algorithm formulation and C programming constructs. It covers topics such as decision control, looping statements, functions, arrays, pointers, and file handling, along with practical applications and examples. Additionally, it includes an introduction to algorithms, flowcharts, and the differences between algorithms and programs.
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/ 35

PROBLEM SOLVING AND PROGRAMMING IN C

Dr. J. RAMYA
AP-II / School of Computing
PROBLEM SOLVING AND PROGRAMMING IN C
COURSE OBJECTIVES

This course will help the learner to formulate simple algorithms for arithmetic & logical
problems and translate the algorithms to C programs using constructs like arrays,
functions, pointers, structures & files

COURSE LEARNING OUTCOMES


Upon successful completion of this course, the learner will be able to

• Write a simple • Illustrate the


algorithm for a given • Develop programs
operations on arrays
problem using input and output
and composite data
• Illustrate declarations statements
types
and operations in C • Select an
• Develop applications
language appropriate C control
using functions,
structure to solve the
pointers and files
given problem
UNIT - I: Introduction to C, Decision Control and Looping
Statements (12 PERIODS)

Decision Control and Looping Statements


Conditional Branching - Iterative - Nested Loops - Break
and Continue - goto. Applications: Even or odd number,
Greatest of three numbers, Finding roots of a quadratic
equation, Factorial, Sum of n numbers, Operations on
digits of an integer, gcd, Base conversion, Generation of
Introduction to C fibonacci series.
Introduction - Structure of C program - Files used in C
Program - Tokens - Character Set - Keyword - Identifiers -
Basic Data Type - Constants and Variables - I/O Statements -
Operators - Type Conversion and Type Casting. Applications:
Swapping of two numbers, Simple arithmetic problems.

Introduction to Algorithms
Building Blocks of Algorithms - Notation for Deploying
Algorithms.
UNIT - III : Strings, Structures and Unions (11 PERIODS)
STRUCTURES AND UNIONS
Introduction - Nested and Array of Structures - Structures
and Functions - Union - Enumerated Data Type.
Applications: Programs using array of structures.

STRINGS
Introduction - Suppressing Input - Taxonomy - Operations on Strings -
Miscellaneous and Character Functions - Array of Strings. Applications:
Keyword searching in a text, Searching & sorting of strings.

UNIT - II : Functions and Arrays (11 PERIODS)


ARRAYS
Arrays: Introduction - Declaration - Access of Array Elements - Storing Value -
Operations - Passing Array to Functions - 2D Array and its Operations - Passing
2D Array to Functions - Multidimensional Array. Applications: Insertion and
deletion in an array, Bubble sort, Linear and binary search, Removal of
duplicates from an ordered array, Basic matrix operations.

FUNCTIONS
Introduction - Declarations - Definitions - Call and Return statement
- Passing Parameters - Variable Scope - Storage Class - Recursive Functions
8/7/2025
and its Types Applications: Factorial, Sum of n numbers, Operations on digits
Dr.J.Ramya,AP-II/SoC 4
of an integer, gcd, Base conversion, Generation of Fibonacci series.
UNIT - IV : Pointers and Files (11 PERIODS)
FILES
Introduction - Using Files in C - Read and Write - EoF - Error
Handling - Functions for Selecting a Record Randomly - Rename
a File. Applications: Random read and write on files

POINTERS
Introduction - Declaration - Pointer Expression and Arithmetic - Null and
Generic Pointers - Passing Arguments using Functions - Comparison of Array
and Pointer - Pointers and Strings - Array of Pointers - Function Pointer -
Pointer to Pointer - Self Referential Structure - Dynamic Memory Allocation -
Drawbacks of Pointers. Applications: Linked List operations.
TEXTBOOK
1. Reema Thareja, Programming in C, 3rd Edition, Oxford University Press, 2023.
REFERENCES
1. Pradip Dey. and Manas Ghosh, Computer fundamentals and programming in C, 2nd Edition, Oxford University Press, 2013.
2. Byron, Gottfried, Programming with C. 3rd Edition, McGraw Hill Education, 2017.
3. Anil Bikas Chaudhuri, The art of programming through flowcharts & algorithms, 1st Edition, Firewall Media, 2018.
4. Brian W Kernighan. and Dennis M Ritchie, The C programming language, 2nd Edition, Pearson Education, 2017.
ONLINE MATERIALS
1. NPTEL -http://nptel.ac.in/courses/106104128/
2. NPTEL -http://nptel.ac.in/courses/106102066/
8/7/2025 Dr.J.Ramya,AP-II/SoC 5
What is computer? Characteristics of computer
A computer is a machine/device that takes instructions
and performs computations based on those
instructions.

BASIC ORGANIZATION OF A COMPUTER


A computer is an electronic device which basically
performs five major operations which includes:
Data and INPUT STORAGE OUTPUT Results
1) accepts data or instructions (input) instructions

2) stores data CPU


CONTROL

3) process data UNIT


ARITHMETIC Flow of data and instructions
LOGIC UNIT
4) displays results (output) and Control exercised by control unit

5) controls and co-ordinates all operations inside a computer


A computer system consists of two major components:
•Computer Hardware
•Computer Software Generation
•Hardware performs physical tasks.
•Software gives instructions to hardware.

8/7/2025 Dr.J.Ramya,AP-II/SoC 7
Introduction to Algorithms

8/7/2025 Dr.J.Ramya,AP-II/SoC 8
Algorithm is a finite sequence of steps to solve a computational problem

8/7/2025 Dr.J.Ramya,AP-II/SoC 9
Building Blocks of
algorithm

8/7/2025 Dr.J.Ramya,AP-II/SoC 10
ALGORITHM SOLVING STEPS

8/7/2025 Dr.J.Ramya,AP-II/SoC 11
Pseudo Code
• More formal representation than algorithm
• Step-by-Step formal representation
• It uses START and STOP or END keywords

Example
1. START
2. Read number1, number2
3. Sum number1 + number2
4. Print sum
5. STOP
Pseudo Code-Greatest of 2 numbers
1. START
2. Read number1, number2
3. If number1 > number2 then
4. Print number1
5. Else
6. Print number2
7. STOP
BUILDING BLOCKS OF ALGORITHM

Selection Logic Structure

8/7/2025 Dr.J.Ramya,AP-II/SoC 14
BUILDING BLOCKS OF ALGORITHM
Iterative Logic Structure (Looping): Pre-test Iteration (Entry-controlled Loop)

•Iterative logic structure is also known as a loop.


•Looping means repeating a set of operations
multiple times to obtain a desired result.
Types of Iteration:
1.Pre-test Iteration (Entry-controlled Loop):
1. The condition (predicate) is tested before
executing the loop body.
2. If the condition is true, the set of operations
inside the loop is performed.
3. If the condition is false, the loop terminates and
control exits.
4. Common examples: while loop, for loop.

8/7/2025 Dr.J.Ramya,AP-II/SoC 15
BUILDING BLOCKS OF ALGORITHM
2. Post-test Iteration (Exit-controlled Loop):
Post-test Iteration (Exit-controlled Loop)
1. The loop body is executed first, and then the
condition is tested.
2. If the condition is true, the loop repeats.
3. If the condition is false, the loop terminates.
4. Common example: do-while loop.

Key Characteristics:
•Loops help in automation of repetitive tasks.
•Condition checking determines whether the loop
continues or stops.
•Pre-test checks at the start of the loop; post-test
checks after one iteration.

8/7/2025 Dr.J.Ramya,AP-II/SoC 16
DIFFERENCE BETWEEN ALGORITHM AND PROGRAM
Aspect Algorithm Program
A finite sequence of well-defined steps to solve a A set of instructions written in a programming language to
Definition
problem. perform tasks.

Phase At Design Phase — before coding starts. At Implementation Phase — actual coding takes place.

Written in Natural Language (English or Pseudo-


Language Used Written in Programming Languages (C, Java, Python, etc.).
code).
Knowledge Requires Domain Knowledge to understand the Requires Programming Knowledge to translate logic into
Required problem and devise logic. code.
Focus on solving the problem logically (what steps Focus on syntax and code structure (how to instruct the
Focus Area
to follow). machine).
Analyze and verify for correctness and efficiency Testing through execution (unit testing, debugging, runtime
Validation
(dry-run, flowcharts). outputs).
Execution Not directly executable by machines. Executed by machines to perform tasks.

Example Tools Flowcharts, Pseudocode, Decision Tables. Compilers, Interpreters, IDEs.

Role in SDLC Basis for Design Documentation. Basis for Software Development and product delivery.

Creator Role Created by Problem Analysts, Designers. Written by Programmers, Developers.

8/7/2025 Dr.J.Ramya,AP-II/SoC 17
FLOWCHARTS

• System Flowchart: Integrates multiple problem


units to meet overall objectives.
8/7/2025 Dr.J.Ramya,AP-II/SoC 18
FLOWCHARTS SYMBOLS

8/7/2025 Dr.J.Ramya,AP-II/SoC 19
Sequence logic structure

8/7/2025 Dr.J.Ramya,AP-II/SoC 20
SELECTION LOGIC STRUCTURE ITERATION / LOOPING LOGIC

8/7/2025 Dr.J.Ramya,AP-II/SoC 21
Example Algorithm: Find the Largest of Two Numbers
Step 1: Start.
Step 1: Start Step 2: Input two numbers A and B.
Step 2: Input two numbers A and B. Step 3: Check if A > B.
Step 3: If A > B, then •If Yes, go to Step 4.
→ Print "A is larger.“ •If No, go to Step 5.
Step 5:Else Step 4: Print "A is larger."
→ Print "B is larger." •Go to Step 6.
Step 5: End Step 5: Print "B is larger."
Step 6: End.

Characteristic Applied As
Input Two numbers A and B
Output Larger number is printed
Definiteness Clear step-wise instructions
Finiteness Algorithm ends after comparison
Effectiveness Simple comparison and print statements
Sequential Statements Executes from Start → Input → Compare → Output
Decision Making IF-ELSE condition used

8/7/2025 Dr.J.Ramya,AP-II/SoC 22
Construct a flowchart to show how a student’s
registration number and grades in 3 subjects,
m1, m2, and m3, are displayed along with the
total average grade.

Task Analysis. The data supplied as inputs are the


registration number and grades obtained in three
subjects. The registration number contributes
nothing to the process of deriving the desired
output; it just identifies the person about whom the
total grade and the average grade are obtained. The
total grade can be obtained by taking the sum of the
marks m1, m2, and m3, and the average can be
obtained by dividing the total by 3.

8/7/2025 Dr.J.Ramya,AP-II/SoC 23
8/7/2025 Dr.J.Ramya,AP-II/SoC 24
Selection logic structure
Construct a flowchart to determine whether a
given number is even or odd.

Task Analysis. We know that a number is an even


number if it is completely divisible by 2. This means
that if we perform integer division upon the given
number, then the remainder of the division will be zero.
To construct the flowchart, we accept a number as
input, obtain the remainder of the integer division by
taking it as the divisor, and then check whether the
remainder is zero. If it is zero, then our conclusion will
be that the number is an even number; otherwise, it
will be an odd number.

8/7/2025 Dr.J.Ramya,AP-II/SoC 25
Selection logic structure

8/7/2025 Dr.J.Ramya,AP-II/SoC 26
Iteration logic structure

To print numbers 1, 2, 3, 4, 5 using a looping structure.


1.Start
Begin the program.
2.Set counter = 1
Create a variable called counter and set its value to 1. This will be used to keep track of the number.
3.Check: Is counter ≤ 5?
If yes, go to the next step. If no, stop the loop.
4.Print the counter
Show the current number (for example, 1, then 2, and so on).
5.Increase counter by 1
Add 1 to the counter, so it moves to the next number.
6.Repeat
Go back to step 3 and check again.
7.End
When the counter becomes 6, the loop stops, and the program ends.

8/7/2025 Dr.J.Ramya,AP-II/SoC 27
Iteration logic structure

To print numbers 1, 2, 3, 4, 5 using a looping


structure.

Iteration Counter Printed Value Next Counter

1 1 1 2
2 2 2 3
3 3 3 4
4 4 4 5

5 5 5 6 → loop ends

8/7/2025 Dr.J.Ramya,AP-II/SoC 28
Sequence logic structure

8/7/2025 Dr.J.Ramya,AP-II/SoC 29
Sequence logic structure

8/7/2025 Dr.J.Ramya,AP-II/SoC 30
8/7/2025 Dr.J.Ramya,AP-II/SoC 31
Selection logic structure
Develop a flowchart to show how the profit or loss for a sale can be obtained.

Task Analysis. The profit or loss for a sale can be obtained if the cost price and sale price
are known. However, there is a need to make a decision here. If the cost price is more than
the sale price, then it indicates a loss in the process; otherwise, there will be either zero
profit (no profit or a loss) or some profit.

8/7/2025 Dr.J.Ramya,AP-II/SoC 32
Selection logic structure

8/7/2025 Dr.J.Ramya,AP-II/SoC 33
Selection logic structure

8/7/2025 Dr.J.Ramya,AP-II/SoC 34
Iteration logic structure

Algorithm: Find the Sum of First 5 Numbers

Algorithm Steps
1.Start
2.Set counter = 1, sum = 0
3.Repeat while counter ≤ 5
a. Add counter to sum
b. Increment counter by 1
4.Print sum
5.End

8/7/2025 Dr.J.Ramya,AP-II/SoC 35

You might also like