Programming for Problem Solving E Balagurusamy download pdf
Programming for Problem Solving E Balagurusamy download pdf
com
https://ebookmass.com/product/programming-for-problem-
solving-e-balagurusamy/
OR CLICK HERE
DOWLOAD NOW
https://ebookmass.com/product/problem-solving-and-python-programming-
e-balagurusamy/
ebookmass.com
https://ebookmass.com/product/computer-programming-e-balagurusamy/
ebookmass.com
Programming in C E. Balagurusamy
https://ebookmass.com/product/programming-in-c-e-balagurusamy/
ebookmass.com
https://ebookmass.com/product/hill-towns-of-central-italy-rick-steves-
snapshot-rick-steves/
ebookmass.com
Advanced Practice Palliative Nursing, 2e (June 16,
2023)_(0197559328)_(Oxford University Press) Constance
Dahlin
https://ebookmass.com/product/advanced-practice-palliative-
nursing-2e-june-16-2023_0197559328_oxford-university-press-constance-
dahlin/
ebookmass.com
https://ebookmass.com/product/critical-infrastructure-protection-risk-
management-and-resilience-a-policy-perspective-ebook-pdf-version/
ebookmass.com
https://ebookmass.com/product/the-strategic-bond-investor-anthony-
crescenzi/
ebookmass.com
https://ebookmass.com/product/critical-care-secrets-6th-edition-renee-
doney-stapleton-editor/
ebookmass.com
https://ebookmass.com/product/mixed-feelings-en-tijuana-bilinguismo-
sentimiento-y-consumo-transfronterizo-1st-edition-liliana-lanz-
vallejo/
ebookmass.com
Edge of Midnight (The McClouds & Friends Book 4) Shannon
Mckenna
https://ebookmass.com/product/edge-of-midnight-the-mcclouds-friends-
book-4-shannon-mckenna/
ebookmass.com
PROGRAMMING
fOR
PRObleM SOlvING
Gujarat Technological University - 2018
About the Author
E Balagurusamy, is presently the Chairman of EBG Foundation, Coimbatore. In the past he has also held
the positions of member, Union Public Service Commission, New Delhi and Vice-Chancellor, Anna University,
Chennai. He is a teacher, trainer and consultant in the fields of Information Technology and Management.
He holds an ME (Hons) in Electrical Engineering and PhD in Systems Engineering from the Indian Institute
of Technology, Roorkee. His areas of interest include Object-Oriented Software Engineering, E-Governance:
Technology Management, Business Process Re-engineering and Total Quality Management.
A prolific writer, he has authored a large number of research papers and several books.
A recipient of numerous honors and awards, he has been listed in the Directory of Who's Who of Intellectuals
and in the Directory of Distinguished Leaders in Education.
PROGRAMMING
fOR
PRObleM SOlvING
Gujarat Technological University - 2018
E Balagurusamy
Chairman
EBG Foundation
Coimbatore
Information contained in this work has been obtained by McGraw Hill Education (India), from sources believed to be
reliable. However, neither McGraw Hill Education (India) nor its authors guarantee the accuracy or completeness of any
information published herein, and neither McGraw Hill Education (India) nor its authors shall be responsible for any errors,
omissions, or damages arising out of use of this information. This work is published with the understanding that McGraw
Hill Education (India) and its authors are supplying information but are not attempting to render engineering or other
professional services. If such services are required, the assistance of an appropriate professional should be sought.
Typeset at The Composers, 260, C.A. Apartment, Paschim Vihar, New Delhi 110 063 and printed at
Cover Printer:
Cover Designer: APS Compugraphics
Cover Image Source: Shutterstock
Visit us at: www.mheducation.co.in
Write to us at: info.india@mheducation.com
CIN: U22200TN1970PTC111531
Toll Free Number: 1800 103 5875
Preface
P
rogramming for Problem Solving requires a deep understanding of C. C is a powerful, flexible, portable
and elegantly structured programming language. Since C combines the features of high-level language
with the elements of the assembler, it is suitable for both systems and applications programming. It
is undoubtedly the most widely used general-purpose language today in operating systems, and embedded
system development. Its influence is evident in almost all modern programming languages. Since its
standardization in 1989, C has undergone a series of changes and improvements in order to enhance the
usefulness of the language.
Acknowledgements
I owe special thanks to the entire team of McGraw Hill Education India.
A note of acknowledgement is due to the following reviewers for their valuable feedback.
This book is my sincere attempt to make a footprint on the immensely vast and infinite sands of knowledge.
I would request the readers to utilize this book to the maximum extent.
E Balagurusamy
Publisher’s Note
McGraw Hill Education (India) invites suggestions and comments from you, all of which can be sent to info.
india@mheducation.com (kindly mention the title and author name in the subject line).
Piracy-related issues may also be reported.
Contents
Translator Programs 58
Problem-Solving Techniques 59
Using the Computer 70
Learning Outcomes 70
Key Concepts 71
Review Questions 73
Discussion Questions 84
2. Fundamentals of C 87
Learning Objectives 87
History of C 87
Importance of C 89
Sample Program 1: Printing a Message 89
Sample Program 2: Adding Two Numbers 92
Sample Program 3: Interest Calculation 93
Sample Program 4: Use of Subroutines 95
Sample Program 5: Use of Math Functions 96
Basic Structure of C Programs 97
Programming Style 98
Executing a ‘C’ Program 99
UNIX System 99
MS-DOS System 102
Key Concepts 102
Always Remember 103
Review Questions 103
Debugging Exercises 105
Programming Exercises 105
5. Functions 220
Learning Objectives 220
Introduction 220
Need for User-Defined Functions 221
A Multi-Function Program 221
Elements of User-Defined Functions 224
x Contents
6. Pointers 273
Learning Objectives 273
Introduction 273
Understanding Pointers 274
Accessing the Address of a Variable 276
Declaring Pointer Variables 277
Initialization of Pointer Variables 278
Accessing a Variable Through its Pointer 279
Chain of Pointers 281
Pointer Expressions 282
Pointer Increments and Scale Factor 284
Pointers and Arrays 284
Pointers and Character Strings 288
Array of Pointers 290
Pointers as Function Arguments 291
Functions Returning Pointers 294
Contents xi
7. Structure 311
Learning Objectives 311
Introduction 311
Defining a Structure 312
Declaring Structure Variables 313
Accessing Structure Members 315
Structure Initialization 316
Copying and Comparing Structure Variables 318
Operations on Individual Members 320
Arrays of Structures 320
Arrays within Structures 323
Structures within Structures 324
Structures and Functions 326
Unions 329
Size of Structures 330
Bit Fields 330
Key Concepts 333
Always Remember 333
Brief Cases 334
Review Questions 338
Debugging Exercises 341
Programming Exercises 341
Introduction to computer and programming: Introduction, Basic block diagram and functions of various
components of computer, Concepts of Hardware and software, Types of software, Compiler and interpreter,
Concepts of Machine level, Assembly level and high level programming, Flowcharts and Algorithms
Fundamentals of C: Features of C language, structure of C Program, comments, header files, data types,
constants and variables, operators, expressions, evaluation of expressions, type conversion, precedence
and associativity, I/O functions
GO TO Chapter 2 Fundamentals of C
Control structure in C: Simple statements, Decision making statements, Looping statements, Nesting of
control structures, break and continue, goto statement
Array & String: Concepts of array, one and two dimensional arrays, declaration and initialization of arrays,
string, string storage, Built-in-string functions
Recursion: Recursion, as a different way of solving problems. Example programs, such as Finding
Factorial, Fibonacci series, Ackerman function etc. Quick sort or Merge sort.
Functions: Concepts of user defined functions, prototypes, definition of function, parameters, parameter
passing, calling a function, recursive function, Macros, Pre-processing
GO TO Chapter 5 Functions
Pointers: Basics of pointers, pointer to pointer, pointer and array, pointer to array, array to pointer, function
returning pointer
GO TO Chapter 6 Pointers
Structure: Basics of structure, structure members, accessing structure members, nested structures, array
of structures, structure and functions, structures and pointers
GO TO Chapter 7 Structure
introduction
A computer is an electronic machine that takes input from the user, processes the given input and generates
output in the form of useful information. A computer accepts input in different forms such as data, programs
and user reply. Data refer to the raw details that need to be processed to generate some useful information.
Programs refer to the set of instructions that can be executed by the computer in sequential or non-
sequential manner. User reply is the input provided by the user in response to a question asked by the
computer.
A computer includes various devices that function as an integrated system to perform several tasks
described above (Fig. 1.1). These devices are:
Central Processing Unit (CPU)
It is the processor of the computer that is responsible for controlling and executing instructions in the computer.
It is considered as the most significant component of the computer.
Another Random Document on
Scribd Without Any Related Topics
on the extreme right was driven out of the engagement, and the
British gunboats, turning the American flank, attacked the
“Ticonderoga,” which maintained a doubtful battle. The American left
was also turned, the “Eagle” having been driven to take refuge
between the “Saratoga” and “Ticonderoga,” in the centre.
Macdonough’s ship was then exposed to the concentrated fire of the
“Confiance” and “Linnet,” and his battery was soon silenced. The
“Saratoga” could no longer use a gun on the engaged side, and the
battle was nearly lost.
Then Macdonough’s forethought changed the impending defeat
into victory. His fire had nearly silenced the “Confiance,” and
disregarding the “Linnet,” he ceased attention to the battle in order
to direct the operation of winding ship. Little by little hauling the ship
about, he opened on the “Confiance” with one gun after another of
the fresh broadside, as they bore; and the “Confiance,” after trying
in vain to effect the same operation, struck her colors. Then the
British fleet was in the situation which Downie had anticipated for
the Americans in the event of silencing the “Saratoga.” The three
smaller vessels were obliged to surrender, and the gunboats alone
escaped. The battle had lasted from quarter past eight till quarter
before eleven.
POSITIONS
OF THE
BRITISH AND AMERICAN FORCES
AT
PLATTSBURG
AFTER A SKETCH BY BRIG. GEN. MACOMB
By land, the British attack was much less effective than by water.
The troops were slow in reaching their positions, and had time to
make no decisive movement. A column under Major-General
Robinson was ordered to move round by the right flank to a ford
previously reconnoitred, some distance up the Saranac, in order to
gain a position whence they could reverse the American works and
carry them by assault; but Robinson’s column missed its way, and
before reaching the ford heard the cheers of the American troops,
and halted to ascertain its cause.[180] The remainder of the army
waited for Robinson’s column to assault. The casualties showed that
nothing like a serious engagement took place. The entire loss of the
British army from September 6 to September 14 was officially
reported as only thirty-seven killed and one hundred and fifty
wounded, and of this loss a large part occurred previous to the
battle of September 11. The entire American loss was thirty-seven
killed and sixty-two wounded.
In the naval battle, Macdonough reported fifty-two killed and fifty-
eight wounded, among about eight hundred and eighty men. The
British reported fifty-seven killed and seventy-two wounded, in crews
whose number was never precisely known, but was probably fully
eight hundred. In neither case was the loss, though severe, as great
relatively to the numbers as the severity of the action seemed to
imply. The “Saratoga” lost twenty-eight killed in a crew of two
hundred and forty. In Perry’s battle on Lake Erie, the “Lawrence” lost
twenty-two men killed in a crew of one hundred and thirty-one.
About one man in eight was killed on Macdonough’s ship; about one
man in six on Perry’s.
With needless precipitation, Prevost instantly retreated the next
day to Champlain, sacrificing stores to a very great amount, and
losing many men by desertion. The army was cruelly mortified, and
Prevost lost whatever military reputation he still preserved in
Canada. In England the impression of disgrace was equally strong.
“It is scarcely possible to conceive the degree of mortification and
disappointment,” said the “Annual Register,”[181] “which the
intelligence of this defeat created in Great Britain.” Yeo brought
official charges of misconduct against Prevost, and Prevost defended
himself by unusual arguments.
“With whatever sorrow I may think of the unfortunate occurrences
to which I allude,” he wrote to Bathurst, three weeks later,[182] “I
consider them as light and trivial when compared to the disastrous
results which, I am solemnly persuaded, would have ensued had any
considerations of personal glory, or any unreflecting disregard of the
safety of the province, or of the honor of the army committed to my
charge, induced me to pursue those offensive operations by land,
independent of the fleet, which it would appear by your Lordship’s
despatch were expected of me. Such operations, my Lord, have been
attempted before, and on the same ground. The history of our
country records their failure; and had they been undertaken again
with double the force placed under my command, they would have
issued in the discomfiture of his Majesty’s arms, and in a defeat not
more disastrous than inevitable.”
The Duke of Wellington was not so severe as other critics, and
hesitated to say that Prevost was wrong; “though of this I am
certain, he must equally have returned ... after the fleet was beaten;
and I am inclined to think he was right. I have told the ministers
repeatedly that a naval superiority on the Lakes is a sine qua non of
success in war on the frontier of Canada, even if our object should
be wholly defensive.”[183] Yet the Duke in conversation seemed to
think that his army in Canada was also at fault. “He had sent them
some of his best troops from Bordeaux,” he said five-and-twenty
years afterward,[184] “but they did not turn out quite right; they
wanted this iron fist to command them.”
Meanwhile Major-General Izard, by Armstrong’s order, marched
his four thousand men as far as possible from the points of attack.
Starting from Champlain, August 29, the army reached Sackett’s
Harbor September 17, having marched about two hundred and
eighty miles in twenty days. At Sackett’s Harbor Izard found no
orders from the government, for the government at that time had
ceased to perform its functions; but he received an earnest appeal
from General Brown to succor Fort Erie. “I will not conceal from
you,” wrote Brown, September 10,[185] “that I consider the fate of
this army very doubtful unless speedy relief is afforded.” Izard, who
had no means of testing the correctness of this opinion, decided to
follow Brown’s wishes, and made, September 17, the necessary
preparations. Violent storms prevented Chauncey from embarking
the troops until September 21; but September 27 the troops reached
Batavia, and Izard met Brown by appointment. The army had then
been a month in movement. The distance was more than four
hundred miles, and no energy could have shortened the time so
much as to have affected the result of the campaign. At one end of
the line Sir George Prevost retreated from Plattsburg September 12;
at the other end, Lieutenant-General Drummond retreated from Fort
Erie September 21; and Izard’s force, constituting the largest body
of regular troops in the field, had been placed where it could
possibly affect neither result.
Izard was a friend of Monroe, and was therefore an object of
Armstrong’s merciless criticism.[186] Brown was a favorite of
Armstrong, and shared his prejudices. The position of Izard at
Buffalo was calculated to excite jealousy. He had implicitly obeyed
the wishes of Armstrong and Brown; in doing so, he had sacrificed
himself,—yielding to Macomb the credit of repulsing Prevost, and to
Brown, who did not wait for his arrival, the credit of repulsing
Drummond. As far as could be seen, Izard had acted with loyalty
toward both Armstrong and Brown; yet both distrusted him. Brown
commonly inclined toward severity, and was the more sensitive
because Izard, as the senior officer, necessarily took command.
Until that moment Izard had enjoyed no chance of showing his
abilities in the field, but at Niagara he saw before him a great
opportunity. Drummond lay at Chippawa, with an army reduced by
battle and sickness to about twenty-five hundred men. Izard
commanded fifty-five hundred regular troops and eight hundred
militia.[187] He had time to capture or destroy Drummond’s entire
force before the winter should set in, and to gather the results of
Brown’s desperate fighting. Brown was eager for the attack, and
Izard assented. October 13 the army moved on Chippawa, and
stopped. October 16, Izard wrote to the War Department,[188]—
“I have just learned by express from Sackett’s Harbor that
Commodore Chauncey with the whole of his fleet has retired into port,
and is throwing up batteries for its protection. This defeats all the
objects of the operations by land in this quarter. I may turn Chippawa,
and should General Drummond not retire, may succeed in giving him
a good deal of trouble; but if he falls back on Fort George or
Burlington Heights, every step I take in pursuit exposes me to be cut
off by the large reinforcements it is in the power of the enemy to
throw in twenty-four hours upon my flank or rear.”
In this state of mind, notwithstanding a successful skirmish,
October 19, between Bissell’s brigade and a strong detachment of
the enemy, Izard made a decision which ruined his military
reputation and destroyed his usefulness to the service. He reported
to the Department, October 23,[189]—
“On the 21st, finding that he [Drummond] still continued within his
works, which he had been assiduously engaged in strengthening from
the moment of our first appearance, the weather beginning to be
severe, and a great quantity of our officers and men suffering from
their continued fatigues and exposure, at twelve at noon I broke up
my encampment, and marched to this ground [opposite Black Rock]
in order to prepare winter quarters for the troops.”
Nothing remained but to break up the army. Brown was sent at
his own request to Sackett’s Harbor, where the next fighting was
expected. A division of the army went with him. The remainder were
placed in winter quarters near Buffalo. Fort Erie was abandoned and
blown up, November 5, and the frontier at Niagara relapsed into
repose.
Izard felt the mortification of his failure. His feelings were those of
a generous character, and his tone toward Brown contrasted to his
advantage both in candor and in temper with Brown’s language
toward him; but great energy generally implied great faults, and
Brown’s faults were better suited than Izard’s virtues for the work of
an American general at Niagara. Greatly to Izard’s credit, he not only
saw his own inferiority, but advised the government of it. He wrote
to the Secretary of War, November 20,[190]—
“The success of the next campaign on this frontier will in a great
measure depend on concert and good understanding among the
superior officers.... General Brown is certainly a brave, intelligent, and
active officer. Where a portion of the forces is composed of irregular
troops, I have no hesitation in acknowledging my conviction of his
being better qualified than I to make them useful in the public
service.”
“When the object of the descent which you may make on the coast
is to take possession of any naval or military stores, you will not delay
the destruction of them in preference to the taking them away, if
there is reasonable ground of apprehension that the enemy is
advancing with superior force to effect their recovery. If in any
descent you shall be enabled to take such a position as to threaten
the inhabitants with the destruction of their property, you are hereby
authorized to levy upon them contributions in return for your
forbearance; but you will not by this understand that the magazines
belonging to the government, or their harbors, or their shipping, are
to be included in such an arrangement. These, together with their
contents, are in all cases to be taken away or destroyed.”
This singular note was carried first to the President, who, having
opened and read it, immediately rode to headquarters. Monroe,
Jones, and Rush followed. Armstrong and Campbell arrived last.
Before Armstrong appeared, a scout arrived at ten o’clock with
information that the British army had broken up its camp at daylight,
and was probably more than half way to Bladensburg.[225]
Winder’s persistence in remaining at the navy-yard was explained
as due to the idea that the enemy might move toward the Potomac,
seize Fort Washington or Warburton, secure the passage of his
ships, and approach the city by the river.[226] The general never
explained how his presence at the navy-yard was to prevent such a
movement if it was made.
The whole eastern side of Washington was covered by a broad
estuary called the Eastern Branch of the Potomac, bridged only at
two points, and impassable, even by pontoons, without ample
warning. From the Potomac River to Bladensburg, a distance of
about seven miles, the city was effectually protected. Bladensburg
made the point of a right angle. There the Baltimore road entered
the city as by a pass; for beyond, to the west, no general would
venture to enter, leaving an enemy at Bladensburg in his rear. Roads
were wanting, and the country was difficult. Through Bladensburg
the attacking army must come; to Bladensburg Winder must go,
unless he meant to retreat to Georgetown, or to re-cross the Eastern
Branch in the enemy’s rear. Monroe notified Serurier Monday evening
that the battle would be fought at Bladensburg. Secretary Jones
wrote to Commodore Rodgers, Tuesday morning, that the British
would probably “advance to-day toward Bladensburg.”[227] Every
one looked instinctively to that spot[228], yet Winder to the last
instant persisted in watching the navy-yard bridge, using the hours
of Wednesday morning to post Barney’s sailors with twenty-four-
pound guns to cover an approach[229] where no enemy could cross.
MAP OF THE
BATTLE
OF
BLADENSBURG
STRUTHERS & CO., ENGR’S, N. Y.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookmass.com