Download PROGRAMMING AND PROBLEM SOLVING WITH C LANGUAGE and more Thesis C programming in PDF only on Docsity!
Programming and Problem Solving
Through ëCí Language
P u blish ed by :
FIREWALL MEDIA
( A n Im prin t of L axm i P u blication s P vt. L td. ) 113, G olden H ou se, D aryagan j, N ew D elhi- P h on e : 011-43 53 25 00 F ax : 011-43 53 25 28 w w w .laxm ipu blication s.com in fo@ laxm ipu blication s.com
P rice : 295. 00 O n ly.. N ew E d ition
O F F IC E S
B a n ga lo re 080-26 75 69 30 C h en n a i 044-24 34 47 26
C o ch in 0484-237 70 04, 405 13 03 G u w a h a ti 0361-251 36 69, 251 38 81
H y d era b a d 040-24 65 23 33 J a lan d h a r 0181-222 12 72
K o lk a ta 033-22 27 43 84 L u ck n o w 0522-220 99 16
M u m b a i 022-24 91 54 15, 24 92 78 69 R a n ch i 0651-221 47 64
FPR-3108-295-PROG PROB SOL TH C LANG-BAN Có5519/012/ Typeset at : Kalyani Computer Services, Delhi. Printed at : Ajit Printing Press, Delhi.
C opyrigh t © 2013 by L axm i P u blication s P vt. L td. A ll righ ts reserved. N o part of th is pu blication m ay be reprod u ced , stored in a retrieval system , or tran sm itted in any form or by an y m eans,electronic,m echanical,ph otocopyin g, record in g or oth erw ise w ith ou t th e prior w ritten perm ission of th e pu blish er.
Contents IX
- Introduction to Programming Contents
- 1.1 The Basic Model of Computation
- 1.2 Problem Solving
- 1.3 Algorithms
- 1.4 Flow-Charts
- 1.5 Programming Languages
- 1.6 Compilation
- 1.7 Linking and Loading
- 1.8 Testing and Debugging
- 1.9 Documentation
- Algorithms for Problem Solving
- 2.1 Exchanging Values of Two Variables
- 2.2 Summation of a Set of Numbers
- 2.3 Decimal Base to Binary Base Conversion
- 2.4 Reversing Digits of an Integer
- 2.5 GOD (Greatest Common Divisor) of Two Numbers
- 2.6 Test Whether a Number is Prime
- 2.7 Organize Numbers in Ascending Order
- 2.8 Find Square Root of a Number
- 2.9 Factorial Computation
- 2.10 Fibonacci Sequence
- 2.11 Evaluate ‘Sin x’ as Sum of a Series
- 2.12 Reverse Order of Elements of an Array
- 2.13 Find Largest Number in an Array
- 2.14 Print Elements of Upper Triangular Matrix
- 2.15 Evaluate a Polynomial
- 2.16 Multiplication of Two Matrices
- Introduction to ‘C’ Language
- 3.1 Character set
- 3.2 Variables and identifiers
- 3.3 Built-in Data Types
- 3.4 Variable Definition
- 3.5 Arithmetic operators
- 3.6 Expressions
- 3.7 Constants and Literals
- 3.8 Simple Assignment Statement
- 3.9 Basic Input/Output Statement
- Conditional Statements and Loops
- 4.1 Decision Making within a Program
- 4.2 Conditions and Relational Operators
- 4.3 Logical Connectives
- 4.4 If statement
- 4.5 If-else statement
- 4.6 Loops
- 4.7 While loop
- 4.8 Do while ...........................................................................................................................................
- 4.9 For loops ..........................................................................................................................................
- 4.10 Nested Loops
- 4.11 Infinite Loops .................................................................................................................................. (VI)
- 4.12 Switch statement ..............................................................................................................................
- 4.13 Structured programming ..................................................................................................................
- Arrays
- 5.1 Introduction to Arrays
- 5.2 Array Manipulation .........................................................................................................................
- 5.3 Searching .........................................................................................................................................
- 5.4 Insertion ..........................................................................................................................................
- 5.5 Deletion of an Element from an Array
- 5.6 Finding the Largest/Smallest Element in an Array
- 5.7 Two Dimensional Arrays ..................................................................................................................
- 5.8 Addition/Multiplication of Two Matrices
- 5.9 Transpose of a Square Matrix
- 5.10 Null Terminated Strings as Array of Characters ...............................................................................
- 5.11 Standard Library String Functions
- 5.12 Array Bounds Checking ..................................................................................................................
- 5.13 Arrays and Functions
- 5.14 Visualizing A C String .......................................................................................................................
- 5.15 Vector Operation ..............................................................................................................................
- 5.16 Random Access ...............................................................................................................................
- 5.17 Multidimensional Arrays and Functions .........................................................................................
- 5.18 Multidimensional Arrays .................................................................................................................
- Functions
- 6.1 Top-down Approach of Problem Solving ........................................................................................
- 6.2 Modular Programming and Functions
- 6.3 Standard Library of C Functions ......................................................................................................
- 6.4 Prototype of a Function ...................................................................................................................
- 6.5 Format Parameter List
- 6.6 Return Type .....................................................................................................................................
- 6.7 Function Call ....................................................................................................................................
- 6.8 Passing Arguments to a Function
- 6.10 Call by Reference .............................................................................................................................
- 6.11 Call by Value ....................................................................................................................................
- 6.12 Recursive Functions ........................................................................................................................
- 6.13 Arrays as Function Arguments .......................................................................................................
- Storage Classes
- 7.1 Scope and Extent .............................................................................................................................
- 7.2 Storage Classes in a Single Source File ...........................................................................................
- 7.3 Storage Classes in Multiple Source Files .........................................................................................
- 7.4 Extern ...............................................................................................................................................
- 7.5 Static ................................................................................................................................................
- Structures and Unions
- 8.1 Structure Variables
- 8.2 Initialization .....................................................................................................................................
- 8.3 Structure Assignment
- 8.4 Nested Structure ..............................................................................................................................
- 8.5 Structures and Functions
- 8.6 Structures and Arrays
- 8.7 Arrays of Structures ........................................................................................................................
- 8.8 Structures containing Arrays ...........................................................................................................
- 8.9 Unions
- Pointers (VII)
- 9.1 Address Operators ..........................................................................................................................
- 9.2 Pointer Type Declaration
- 9.3 Pointer Assignment .........................................................................................................................
- 9.4 Pointer Initialization .........................................................................................................................
- 9.5 Pointer Arithmetic
- 9.6 Functions and Pointers ....................................................................................................................
- 9.7 Arrays and Pointers .........................................................................................................................
- 9.8 Pointer Arrays
- 9.9 Pointers and Structures ...................................................................................................................
- 9.10 Dynamic Memory Allocation
- Self Referential Structures and Linked Lists
- 10.1 Creation of a Singly Connected Linked List ....................................................................................
- 10.2 Traversing a Linked List ..................................................................................................................
- 10.3 Insertion into a Linked List ..............................................................................................................
- 10.4 Deletion from a Linked List ..............................................................................................................
- File Processing
- 11.1 Concept of Files ...............................................................................................................................
- 11.2 File Opening in Various Modes .......................................................................................................
- 11.3 Closing of a File ...............................................................................................................................
- 11.4 Reading from a File ..........................................................................................................................
- 11.5 Writing onto a File
- Sample Paper –
- Sample Paper –
- Sample Paper –
- Sample Paper –
- Sample Paper –
- Glossary
- Index
SYLLABUS
M3-R4: PROGRAMMING AND PROBLEM SOLVING
THROUGH ‘C’ LANGUAGE
The objective of this course are to make the student understand programmng language, programming, concepts of loops, reading a set of data, stepwise refinement, Functon, Control strucutres, Arrays. After completion of this course the student is expected to analyze the real life problem and write a program in ‘C’ language to solve the problem. The main emphases of the course will on problem solving aspect, i,e., developing proper algorithms. At the end of the course the students will be able to:
- Develop efficient algorithms for solving a problem.
- Use the various constructs of a programming language viz., conditional, iteration and recursion.
- Implement the algorithm in ‘C’ language.
- Use simple data structures like arrays, stacks, and linked list in solving problems.
- Handling File in ‘C’.
Outline of Course
S.No. Topic Minimum No. of Hours
1. Introduction to Programming 04
2. Algorithm for Problem Solving 10
3. Introduction to ‘C’ Language 04
4. Conditional Staements and Loops 07
5. Arrays 06
6. Functions 06
7. Storage Classes 03
8. Structures and Unions 06
9. Pointers 06
10. Self Referential Structures and Linked Lists 04
11. File Processing 04
Lectures = 60
Practicals /Tutorials = 60
Total = 120
(XI)
DETAILED SYLLABUS
1. Introduction to Programming 04 Hrs.
The Basic Model of Computation, Algorithms, Flow-Charts, Programming Languages,
Compilation, Linking and Loading, Testing and Debugging, Documentation.
2. Algorithms for Problem Solving 10 Hrs.
Exchanging values of two variables, Summation of a set of numbers, Decimal Base
to Binary Base Conversion, Reversing digits of an integer, GOD (Greatest Common
Division) of two numbers, Test whether a number is prime, Organize numbers in
ascending order, Find square root of a number, Factorial computation, Fibonacci
sequence, Evaluate ‘sin x’ as sum of a series, Reverse order of elements of an
array, Find largest number in an array, Print elements of upper triangular matrix,
multiplication of two matrices, Evaluate a Polynomial.
3. Introduction to ‘C’ Language 04 hrs.
Character set, Variables and identifiers, Built-in Data Types, Variable Definition,
Arithmetic operators, and expressions, Constants and Literals, Simple assignment
statement, Basic input/output statement, Simple ‘C’ programs.
4. Conditional Statements and Loops 07 Hrs.
Decision making within a program, Conditions, Relational Operators, Logical
Connectives, If statement, If-else statement, Loops, While loop, Do while, For loop,
Nested loops, Infinite loops, Switch statement, Structured programming.
5. Arrays 06 Hrs.
One dimensional arrays, Array manipulation; Searching, Insertion, Deletion of an
element from an array; Finding the largest/smallest element in an array; Two
dimensional arrays; Addition/Multiplication of two matrices, Transpose of a square
matrix; Null terminated strings as array of characters, Standard library string functions.
6. Functions 06 Hrs.
Top-down approach of problem solving, Modular programming and functions,
Standard Library of C functions, Prototype of a function; Format parameter list,
Return Type, Function call, Block structure, Passing arguments to a Function; call
by reference, call by value, Recursive Functions, arrays as function arguments.
7. Storage Classes 03 Hrs.
Scope and extent, Storage Classes in a single source file; auto, extern and static,
register, Storage Classes in a multiple source files; extern and static.
Introduction to
Programming
Chapter
1.1 THE BASIC MODEL OF COMPUTATION
A good programmer is an asset to the company. But, nobody is born to write programs. A good
programmer is not born but is developed by his own actions. These actions make him a good
programmer. There is a methology which he has to follow to become a good programmer. In this
chapter I will discuss this methology, and how it is used while writing programs irrespective of the
language he is using.
Programming is a team effort. What can a good programmer do if the inputs to him are given wrong.
He has to coordinate between the various users and data providers to cater to their needs and write a
good program which would produce the results the user is looking forward to. A manager, for example,
is not concerned how the problem is solved or how the data is obtained. He is only interested in
results which the program is supposed to provide.
For writing any program the programmer has to keep lots of things in mind. Few of them have been
mentioned above, the others including problem definition, program design, coding, debugging, testing,
documentation, maintenance and redesign. Let us start with the general concepts used for writing any
program.
1.1.1 General Concepts
Program writing is definitely a technique. One has to master it by experience. One should always
start with writing small and simple programs and then graduate to complex and complicated programs.
Whatever he may write, it should be written in a manner which is understood by others. God forbid
if he is not around the others should be able to run and modify it. Here the documentation parts becomes
very necessary. I will talk about it little later but first let us see what a good program should have.
A good program should have the following characteristics:
1. Readability
2. Efficiency
3. Reliability
4. Meaningfulness
5. Portability
6. User-friendly.
Let us read about them in little details.
1.1.1.1 Readability
As mentioned above the program should be such that it can be read and understood by others. This
makes it easy to modify by others.
2 Programming and Problem Solving Through ‘C’ Language
1.1.1.2 Efficiency
While writing the program the programmer must take care of the following:
1. Processing
He should make sure that the program takes the minimum processing time.
2. Memory
Mimimum utalization of memory and hard disk space should be used.
3. Time
The programmer should be able to produce the program in time frame given.
1.1.1.3 Realiability
It goes beyond saying that the program should be reliable and must be in a position to produce good
and accurate results. It should take care that the various checks are provided to overcome wrong
data and machine malfunctions.
1.1.1.4 Meaningfulness
The program should give the meaningful outputs.
1.1.1.5 Portability
With the advent of PCs the compatability of one computer with another has been taken care of. But,
still there is a possibility that the program may be required to run on a machine which is alien to the
program. In this case the program should be such that it needs minimum efforts to be modified to
suit the new environments.
1.1.1.6 User-friendly
A program should be user-friendly.
1.2 PROBLEM SOLVING
It is impossible to solve a problem by using a computer, without a clear understanding and identification
of the problem. Inadequate recognition of a problem is the key element responsible for poor performance
of computers. This step is generally difficult and the programmer should invest a significant portion
of his time in problem identification. If he does not spend enough time at this state, he may find that
his well-written program fails to solve the real problem.
1.2.1 Understanding of the Problem
Depending on the complexity of the problem, the result of a careful analysis may be simply a testing
of the factors with which the program must deal, or it may be a formal written statement of the
problem, supported by a description of all the considerations necessary to solve the problem. This
step is the process of becoming familiar with the problem that will be solved with a computer program.
It starts when the programmer is assigned the task.
This step includes the reviewing of the design document that was prepared for the program, as well
as any system-wide information that would be helpful. The process ends when all the programmer’s
questions have been resolved and the requirements of the program are understood.