Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

C Recipes - A Problem-Solution

Approach 1st Edition Shirish Chavan


Visit to download the full and correct content document:
https://textbookfull.com/product/c-recipes-a-problem-solution-approach-1st-edition-shi
rish-chavan/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

Android Recipes A Problem-Solution Approach Dave Smith

https://textbookfull.com/product/android-recipes-a-problem-
solution-approach-dave-smith/

Raku Recipes: A Problem-Solution Approach J.J. Merelo

https://textbookfull.com/product/raku-recipes-a-problem-solution-
approach-j-j-merelo/

JavaScript Recipes: A Problem-Solution Approach 1st


Edition Russ Ferguson

https://textbookfull.com/product/javascript-recipes-a-problem-
solution-approach-1st-edition-russ-ferguson/

wxPython Recipes: A Problem - Solution Approach 1st


Edition Mike Driscoll

https://textbookfull.com/product/wxpython-recipes-a-problem-
solution-approach-1st-edition-mike-driscoll/
MATLAB Recipes: a problem-solution approach 2nd Edition
Michael Paluszek

https://textbookfull.com/product/matlab-recipes-a-problem-
solution-approach-2nd-edition-michael-paluszek/

PrestaShop Recipes : A Problem-Solution Approach 1st


Edition Arnaldo Pérez Castaño (Auth.)

https://textbookfull.com/product/prestashop-recipes-a-problem-
solution-approach-1st-edition-arnaldo-perez-castano-auth/

Java EE 7 Recipes A Problem Solution Approach 1st


Edition Juneau Josh

https://textbookfull.com/product/java-ee-7-recipes-a-problem-
solution-approach-1st-edition-juneau-josh/

Java 9 Recipes: A Problem-Solution Approach 3rd Edition


Josh Juneau

https://textbookfull.com/product/java-9-recipes-a-problem-
solution-approach-3rd-edition-josh-juneau/

Spring 5 Recipes: A Problem-Solution Approach 4th


Edition Marten Deinum

https://textbookfull.com/product/spring-5-recipes-a-problem-
solution-approach-4th-edition-marten-deinum/
C Recipes
A Problem-Solution Approach

Shirish Chavan
C Recipes
A Problem-Solution Approach

Shirish Chavan
C Recipes: A Problem-Solution Approach
Shirish Chavan
Sangli, Maharashtra, India
ISBN-13 (pbk): 978-1-4842-2966-8 ISBN-13 (electronic): 978-1-4842-2967-5
DOI 10.1007/978-1-4842-2967-5
Library of Congress Control Number: 2017950166
Copyright © 2017 by Shirish Chavan
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole
or part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical
way, and transmission or information storage and retrieval, electronic adaptation, computer
software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the
date of publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Cover image designed by Freepik
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Celestin Suresh John
Development Editor: Matthew Moodie
Technical Reviewer: Yogesh Sharma
Coordinating Editor: Prachi Mehta
Copy Editor: Kim Wimpsett
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201)
348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress
Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business
Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit
www.apress.com/rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our
Print and eBook Bulk Sales web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this
book is available to readers on GitHub via the book's product page, located at
www.apress.com/978-1-4842-2966-8. For more detailed information, please visit
www.apress.com/source-code.
Printed on acid-free paper
This is dedicated to
Honorable Dr. Patangrao Kadam,
Chancellor at Bharati Vidyapeeth University in Pune.
—Shirish Chavan
Contents at a Glance

About the Author��������������������������������������������������������������������������xxvii


About the Technical Reviewer�������������������������������������������������������xxix
Acknowledgments�������������������������������������������������������������������������xxxi
Introduction���������������������������������������������������������������������������������xxxiii


■Chapter 1: Welcome to C���������������������������������������������������������������� 1

■Chapter 2: Control Statements����������������������������������������������������� 13

■Chapter 3: Functions and Arrays�������������������������������������������������� 39

■Chapter 4: Pointers and Arrays���������������������������������������������������� 75

■Chapter 5: Functions and Structures with Pointers������������������� 119

■Chapter 6: Data Files������������������������������������������������������������������ 149

■Chapter 7: Self-Referential Structures��������������������������������������� 213

■Chapter 8: Stacks and Queues���������������������������������������������������� 253

■Chapter 9: Searching and Sorting���������������������������������������������� 277

■Chapter 10: Cryptographic Systems������������������������������������������� 301

■Chapter 11: Numerical Methods������������������������������������������������� 349

v
■ Contents at a Glance


■Appendix A: Reference Tables���������������������������������������������������� 403

■Appendix B: Library Functions��������������������������������������������������� 415

■Appendix C: C Idioms������������������������������������������������������������������ 421

■Appendix D: Glossary of Terms��������������������������������������������������� 431

Index���������������������������������������������������������������������������������������������� 439

vi
Contents

About the Author��������������������������������������������������������������������������xxvii


About the Technical Reviewer�������������������������������������������������������xxix
Acknowledgments�������������������������������������������������������������������������xxxi
Introduction���������������������������������������������������������������������������������xxxiii


■Chapter 1: Welcome to C���������������������������������������������������������������� 1
Programs, Software, and Operating System������������������������������������������� 2
Machine Language and Assembly Language������������������������������������������ 2
Procedural Languages����������������������������������������������������������������������������� 3
Object-Oriented Languages��������������������������������������������������������������������� 3
Terminology in Computers���������������������������������������������������������������������� 4
Compiled and Interpreted Languages����������������������������������������������������� 4
Compilation��������������������������������������������������������������������������������������������������������������� 5
Interpretation������������������������������������������������������������������������������������������������������������ 5

Your First C Program������������������������������������������������������������������������������� 6


Salient Features of C������������������������������������������������������������������������������� 7
Implicit Type Conversion������������������������������������������������������������������������������������������� 8
Explicit Type Conversion ���������������������������������������������������������������������������������������� 10


■Chapter 2: Control Statements����������������������������������������������������� 13
Selection Statements���������������������������������������������������������������������������� 13
Iteration Statements������������������������������������������������������������������������������ 13
Jump Statements���������������������������������������������������������������������������������� 13

vii
■ Contents

2-1. Sum 1 to N Numbers���������������������������������������������������������������������� 14


Problem������������������������������������������������������������������������������������������������������������������ 14
Solution������������������������������������������������������������������������������������������������������������������ 14
How It Works����������������������������������������������������������������������������������������������������������� 15

2-2. Compute the Factorial of a Number����������������������������������������������� 16


Problem������������������������������������������������������������������������������������������������������������������ 16
Solution������������������������������������������������������������������������������������������������������������������ 16
How It Works����������������������������������������������������������������������������������������������������������� 18

2-3. Generate a Fibonacci Sequence���������������������������������������������������� 18


Problem������������������������������������������������������������������������������������������������������������������ 18
Solution������������������������������������������������������������������������������������������������������������������ 18
How It Works����������������������������������������������������������������������������������������������������������� 21

2-4. Determine Whether a Given Number Is Prime������������������������������� 21


Problem������������������������������������������������������������������������������������������������������������������ 21
Solution������������������������������������������������������������������������������������������������������������������ 21
How It Works����������������������������������������������������������������������������������������������������������� 24

2-5. Compute the Sine Function������������������������������������������������������������ 25


Problem������������������������������������������������������������������������������������������������������������������ 25
Solution������������������������������������������������������������������������������������������������������������������ 25
How It Works����������������������������������������������������������������������������������������������������������� 26

2-6. Compute the Cosine Function�������������������������������������������������������� 27


Problem������������������������������������������������������������������������������������������������������������������ 27
Solution������������������������������������������������������������������������������������������������������������������ 27
How It Works����������������������������������������������������������������������������������������������������������� 29

2-7. Compute the Roots of Quadratic Equation������������������������������������� 29


Problem������������������������������������������������������������������������������������������������������������������ 29
Solution������������������������������������������������������������������������������������������������������������������ 29
How It Works����������������������������������������������������������������������������������������������������������� 31

viii
■ Contents

2-8. Compute the Reverse of an Integer����������������������������������������������� 31


Problem������������������������������������������������������������������������������������������������������������������ 31
Solution������������������������������������������������������������������������������������������������������������������ 31
How It Works����������������������������������������������������������������������������������������������������������� 32

2-9. Print a Geometrical Pattern Using Nested Loops��������������������������� 33


Problem������������������������������������������������������������������������������������������������������������������ 33
Solution������������������������������������������������������������������������������������������������������������������ 33
How It Works����������������������������������������������������������������������������������������������������������� 34

2-10. Generate a Table of Future Value Interest Factors����������������������� 35


Problem������������������������������������������������������������������������������������������������������������������ 35
Solution������������������������������������������������������������������������������������������������������������������ 35
How It Works����������������������������������������������������������������������������������������������������������� 36


■Chapter 3: Functions and Arrays�������������������������������������������������� 39
3-1. Determine the Value of Pi�������������������������������������������������������������� 41
Problem������������������������������������������������������������������������������������������������������������������ 41
Solution������������������������������������������������������������������������������������������������������������������ 41
How It Works����������������������������������������������������������������������������������������������������������� 42

3-2. Pick the Prime Numbers from a List of Numbers�������������������������� 43


Problem������������������������������������������������������������������������������������������������������������������ 43
Solution������������������������������������������������������������������������������������������������������������������ 43
How It Works����������������������������������������������������������������������������������������������������������� 45

3-3. Sum Numbers Using Recursion����������������������������������������������������� 46


Problem������������������������������������������������������������������������������������������������������������������ 46
Solution������������������������������������������������������������������������������������������������������������������ 46
How It Works����������������������������������������������������������������������������������������������������������� 47

3-4. Compute the Fibonacci Sequence Using Recursion���������������������� 49


Problem������������������������������������������������������������������������������������������������������������������ 49
Solution������������������������������������������������������������������������������������������������������������������ 49
How It Works����������������������������������������������������������������������������������������������������������� 50

ix
■ Contents

3-5. Compute the Factorial of a Number Using Recursion�������������������� 51


Problem������������������������������������������������������������������������������������������������������������������ 51
Solution������������������������������������������������������������������������������������������������������������������ 51
How It Works����������������������������������������������������������������������������������������������������������� 52

3-6. Search the Largest Element in an Array of Integers ��������������������� 52


Problem������������������������������������������������������������������������������������������������������������������ 52
Solution������������������������������������������������������������������������������������������������������������������ 52
How It Works����������������������������������������������������������������������������������������������������������� 54

3-7. Solve the Classic Problem of the Towers of Hanoi������������������������� 54


Solution������������������������������������������������������������������������������������������������������������������ 54
How It Works����������������������������������������������������������������������������������������������������������� 56

3-8. Solve the Eight Queens Problem���������������������������������������������������� 57


Problem������������������������������������������������������������������������������������������������������������������ 57
Solution������������������������������������������������������������������������������������������������������������������ 57
How It Works����������������������������������������������������������������������������������������������������������� 60

3-9. Compute Permutations and Combinations of a Given


Set of Objects���������������������������������������������������������������������������������������� 60
Problem������������������������������������������������������������������������������������������������������������������ 60
Solution������������������������������������������������������������������������������������������������������������������ 60
How It Works����������������������������������������������������������������������������������������������������������� 63

3-10. Perform the Summation of Two Matrices ����������������������������������� 63


Problem������������������������������������������������������������������������������������������������������������������ 63
Solution������������������������������������������������������������������������������������������������������������������ 63
How It Works����������������������������������������������������������������������������������������������������������� 66

3-11. Compute the Transpose of a Matrix��������������������������������������������� 67


Problem������������������������������������������������������������������������������������������������������������������ 67
Solution������������������������������������������������������������������������������������������������������������������ 67
How It Works����������������������������������������������������������������������������������������������������������� 69

x
■ Contents

3-12. Compute the Product of Matrices ����������������������������������������������� 69


Problem������������������������������������������������������������������������������������������������������������������ 69
Solution������������������������������������������������������������������������������������������������������������������ 69
How It Works����������������������������������������������������������������������������������������������������������� 73


■Chapter 4: Pointers and Arrays���������������������������������������������������� 75
4-1. Retrieve Data from an Array with the int Type Data����������������������� 75
Problem������������������������������������������������������������������������������������������������������������������ 75
Solution������������������������������������������������������������������������������������������������������������������ 75
How It Works����������������������������������������������������������������������������������������������������������� 76

4-2. Retrieve Data from an Array Using the Array Name����������������������� 77


Problem������������������������������������������������������������������������������������������������������������������ 77
Solution������������������������������������������������������������������������������������������������������������������ 77
How It Works����������������������������������������������������������������������������������������������������������� 79

4-3. Retrieve Data from an Array with char and double Type Data������� 80
Problem������������������������������������������������������������������������������������������������������������������ 80
Solution������������������������������������������������������������������������������������������������������������������ 80
How It Works����������������������������������������������������������������������������������������������������������� 82

4-4. Access the Out-of-Bounds Array Elements������������������������������������ 82


Problem������������������������������������������������������������������������������������������������������������������ 82
Solution������������������������������������������������������������������������������������������������������������������ 82
How It Works����������������������������������������������������������������������������������������������������������� 83

4-5. Store Strings���������������������������������������������������������������������������������� 84


Problem������������������������������������������������������������������������������������������������������������������ 84
Solution������������������������������������������������������������������������������������������������������������������ 84
How It Works����������������������������������������������������������������������������������������������������������� 86

4-6. Store Strings Without Initialization������������������������������������������������ 87


Problem������������������������������������������������������������������������������������������������������������������ 87
Solution������������������������������������������������������������������������������������������������������������������ 87
How It Works����������������������������������������������������������������������������������������������������������� 89

xi
■ Contents

4-7. Store Strings in an Interactive Session������������������������������������������ 89


Problem������������������������������������������������������������������������������������������������������������������ 89
Solution������������������������������������������������������������������������������������������������������������������ 89
How It Works����������������������������������������������������������������������������������������������������������� 90

4-8. Retrieve the Addresses of Elements in a


Two-Dimensional Array������������������������������������������������������������������������� 91
Problem������������������������������������������������������������������������������������������������������������������ 91
Solution������������������������������������������������������������������������������������������������������������������ 91
How It Works����������������������������������������������������������������������������������������������������������� 92

4-9. Retrieve the Base Addresses of Rows in a


Two-Dimensional Array������������������������������������������������������������������������� 93
Problem������������������������������������������������������������������������������������������������������������������ 93
Solution������������������������������������������������������������������������������������������������������������������ 93
How It Works����������������������������������������������������������������������������������������������������������� 94

4-10. Retrieve Data from a Two-Dimensional Array������������������������������ 95


Problem������������������������������������������������������������������������������������������������������������������ 95
Solution������������������������������������������������������������������������������������������������������������������ 95
How It Works����������������������������������������������������������������������������������������������������������� 96

4-11. Retrieve Data from a Two-Dimensional Array Using an


Array Name������������������������������������������������������������������������������������������� 97
Problem������������������������������������������������������������������������������������������������������������������ 97
Solution������������������������������������������������������������������������������������������������������������������ 97
How It Works����������������������������������������������������������������������������������������������������������� 99

4-12. Retrieve Data from an Array Using an Array of Pointers������������ 100


Problem���������������������������������������������������������������������������������������������������������������� 100
Solution���������������������������������������������������������������������������������������������������������������� 100
How It Works��������������������������������������������������������������������������������������������������������� 101

xii
■ Contents

4-13. Swap Strings Physically������������������������������������������������������������� 102


Problem���������������������������������������������������������������������������������������������������������������� 102
Solution���������������������������������������������������������������������������������������������������������������� 102
How It Works��������������������������������������������������������������������������������������������������������� 103

4-14. Swap Strings Logically�������������������������������������������������������������� 104


Problem���������������������������������������������������������������������������������������������������������������� 104
Solution���������������������������������������������������������������������������������������������������������������� 104
How It Works��������������������������������������������������������������������������������������������������������� 106

4-15. Store Strings Interactively��������������������������������������������������������� 108


Problem���������������������������������������������������������������������������������������������������������������� 108
Solution���������������������������������������������������������������������������������������������������������������� 108
How It Works��������������������������������������������������������������������������������������������������������� 109

4-16. Pass Arguments to a Program from the Command Line������������ 110


Problem���������������������������������������������������������������������������������������������������������������� 110
Solution���������������������������������������������������������������������������������������������������������������� 110
How It Works��������������������������������������������������������������������������������������������������������� 111

4-17. Retrieve Stored Strings Using a Pointer to a Pointer����������������� 113


Problem���������������������������������������������������������������������������������������������������������������� 113
Solution���������������������������������������������������������������������������������������������������������������� 114
How It Works��������������������������������������������������������������������������������������������������������� 115


■Chapter 5: Functions and Structures with Pointers������������������� 119
5-1. Pass Arguments by Reference to a Function������������������������������� 119
Problem���������������������������������������������������������������������������������������������������������������� 119
Solution���������������������������������������������������������������������������������������������������������������� 119
How It Works��������������������������������������������������������������������������������������������������������� 121

xiii
■ Contents

5-2. Display Data Stored in Nested Structures������������������������������������ 122


Problem���������������������������������������������������������������������������������������������������������������� 122
Solution���������������������������������������������������������������������������������������������������������������� 123
How It Works��������������������������������������������������������������������������������������������������������� 125

5-3. Build a Structure Using a Function���������������������������������������������� 129


Problem���������������������������������������������������������������������������������������������������������������� 129
Solution���������������������������������������������������������������������������������������������������������������� 129
How It Works��������������������������������������������������������������������������������������������������������� 131

5-4. Modify the Data in a Structure by Passing It to a Function���������� 131


Problem���������������������������������������������������������������������������������������������������������������� 131
Solution���������������������������������������������������������������������������������������������������������������� 131
How It Works��������������������������������������������������������������������������������������������������������� 133

5-5. Modify the Data in a Structure by Passing a


Pointer-to-Structure to a Function������������������������������������������������������ 134
Problem���������������������������������������������������������������������������������������������������������������� 134
Solution���������������������������������������������������������������������������������������������������������������� 134
How It Works��������������������������������������������������������������������������������������������������������� 136

5-6. Store and Retrieve Data Using an Array of Structures����������������� 136


Problem���������������������������������������������������������������������������������������������������������������� 136
Solution���������������������������������������������������������������������������������������������������������������� 136
How It Works��������������������������������������������������������������������������������������������������������� 138

5-7. Store and Retrieve Data Using an Array of Structures in


Interactive Mode��������������������������������������������������������������������������������� 139
Problem���������������������������������������������������������������������������������������������������������������� 139
Solution���������������������������������������������������������������������������������������������������������������� 139
How It Works��������������������������������������������������������������������������������������������������������� 142

xiv
■ Contents

5-8. Invoke a Function Using a Pointer-to-Function���������������������������� 142


Problem���������������������������������������������������������������������������������������������������������������� 142
Solution���������������������������������������������������������������������������������������������������������������� 142
How It Works��������������������������������������������������������������������������������������������������������� 144

5-9. Implement a Text-Based Menu System��������������������������������������� 146


Problem���������������������������������������������������������������������������������������������������������������� 146
Solution���������������������������������������������������������������������������������������������������������������� 146
How It Works��������������������������������������������������������������������������������������������������������� 148


■Chapter 6: Data Files������������������������������������������������������������������ 149
6-1. Read a Text File Character by Character�������������������������������������� 149
Problem���������������������������������������������������������������������������������������������������������������� 149
Solution���������������������������������������������������������������������������������������������������������������� 149
How It Works��������������������������������������������������������������������������������������������������������� 150

6-2. Handle Errors When File Opening Fails���������������������������������������� 154


Problem���������������������������������������������������������������������������������������������������������������� 154
Solution���������������������������������������������������������������������������������������������������������������� 154
How It Works��������������������������������������������������������������������������������������������������������� 156

6-3. Write to a Text File in Batch Mode����������������������������������������������� 157


Problem���������������������������������������������������������������������������������������������������������������� 157
Solution���������������������������������������������������������������������������������������������������������������� 157
How It Works��������������������������������������������������������������������������������������������������������� 158

6-4. Write to a Text File in Interactive Mode���������������������������������������� 160


Problem���������������������������������������������������������������������������������������������������������������� 160
Solution���������������������������������������������������������������������������������������������������������������� 160
How It Works��������������������������������������������������������������������������������������������������������� 161

xv
■ Contents

6-5. Read a Text File String by String�������������������������������������������������� 163


Problem���������������������������������������������������������������������������������������������������������������� 163
Solution���������������������������������������������������������������������������������������������������������������� 163
How It Works��������������������������������������������������������������������������������������������������������� 164

6-6. Write to a Text File Character by Character���������������������������������� 166


Problem���������������������������������������������������������������������������������������������������������������� 166
Solution���������������������������������������������������������������������������������������������������������������� 166
How It Works��������������������������������������������������������������������������������������������������������� 167

6-7. Write Integers to a Text File��������������������������������������������������������� 168


Problem���������������������������������������������������������������������������������������������������������������� 168
Solution���������������������������������������������������������������������������������������������������������������� 168
How It Works��������������������������������������������������������������������������������������������������������� 170

6-8. Write Structures to a Text File������������������������������������������������������ 171


Problem���������������������������������������������������������������������������������������������������������������� 171
Solution���������������������������������������������������������������������������������������������������������������� 171
How It Works��������������������������������������������������������������������������������������������������������� 173

6-9. Read Integers Stored in a Text File���������������������������������������������� 174


Problem���������������������������������������������������������������������������������������������������������������� 174
Solution���������������������������������������������������������������������������������������������������������������� 174
How It Works��������������������������������������������������������������������������������������������������������� 175

6-10. Read Structures Stored in a Text File����������������������������������������� 176


Problem���������������������������������������������������������������������������������������������������������������� 176
Solution���������������������������������������������������������������������������������������������������������������� 176
How It Works��������������������������������������������������������������������������������������������������������� 178

6-11. Write Integers to a Binary File��������������������������������������������������� 179


Problem���������������������������������������������������������������������������������������������������������������� 179
Solution���������������������������������������������������������������������������������������������������������������� 179
How It Works��������������������������������������������������������������������������������������������������������� 180

xvi
■ Contents

6-12. Write Structures to a Binary File������������������������������������������������ 181


Problem���������������������������������������������������������������������������������������������������������������� 181
Solution���������������������������������������������������������������������������������������������������������������� 181
How It Works��������������������������������������������������������������������������������������������������������� 183

6-13. Read Integers Written to a Binary File��������������������������������������� 184


Problem���������������������������������������������������������������������������������������������������������������� 184
Solution���������������������������������������������������������������������������������������������������������������� 184
How It Works��������������������������������������������������������������������������������������������������������� 185

6-14. Read Structures Written to a Binary File������������������������������������ 186


Problem���������������������������������������������������������������������������������������������������������������� 186
Solution���������������������������������������������������������������������������������������������������������������� 186
How It Works��������������������������������������������������������������������������������������������������������� 188

6-15. Rename a File���������������������������������������������������������������������������� 189


Problem���������������������������������������������������������������������������������������������������������������� 189
Solution���������������������������������������������������������������������������������������������������������������� 189
How It Works��������������������������������������������������������������������������������������������������������� 190

6-16. Delete a File������������������������������������������������������������������������������� 190


Problem���������������������������������������������������������������������������������������������������������������� 190
Solution���������������������������������������������������������������������������������������������������������������� 190
How It Works��������������������������������������������������������������������������������������������������������� 191

6-17. Copy a Text File�������������������������������������������������������������������������� 191


Problem���������������������������������������������������������������������������������������������������������������� 191
Solution���������������������������������������������������������������������������������������������������������������� 191
How It Works��������������������������������������������������������������������������������������������������������� 193

6-18. Copy a Binary File���������������������������������������������������������������������� 194


Problem���������������������������������������������������������������������������������������������������������������� 194
Solution���������������������������������������������������������������������������������������������������������������� 194
How It Works��������������������������������������������������������������������������������������������������������� 196

xvii
■ Contents

6-19. Write to a File and Then Read from That File����������������������������� 197
Problem���������������������������������������������������������������������������������������������������������������� 197
Solution���������������������������������������������������������������������������������������������������������������� 197
How It Works��������������������������������������������������������������������������������������������������������� 198

6-20. Position a Text File to a Desired Character�������������������������������� 199


Problem���������������������������������������������������������������������������������������������������������������� 199
Solution���������������������������������������������������������������������������������������������������������������� 199
How It Works��������������������������������������������������������������������������������������������������������� 201

6-21. Read from the Device File Keyboard������������������������������������������ 206


Problem���������������������������������������������������������������������������������������������������������������� 206
Solution���������������������������������������������������������������������������������������������������������������� 206
How It Works��������������������������������������������������������������������������������������������������������� 207

6-22. Write Text to the Device File Monitor����������������������������������������� 209


Problem���������������������������������������������������������������������������������������������������������������� 209
Solution���������������������������������������������������������������������������������������������������������������� 209
How It Works��������������������������������������������������������������������������������������������������������� 210

6-23. Read Text from the Device File Keyboard and


Write It to the Device File Monitor������������������������������������������������������� 211
Problem���������������������������������������������������������������������������������������������������������������� 211
Solution���������������������������������������������������������������������������������������������������������������� 211
How It Works��������������������������������������������������������������������������������������������������������� 212


■Chapter 7: Self-Referential Structures��������������������������������������� 213
7-1. Generate Lists of Numbers in an Interactive Manner������������������ 213
Problem���������������������������������������������������������������������������������������������������������������� 213
Solution���������������������������������������������������������������������������������������������������������������� 214
How It Works��������������������������������������������������������������������������������������������������������� 215

xviii
■ Contents

7-2. Create a Linked List Using Anonymous Variables������������������������ 216


Problem���������������������������������������������������������������������������������������������������������������� 216
Solution���������������������������������������������������������������������������������������������������������������� 216
How It Works��������������������������������������������������������������������������������������������������������� 217

7-3. Delete a Component from a Linked List��������������������������������������� 220


Problem���������������������������������������������������������������������������������������������������������������� 220
Solution���������������������������������������������������������������������������������������������������������������� 220
How It Works��������������������������������������������������������������������������������������������������������� 222

7-4. Insert a Component into a Linked List����������������������������������������� 225


Problem���������������������������������������������������������������������������������������������������������������� 225
Solution���������������������������������������������������������������������������������������������������������������� 225
How It Works��������������������������������������������������������������������������������������������������������� 227

7-5. Create a Linked List in an Interactive Session����������������������������� 232


Problem���������������������������������������������������������������������������������������������������������������� 232
Solution���������������������������������������������������������������������������������������������������������������� 233
How It Works��������������������������������������������������������������������������������������������������������� 235

7-6. Process a Linear Linked List�������������������������������������������������������� 238


Problem���������������������������������������������������������������������������������������������������������������� 238
Solution���������������������������������������������������������������������������������������������������������������� 238
How It Works��������������������������������������������������������������������������������������������������������� 247

7-7. Create a Linear Linked List with Forward and


Backward Traversing��������������������������������������������������������������������������� 249
Problem���������������������������������������������������������������������������������������������������������������� 249
Solution���������������������������������������������������������������������������������������������������������������� 249
How It Works��������������������������������������������������������������������������������������������������������� 251

xix
■ Contents


■Chapter 8: Stacks and Queues���������������������������������������������������� 253
8-1. Implement a Stack as an Array���������������������������������������������������� 254
Problem���������������������������������������������������������������������������������������������������������������� 254
Solution���������������������������������������������������������������������������������������������������������������� 254
How It Works��������������������������������������������������������������������������������������������������������� 258
8-2. Implement a Stack as a Linked List��������������������������������������������� 258
Problem���������������������������������������������������������������������������������������������������������������� 258
Solution���������������������������������������������������������������������������������������������������������������� 258
How It Works��������������������������������������������������������������������������������������������������������� 263

8-3. Convert an Infix Expression to a Postfix Expression�������������������� 263


Problem���������������������������������������������������������������������������������������������������������������� 263
Solution���������������������������������������������������������������������������������������������������������������� 263
How It Works��������������������������������������������������������������������������������������������������������� 266

8-4. Convert an Infix Expression to a Prefix Expression���������������������� 267


Problem���������������������������������������������������������������������������������������������������������������� 267
Solution���������������������������������������������������������������������������������������������������������������� 267
How It Works��������������������������������������������������������������������������������������������������������� 270

8-5. Implement a Circular Queue as an Array������������������������������������� 271


Problem���������������������������������������������������������������������������������������������������������������� 271
Solution���������������������������������������������������������������������������������������������������������������� 271
How It Works��������������������������������������������������������������������������������������������������������� 275


■Chapter 9: Searching and Sorting���������������������������������������������� 277
9-1. Find a Data Element Using a Linear Search��������������������������������� 278
Problem���������������������������������������������������������������������������������������������������������������� 278
Solution���������������������������������������������������������������������������������������������������������������� 278
How It Works��������������������������������������������������������������������������������������������������������� 280

xx
■ Contents

9-2. Find a Data Element Using a Binary Search�������������������������������� 280


Problem���������������������������������������������������������������������������������������������������������������� 280
Solution���������������������������������������������������������������������������������������������������������������� 280
How It Works��������������������������������������������������������������������������������������������������������� 282

9-3. Sort a Given List of Numbers Using a Bubble Sort����������������������� 283


Problem���������������������������������������������������������������������������������������������������������������� 283
Solution���������������������������������������������������������������������������������������������������������������� 283
How It Works��������������������������������������������������������������������������������������������������������� 285

9-4. Sort a Given List of Numbers Using an Insertion Sort������������������ 286


Problem���������������������������������������������������������������������������������������������������������������� 286
Solution���������������������������������������������������������������������������������������������������������������� 286
How It Works��������������������������������������������������������������������������������������������������������� 287

9-5. Sort a Given List of Numbers Using a Selection Sort������������������� 288


Problem���������������������������������������������������������������������������������������������������������������� 288
Solution���������������������������������������������������������������������������������������������������������������� 288
How It Works��������������������������������������������������������������������������������������������������������� 290

9-6. Sort a Given List of Numbers Using a Merge Sort����������������������� 291


Problem���������������������������������������������������������������������������������������������������������������� 291
Solution���������������������������������������������������������������������������������������������������������������� 291
How It Works��������������������������������������������������������������������������������������������������������� 293

9-7. Sort a Given List of Numbers Using a Shell Sort�������������������������� 294


Problem���������������������������������������������������������������������������������������������������������������� 294
Solution���������������������������������������������������������������������������������������������������������������� 294
How It Works��������������������������������������������������������������������������������������������������������� 296

9-8. Sort a Given List of Numbers Using a Quick Sort������������������������� 296


Problem���������������������������������������������������������������������������������������������������������������� 296
Solution���������������������������������������������������������������������������������������������������������������� 296
How It Works��������������������������������������������������������������������������������������������������������� 298

xxi
■ Contents


■Chapter 10: Cryptographic Systems������������������������������������������� 301
10-1. Use the Reverse Cipher Method������������������������������������������������ 303
Problem���������������������������������������������������������������������������������������������������������������� 303
Solution���������������������������������������������������������������������������������������������������������������� 304
How It Works��������������������������������������������������������������������������������������������������������� 306
10-2. Use the Caesar Cipher Method�������������������������������������������������� 307
Problem���������������������������������������������������������������������������������������������������������������� 307
Solution���������������������������������������������������������������������������������������������������������������� 307
How It Works��������������������������������������������������������������������������������������������������������� 310

10-3. Use the Transposition Cipher Method���������������������������������������� 311


Problem���������������������������������������������������������������������������������������������������������������� 311
Solution���������������������������������������������������������������������������������������������������������������� 311
How It Works��������������������������������������������������������������������������������������������������������� 314

10-4. Use the Multiplicative Cipher Method���������������������������������������� 315


Problem���������������������������������������������������������������������������������������������������������������� 315
Solution���������������������������������������������������������������������������������������������������������������� 316
How It Works��������������������������������������������������������������������������������������������������������� 318

10-5. Use the Affine Cipher Method���������������������������������������������������� 320


Problem���������������������������������������������������������������������������������������������������������������� 320
Solution���������������������������������������������������������������������������������������������������������������� 321
How It Works��������������������������������������������������������������������������������������������������������� 323

10-6. Use the Simple Substitution Cipher Method������������������������������ 324


Problem���������������������������������������������������������������������������������������������������������������� 324
Solution���������������������������������������������������������������������������������������������������������������� 325
How It Works��������������������������������������������������������������������������������������������������������� 328
10-7. Use the Vigenère Cipher Method����������������������������������������������� 330
Problem���������������������������������������������������������������������������������������������������������������� 330
Solution���������������������������������������������������������������������������������������������������������������� 330
How It Works��������������������������������������������������������������������������������������������������������� 334

xxii
■ Contents

10-8. Use the One-Time Pad Cipher Method��������������������������������������� 335


Problem���������������������������������������������������������������������������������������������������������������� 335
Solution���������������������������������������������������������������������������������������������������������������� 336
How It Works��������������������������������������������������������������������������������������������������������� 339

10-9. Use the RSA Cipher Method������������������������������������������������������� 340


Problem���������������������������������������������������������������������������������������������������������������� 340
Solution���������������������������������������������������������������������������������������������������������������� 341
How It Works��������������������������������������������������������������������������������������������������������� 345


■Chapter 11: Numerical Methods������������������������������������������������� 349
11-1. To Find the Roots of an Equation Using the Bisection Method��� 350
Problem���������������������������������������������������������������������������������������������������������������� 350
Solution���������������������������������������������������������������������������������������������������������������� 350
How It Works��������������������������������������������������������������������������������������������������������� 352

11-2. To Find the Roots of an Equation Using the Regula


Falsi Method���������������������������������������������������������������������������������������� 353
Problem���������������������������������������������������������������������������������������������������������������� 353
Solution���������������������������������������������������������������������������������������������������������������� 354
How It Works��������������������������������������������������������������������������������������������������������� 356

11-3. To Find the Roots of an Equation Using Muller’s Method����������� 357


Problem���������������������������������������������������������������������������������������������������������������� 357
Solution���������������������������������������������������������������������������������������������������������������� 357
How It Works��������������������������������������������������������������������������������������������������������� 359

11-4. To Find the Roots of an Equation Using the Newton Raphson


Method������������������������������������������������������������������������������������������������ 360
Problem���������������������������������������������������������������������������������������������������������������� 360
Solution���������������������������������������������������������������������������������������������������������������� 360
How It Works��������������������������������������������������������������������������������������������������������� 362

xxiii
■ Contents

11-5. To Construct the New Data Points Using Newton’s Forward


Method of Interpolation����������������������������������������������������������������������� 363
Problem���������������������������������������������������������������������������������������������������������������� 363
Solution���������������������������������������������������������������������������������������������������������������� 363
How It Works��������������������������������������������������������������������������������������������������������� 365

11-6. To Construct the New Data Points Using Newton’s Backward


Method of Interpolation����������������������������������������������������������������������� 366
Problem���������������������������������������������������������������������������������������������������������������� 366
Solution���������������������������������������������������������������������������������������������������������������� 366
How It Works��������������������������������������������������������������������������������������������������������� 368

11-7. To Construct the New Data Points Using Gauss’s Forward


Method of Interpolation����������������������������������������������������������������������� 369
Problem���������������������������������������������������������������������������������������������������������������� 369
Solution���������������������������������������������������������������������������������������������������������������� 369
How It Works��������������������������������������������������������������������������������������������������������� 371

11-8. To Construct the New Data Points Using Gauss’s Backward


Method of Interpolation����������������������������������������������������������������������� 372
Problem���������������������������������������������������������������������������������������������������������������� 372
Solution���������������������������������������������������������������������������������������������������������������� 373
How It Works��������������������������������������������������������������������������������������������������������� 375
11-9. To Construct the New Data Points Using Stirling’s Method of
Interpolation���������������������������������������������������������������������������������������� 376
Problem���������������������������������������������������������������������������������������������������������������� 376
Solution���������������������������������������������������������������������������������������������������������������� 376
How It Works��������������������������������������������������������������������������������������������������������� 378

11-10. To Construct the New Data Points Using Bessel’s Method of


Interpolation���������������������������������������������������������������������������������������� 379
Problem���������������������������������������������������������������������������������������������������������������� 379
Solution���������������������������������������������������������������������������������������������������������������� 379
How It Works��������������������������������������������������������������������������������������������������������� 381

xxiv
■ Contents

11-11. To Construct the New Data Points Using Laplace Everett’s


Method of Interpolation����������������������������������������������������������������������� 382
Problem���������������������������������������������������������������������������������������������������������������� 382
Solution���������������������������������������������������������������������������������������������������������������� 382
How It Works��������������������������������������������������������������������������������������������������������� 384

11-12. To Construct the New Data Points Using Lagrange’s


Method of Interpolation����������������������������������������������������������������������� 385
Problem���������������������������������������������������������������������������������������������������������������� 385
Solution���������������������������������������������������������������������������������������������������������������� 386
How It Works��������������������������������������������������������������������������������������������������������� 387

11-13. To Compute the Value of Integration Using Trapezoidal


Method of Numerical Integration�������������������������������������������������������� 388
Problem���������������������������������������������������������������������������������������������������������������� 388
Solution���������������������������������������������������������������������������������������������������������������� 389
How It Works��������������������������������������������������������������������������������������������������������� 390

11-14. To Compute the Value of Integration Using Simpson’s


3/8th Method of Numerical Integration����������������������������������������������� 391
Problem���������������������������������������������������������������������������������������������������������������� 391
Solution���������������������������������������������������������������������������������������������������������������� 391
How It Works��������������������������������������������������������������������������������������������������������� 393
11-15. To Compute the Value of Integration Using Simpson’s
1/3rd Method of Numerical Integration����������������������������������������������� 393
Problem���������������������������������������������������������������������������������������������������������������� 393
Solution���������������������������������������������������������������������������������������������������������������� 394
How It Works��������������������������������������������������������������������������������������������������������� 395

11-16. To Solve a Differential Equation Using Modified


Euler’s Method������������������������������������������������������������������������������������ 396
Problem���������������������������������������������������������������������������������������������������������������� 396
Solution���������������������������������������������������������������������������������������������������������������� 396
How It Works��������������������������������������������������������������������������������������������������������� 398

xxv
■ Contents

11-17. To Solve a Differential Equation Using Runge Kutta Method���� 399


Problem���������������������������������������������������������������������������������������������������������������� 399
Solution���������������������������������������������������������������������������������������������������������������� 399
How It Works��������������������������������������������������������������������������������������������������������� 401


■Appendix A: Reference Tables���������������������������������������������������� 403

■Appendix B: Library Functions��������������������������������������������������� 415
Character Testing and Processing Functions�������������������������������������� 415
String Processing Functions��������������������������������������������������������������� 416
Mathematical Functions���������������������������������������������������������������������� 417
Utility Functions����������������������������������������������������������������������������������� 419

■Appendix C: C Idioms������������������������������������������������������������������ 421

■Appendix D: Glossary of Terms��������������������������������������������������� 431

Index���������������������������������������������������������������������������������������������� 439

xxvi
About the Author

Shirish Chavan is a software developer, teacher, writer,


and orator. He has authored nine books on computers
including books on Java, Visual Basic .NET, and desktop
publishing (DTP). He has 25 years of experience in
various branches of IT. He is mainly interested in
cryptography. He is currently working on a book on
Python programming and also working on a couple
of software projects. He earned his Master of Science
degree in theoretical physics from Shivaji University
in Kolhapur, India, in 1982. He also teaches computer
science and physics at various institutes as a visiting
professor.

xxvii
About the Technical
Reviewer

Yogesh Sharma is currently employed as a senior engineer at Mphasis with almost a


decade of experience in the development and maintenance of small-scale to enterprise-
grade applications. Yogesh earned his bachelor’s degree in information technology from
VSIT Bombay and is currently experimenting with mobility and NLP technologies with
Prof. Yogesh Karunakar. Prior to editing this book, Yogesh reviewed Beginning Laravel by
Sanjib Sinha (Apress, 2017). He would like to acknowledge his joy for all the support and
motivation in his endeavors.

xxix
Acknowledgments

Thanks to everyone working at Apress who made this book possible. I am particularly
thankful to Mr. Celestin Suresh John, acquisitions editor, and Ms. Prachi Mehta,
coordinating editor, for their patience and guidance.
A good number of my techie friends helped me on technical matters in this book.
Notable among them are Mr. Ajay Dhande, CEO of Cryptex Technologies in Nagpur
(www.cryptextechnologies.com); Prof. Shivajirao Salunkhe and Prof. Manisha
Salunkhe of Harsh Computer Institute in Satara; Dr. Vilas Pharande, Principal at
Arvind Gavali College of Engineering in Satara (www.agce.sets.edu.in); Prof. Sachin
Pratapure and Prof. Vishal Khade at Kalasagar Academy (www.kalasagaracademy.in)
in Wai, Satara; Prof. Anant Bodas and Prof. Vikas Dhane at Yashoda College of
Engineering in Satara; Prof. Sanjay Adhau of Shrikant Computer Training Center,
Amravati (https://www.sctcamravati.com); Dr. Mir Sadique Ali, Principal, P R M I T
& R, Amravati (mitra.ac.in); and Mr. Nikhil Kumbhar, CEO of Aphron Infotech in Pune
(www.aphroninfotech.in). I am also thankful to Mr. Tushar Soni and Mr. Ajay Sawant,
who run the web site Coding Alpha (www.codingalpha.com), and Mr. Neeraj Mishra,
who runs the web site The Crazy Programmer (www.thecrazyprogrammer.com), for their
valuable help in the making of this book.
Dr. Vijay Bhatkar, eminent computer scientist and father of the Indian
supercomputer PARAM 10000, has been a source of inspiration for me. I am grateful to
him for inspiring me.
Last but not least, I am always thankful to Mr. Jarron & Mr. John Borges and their
active Technical Book Services team in Pune for the prompt supply of books.
Thank you, Sirs and Madams, you all made this book possible. Finally, a note: Pune,
Nagpur, and Satara are cities in Maharashtra, India.

xxxi
Another random document with
no related content on Scribd:
memory of Burns,—every man’s, every boy’s and girl’s head carries
snatches of his songs, and they say them by heart, and, what is
strangest of all, never learned them from a book, but from mouth to
mouth. The wind whispers them, the birds whistle them, the corn,
barley, and bulrushes hoarsely rustle them, nay, the music-boxes at
Geneva are framed and toothed to play them; the hand-organs of the
Savoyards in all cities repeat them, and the chimes of bells ring them
in the spires. They are the property and the solace of mankind.[213]
XXIII
REMARKS

AT THE CELEBRATION OF THE THREE


HUNDREDTH ANNIVERSARY OF THE BIRTH OF
SHAKSPEARE BY THE SATURDAY CLUB AT THE
REVERE HOUSE, BOSTON, 1864

England’s genius filled all measure


Of heart and soul, of strength and pleasure,
Gave to mind its emperor
And life was larger than before;
And centuries brood, nor can attain
The sense and bound of Shakspeare’s brain.
The men who lived with him became
Poets, for the air was fame.

SHAKSPEARE
’Tis not our fault if we have not made this evening’s circle still
richer than it is. We seriously endeavored, besides our brothers and
our seniors, on whom the ordinary lead of literary and social action
falls—and falls because of their ability—to draw out of their
retirements a few rarer lovers of the muse—“seld-seen flamens”—
whom this day seemed to elect and challenge. And it is to us a
painful disappointment that Bryant and Whittier as guests, and our
own Hawthorne,—with the best will to come,—should have found it
impossible at last; and again, that a well-known and honored
compatriot, who first in Boston wrote elegant verse, and on
Shakspeare, and whose American devotion through forty or fifty
years to the affairs of a bank, has not been able to bury the fires of
his genius,—Mr. Charles Sprague,—pleads the infirmities of age as
an absolute bar to his presence with us.
We regret also the absence of our members Sumner and Motley.
We can hardly think of an occasion where so little need be said.
We are all content to let Shakspeare speak for himself. His fame is
settled on the foundations of the moral and intellectual world.
Wherever there are men, and in the degree in which they are civil—
have power of mind, sensibility to beauty, music, the secrets of
passion, and the liquid expression of thought, he has risen to his
place as the first poet of the world.
Genius is the consoler of our mortal condition, and Shakspeare
taught us that the little world of the heart is vaster, deeper and richer
than the spaces of astronomy. What shocks of surprise and
sympathetic power, this battery, which he is, imparts to every fine
mind that is born! We say to the young child in the cradle, ‘Happy,
and defended against Fate! for here is Nature, and here is
Shakspeare, waiting for you!’
’Tis our metre of culture. He is a cultivated man—who can tell us
something new of Shakspeare. All criticism is only a making of rules
out of his beauties. He is as superior to his countrymen, as to all
other countrymen. He fulfilled the famous prophecy of Socrates, that
the poet most excellent in tragedy would be most excellent in
comedy, and more than fulfilled it by making tragedy also a victorious
melody which healed its own wounds. In short, Shakspeare is the
one resource of our life on which no gloom gathers; the fountain of
joy which honors him who tastes it; day without night; pleasure
without repentance; the genius which, in unpoetic ages, keeps
poetry in honor and, in sterile periods, keeps up the credit of the
human mind.
His genius has reacted on himself. Men were so astonished and
occupied by his poems that they have not been able to see his face
and condition, or say, who was his father and his brethren; or what
life he led; and at the short distance of three hundred years he is
mythical, like Orpheus and Homer, and we have already seen the
most fantastic theories plausibly urged, as that Raleigh and Bacon
were the authors of the plays.
Yet we pause expectant before the genius of Shakspeare—as if
his biography were not yet written; until the problem of the whole
English race is solved.
I see, among the lovers of this catholic genius, here present, a
few, whose deeper knowledge invites me to hazard an article of my
literary creed; that Shakspeare, by his transcendant reach of
thought, so unites the extremes, that, whilst he has kept the theatre
now for three centuries, and, like a street-bible, furnishes sayings to
the market, courts of law, the senate, and common discourse,—he is
yet to all wise men the companion of the closet. The student finds
the solitariest place not solitary enough to read him; and so
searching is his penetration, and such the charm of his speech, that
he still agitates the heart in age as in youth, and will, until it ceases
to beat.
Young men of a contemplative turn carry his sonnets in the pocket.
With that book, the shade of any tree, a room in any inn, becomes a
chapel or oratory in which to sit out their happiest hours. Later they
find riper and manlier lessons in the plays.
And secondly, he is the most robust and potent thinker that ever
was. I find that it was not history, courts and affairs that gave him
lessons, but he that gave grandeur and prestige to them. There
never was a writer who, seeming to draw every hint from outward
history, the life of cities and courts, owed them so little. You shall
never find in this world the barons or kings he depicted. ’Tis fine for
Englishmen to say, they only know history by Shakspeare. The
palaces they compass earth and sea to enter, the magnificence and
personages of royal and imperial abodes, are shabby imitations and
caricatures of his,—clumsy pupils of his instruction. There are no
Warwicks, no Talbots, no Bolingbrokes, no Cardinals, no Harry Fifth,
in real Europe, like his. The loyalty and royalty he drew were all his
own. The real Elizabeths, Jameses and Louises were painted sticks
before this magician.
The unaffected joy of the comedy,—he lives in a gale,—contrasted
with the grandeur of the tragedy, where he stoops to no contrivance,
no pulpiting, but flies an eagle at the heart of the problem; where his
speech is a Delphi,—the great Nemesis that he is and utters. What a
great heart of equity is he! How good and sound and inviolable his
innocency, that is never to seek, and never wrong, but speaks the
pure sense of humanity on each occasion. He dwarfs all writers
without a solitary exception. No egotism. The egotism of men is
immense. It concealed Shakspeare for a century. His mind has a
superiority such that the universities should read lectures on him,
and conquer the unconquerable if they can.
There are periods fruitful of great men; others, barren; or, as the
world is always equal to itself, periods when the heat is latent,—
others when it is given out.
They are like the great wine years,—the vintage of 1847, is it? or
1835?—which are not only noted in the carte of the table d’hôte, but
which, it is said, are always followed by new vivacity in the politics of
Europe. His birth marked a great wine year when wonderful grapes
ripened in the vintage of God, when Shakspeare and Galileo were
born within a few months of each other, and Cervantes was his exact
contemporary, and, in short space before and after, Montaigne,
Bacon, Spenser, Raleigh and Jonson. Yet Shakspeare, not by any
inferiority of theirs, but simply by his colossal proportions, dwarfs the
geniuses of Elizabeth as easily as the wits of Anne, or the poor
slipshod troubadours of King René.
In our ordinary experience of men there are some men so born to
live well that, in whatever company they fall,—high or low,—they fit
well, and lead it! but, being advanced to a higher class, they are just
as much in their element as before, and easily command: and being
again preferred to selecter companions, find no obstacle to ruling
these as they did their earlier mates; I suppose because they have
more humanity than talent, whilst they have quite as much of the last
as any of the company. It would strike you as comic, if I should give
my own customary examples of this elasticity, though striking
enough to me. I could name in this very company—or not going far
out of it—very good types, but in order to be parliamentary, Franklin,
Burns and Walter Scott are examples of the rule; and king of men, by
this grace of God also, is Shakspeare.
The Pilgrims came to Plymouth in 1620. The plays of Shakspeare
were not published until three years later. Had they been published
earlier, our forefathers, or the most poetical among them, might have
stayed at home to read them.
XXIV
HUMBOLDT

AN ABSTRACT OF MR. EMERSON’S REMARKS


MADE AT THE CELEBRATION OF THE
CENTENNIAL ANNIVERSARY OF THE BIRTH OF
ALEXANDER VON HUMBOLDT, SEPTEMBER 14,
1869

“If a life prolonged to an advanced period bring with it


several inconveniences to the individual, there is a
compensation in the delight of being able to compare older
states of knowledge with that which now exists, and to see
great advances in knowledge develop themselves under our
eyes in departments which had long slept in inactivity.”
Humboldt, Letter to Ritter.

HUMBOLDT
Humboldt was one of those wonders of the world, like Aristotle,
like Julius Cæsar, like the Admirable Crichton, who appear from time
to time, as if to show us the possibilities of the human mind, the force
and the range of the faculties,—a universal man, not only possessed
of great particular talents, but they were symmetrical, his parts were
well put together. As we know, a man’s natural powers are often a
sort of committee that slowly, one at a time, give their attention and
action; but Humboldt’s were all united, one electric chain, so that a
university, a whole French Academy, travelled in his shoes. With
great propriety, he named his sketch of the results of science
Cosmos. There is no other such survey or surveyor. The wonderful
Humboldt, with his solid centre and expanded wings, marches like
an army, gathering all things as he goes. How he reaches from
science to science, from law to law, folding away moons and
asteroids and solar systems in the clauses and parentheses of his
encyclopædic paragraphs! There is no book like it; none indicating
such a battalion of powers. You could not put him on any sea or
shore but his instant recollection of every other sea or shore
illuminated this.
He was properly a man of the world; you could not lose him; you
could not detain him; you could not disappoint him, for at any point
on land or sea he found the objects of his researches. When he was
stopped in Spain and could not get away, he turned round and
interpreted their mountain system, explaining the past history of the
continent of Europe. He belonged to that wonderful German nation,
the foremost scholars in all history, who surpass all others in
industry, space and endurance. A German reads a literature whilst
we are reading a book. One of their writers warns his countrymen
that it is not the Battle of Leipsic, but the Leipsic Fair Catalogue,
which raises them above the French. I remember Cuvier tells us of
fossil elephants; that Germany has furnished the greatest number;—
not because there are more elephants in Germany,—oh no; but
because in that empire there is no canton without some well-
informed person capable of making researches and publishing
interesting results. I know that we have been accustomed to think
they were too good scholars, that because they reflect, they never
resolve, that “in a crisis no plan-maker was to be found in the
empire;” but we have lived to see now, for the second time in the
history of Prussia, a statesman of the first class, with a clear head
and an inflexible will.
XXV
WALTER SCOTT

REMARKS AT THE CELEBRATION BY THE


MASSACHUSETTS HISTORICAL SOCIETY OF
THE CENTENNIAL ANNIVERSARY OF HIS BIRTH,
AUGUST 15, 1871
Scott, the delight of generous boys.

As far as Sir Walter Scott aspired to be known for a fine


gentleman, so far our sympathies leave him.... Our concern is
only with the residue, where the man Scott was warmed with
a divine ray that clad with beauty every sheet of water, every
bald hill in the country he looked upon, and so reanimated the
well-nigh obsolete feudal history and illustrated every hidden
corner of a barren and disagreeable territory.
Lecture, “Being and Seeing,” 1838.

WALTER SCOTT
The memory of Sir Walter Scott is dear to this Society, of which he
was for ten years an honorary member. If only as an eminent
antiquary who has shed light on the history of Europe and of the
English race, he had high claims to our regard. But to the rare tribute
of a centennial anniversary of his birthday, which we gladly join with
Scotland, and indeed with Europe, to keep, he is not less entitled—
perhaps he alone among literary men of this century is entitled—by
the exceptional debt which all English-speaking men have gladly
owed to his character and genius. I think no modern writer has
inspired his readers with such affection to his own personality. I can
well remember as far back as when The Lord of the Isles was first
republished in Boston, in 1815,—my own and my school-fellows’ joy
in the book.[214] Marmion and The Lay had gone before, but we
were then learning to spell. In the face of the later novels, we still
claim that his poetry is the delight of boys. But this means that when
we reopen these old books we all consent to be boys again. We
tread over our youthful grounds with joy. Critics have found them to
be only rhymed prose. But I believe that many of those who read
them in youth, when, later, they come to dismiss finally their school-
days’ library, will make some fond exception for Scott as for Byron.
It is easy to see the origin of his poems. His own ear had been
charmed by old ballads crooned by Scottish dames at firesides, and
written down from their lips by antiquaries; and finding them now
outgrown and dishonored by the new culture, he attempted to dignify
and adapt them to the times in which he lived. Just so much thought,
so much picturesque detail in dialogue or description as the old
ballad required, so much suppression of details and leaping to the
event, he would keep and use, but without any ambition to write a
high poem after a classic model. He made no pretension to the lofty
style of Spenser, or Milton, or Wordsworth. Compared with their
purified songs, purified of all ephemeral color or material, his were
vers de société. But he had the skill proper to vers de société,—skill
to fit his verse to his topic, and not to write solemn pentameters alike
on a hero or a spaniel. His good sense probably elected the ballad to
make his audience larger. He apprehended in advance the immense
enlargement of the reading public, which almost dates from the era
of his books,—which his books and Byron’s inaugurated; and which,
though until then unheard of, has become familiar to the present
time.
If the success of his poems, however large, was partial, that of his
novels was complete. The tone of strength in Waverley at once
announced the master, and was more than justified by the superior
genius of the following romances, up to the Bride of Lammermoor,
which almost goes back to Æschylus for a counterpart as a painting
of Fate,—leaving on every reader the impression of the highest and
purest tragedy.[215]
His power on the public mind rests on the singular union of two
influences. By nature, by his reading and taste an aristocrat, in a
time and country which easily gave him that bias, he had the virtues
and graces of that class, and by his eminent humanity and his love
of labor escaped its harm. He saw in the English Church the symbol
and seal of all social order; in the historical aristocracy the benefits to
the state which Burke claimed for it; and in his own reading and
research such store of legend and renown as won his imagination to
their cause. Not less his eminent humanity delighted in the sense
and virtue and wit of the common people. In his own household and
neighbors he found characters and pets of humble class, with whom
he established the best relation,—small farmers and tradesmen,
shepherds, fishermen, gypsies, peasant-girls, crones,—and came
with these into real ties of mutual help and good will. From these
originals he drew so genially his Jeanie Deans, his Dinmonts and
Edie Ochiltrees, Caleb Balderstones and Fairservices, Cuddie
Headriggs, Dominies, Meg Merrilies, and Jenny Rintherouts, full of
life and reality; making these, too, the pivots on which the plots of his
stories turn; and meantime without one word of brag of this
discernment,—nay, this extreme sympathy reaching down to every
beggar and beggar’s dog, and horse and cow. In the number and
variety of his characters he approaches Shakspeare. Other painters
in verse or prose have thrown into literature a few type-figures; as
Cervantes, De Foe, Richardson, Goldsmith, Sterne and Fielding; but
Scott portrayed with equal strength and success every figure in his
crowded company.
His strong good sense saved him from the faults and foibles
incident to poets,—from nervous egotism, sham modesty or
jealousy. He played ever a manly part.[216] With such a fortune and
such a genius, we should look to see what heavy toll the Fates took
of him, as of Rousseau or Voltaire, of Swift or Byron. But no: he had
no insanity, or vice, or blemish. He was a thoroughly upright, wise
and great-hearted man, equal to whatever event or fortune should try
him. Disasters only drove him to immense exertion. What an
ornament and safeguard is humor! Far better than wit for a poet and
writer. It is a genius itself, and so defends from the insanities.
Under what rare conjunction of stars was this man born, that,
wherever he lived, he found superior men, passed all his life in the
best company, and still found himself the best of the best! He was
apprenticed at Edinburgh to a Writer to the Signet, and became a
Writer to the Signet, and found himself in his youth and manhood
and age in the society of Mackintosh, Horner, Jeffrey, Playfair,
Dugald Stewart, Sydney Smith, Leslie, Sir William Hamilton, Wilson,
Hogg, De Quincey,—to name only some of his literary neighbors,
and, as soon as he died, all this brilliant circle was broken up.
XXVI
SPEECH

AT BANQUET IN HONOR OF THE CHINESE


EMBASSY BOSTON, 1860

Nature creates in the East the uncontrollable yearning to


escape from limitation into the vast and boundless, to use a
freedom of fancy which plays with all works of Nature, great
or minute, galaxy or grain of dust, as toys and words of the
mind; inculcates a beatitude to be found in escape from all
organization and all personality, and makes ecstasy an
institution.

SPEECH
AT THE BANQUET IN HONOR OF THE CHINESE EMBASSY

Mr. Mayor: I suppose we are all of one opinion on this


remarkable occasion of meeting the embassy sent from the oldest
Empire in the world to the youngest Republic. All share the surprise
and pleasure when the venerable Oriental dynasty—hitherto a
romantic legend to most of us—suddenly steps into the fellowship of
nations. This auspicious event, considered in connection with the
late innovations in Japan, marks a new era, and is an irresistible
result of the science which has given us the power of steam and the
electric telegraph. It is the more welcome for the surprise. We had
said of China, as the old prophet said of Egypt, “Her strength is to sit
still.” Her people had such elemental conservatism that by some
wonderful force of race and national manners, the wars and
revolutions that occur in her annals have proved but momentary
swells or surges on the pacific ocean of her history, leaving no trace.
But in its immovability this race has claims. China is old, not in time
only, but in wisdom, which is gray hair to a nation,—or, rather, truly
seen, is eternal youth. As we know, China had the magnet centuries
before Europe; and block-printing or stereotype, and lithography, and
gunpowder, and vaccination, and canals; had anticipated Linnæus’s
nomenclature of plants; had codes, journals, clubs, hackney
coaches, and, thirty centuries before New York, had the custom of
New Year’s calls of comity and reconciliation. I need not mention its
useful arts,—its pottery indispensable to the world, the luxury of
silks, and its tea, the cordial of nations. But I must remember that
she has respectable remains of astronomic science, and historic
records of forgotten time, that have supplied important gaps in the
ancient history of the western nations. Then she has philosophers
who cannot be spared. Confucius has not yet gathered all his fame.
When Socrates heard that the oracle declared that he was the wisest
of men, he said, it must mean that other men held that they were
wise, but that he knew that he knew nothing. Confucius had already
affirmed this of himself: and what we call the Golden Rule of
Jesus, Confucius had uttered in the same terms five hundred years
before. His morals, though addressed to a state of society unlike
ours, we read with profit to-day. His rare perception appears in his
Golden Mean, his doctrine of Reciprocity, his unerring insight,—
putting always the blame of our misfortunes on ourselves; as when
to the governor who complained of thieves, he said, “If you, sir, were
not covetous, though you should reward them for it, they would not
steal.” His ideal of greatness predicts Marcus Antoninus. At the same
time, he abstained from paradox, and met the ingrained prudence of
his nation by saying always, “Bend one cubit to straighten eight.”
China interests us at this moment in a point of politics. I am sure
that gentlemen around me bear in mind the bill which the Hon. Mr.
Jenckes of Rhode Island has twice attempted to carry through
Congress, requiring that candidates for public offices shall first pass
examinations on their literary qualifications for the same. Well, China
has preceded us, as well as England and France, in this essential
correction of a reckless usage; and the like high esteem of education
appears in China in social life, to whose distinctions it is made an
indispensable passport.
It is gratifying to know that the advantages of the new intercourse
between the two countries are daily manifest on the Pacific coast.
The immigrants from Asia come in crowds. Their power of
continuous labor, their versatility in adapting themselves to new
conditions, their stoical economy, are unlooked-for virtues. They
send back to their friends, in China, money, new products of art, new
tools, machinery, new foods, etc., and are thus establishing a
commerce without limit. I cannot help adding, after what I have heard
to-night, that I have read in the journals a statement from an English
source, that Sir Frederic Bruce attributed to Mr. Burlingame the merit
of the happy reform in the relations of foreign governments to China.
I am quite sure that I heard from Mr. Burlingame in New York, in his
last visit to America, that the whole merit of it belonged to Sir
Frederic Bruce. It appears that the ambassadors were emulous in
their magnanimity. It is certainly the best guaranty for the interests of
China and of humanity.
XXVII
REMARKS

AT THE MEETING FOR ORGANIZING THE FREE


RELIGIOUS ASSOCIATION, BOSTON MAY 30,
1867

In many forms we try


To utter God’s infinity,
But the Boundless hath no form,
And the Universal Friend
Doth as far transcend
An angel as a worm.

The great Idea baffles wit,


Language falters under it,
It leaves the learned in the lurch;
Nor art, nor power, nor toil can find
The measure of the eternal Mind,
Nor hymn nor prayer nor church.

REMARKS
AT THE MEETING FOR ORGANIZING THE FREE RELIGIOUS ASSOCIATION

Mr. Chairman: I hardly felt, in finding this house this morning, that
I had come into the right hall. I came, as I supposed myself
summoned, to a little committee meeting, for some practical end,
where I should happily and humbly learn my lesson; and I supposed
myself no longer subject to your call when I saw this house. I have
listened with great pleasure to the lessons which we have heard. To
many, to those last spoken, I have found so much in accord with my
own thought that I have little left to say. I think that it does great
honor to the sensibility of the committee that they have felt the
universal demand in the community for just the movement they have
begun. I say again, in the phrase used by my friend, that we began
many years ago,—yes, and many ages before that. But I think the
necessity very great, and it has prompted an equal magnanimity, that
thus invites all classes, all religious men, whatever their connections,
whatever their specialties, in whatever relation they stand to the
Christian Church, to unite in a movement of benefit to men, under
the sanction of religion. We are all very sensible—it is forced on us
every day—of the feeling that churches are outgrown; that the
creeds are outgrown; that a technical theology no longer suits us. It
is not the ill will of people—no, indeed, but the incapacity for
confining themselves there. The church is not large enough for the
man; it cannot inspire the enthusiasm which is the parent of
everything good in history, which makes the romance of history. For
that enthusiasm you must have something greater than yourselves,
and not less.
The child, the young student, finds scope in his mathematics and
chemistry or natural history, because he finds a truth larger than he
is; finds himself continually instructed. But, in churches, every
healthy and thoughtful mind finds itself in something less; it is
checked, cribbed, confined. And the statistics of the American, the
English and the German cities, showing that the mass of the
population is leaving off going to church, indicate the necessity,
which should have been foreseen, that the Church should always be
new and extemporized, because it is eternal and springs from the
sentiment of men, or it does not exist.[217] One wonders sometimes
that the churches still retain so many votaries, when he reads the
histories of the Church. There is an element of childish infatuation in
them which does not exalt our respect for man. Read in Michelet,
that in Europe, for twelve or fourteen centuries, God the Father had
no temple and no altar. The Holy Ghost and the Son of Mary were
worshipped, and in the thirteenth century the First Person began to
appear at the side of his Son, in pictures and in sculpture, for
worship, but only through favor of his Son. These mortifying
puerilities abound in religious history. But as soon as every man is
apprised of the Divine Presence within his own mind,—is apprised
that the perfect law of duty corresponds with the laws of chemistry, of
vegetation, of astronomy, as face to face in a glass; that the basis of
duty, the order of society, the power of character, the wealth of
culture, the perfection of taste, all draw their essence from this moral
sentiment, then we have a religion that exalts, that commands all the
social and all the private action.
What strikes me in the sudden movement which brings together
to-day so many separated friends,—separated but sympathetic,—
and what I expected to find here, was some practical suggestions by
which we were to reanimate and reorganize for ourselves the true
Church, the pure worship. Pure doctrine always bears fruit in pure
benefits. It is only by good works, it is only on the basis of active
duty, that worship finds expression. What is best in the ancient
religions was the sacred friendships between heroes, the Sacred
Bands, and the relations of the Pythagorean disciples. Our Masonic
institutions probably grew from the like origin. The close association
which bound the first disciples of Jesus is another example; and it
were easy to find more. The soul of our late war, which will always
be remembered as dignifying it, was, first, the desire to abolish
slavery in this country, and secondly, to abolish the mischief of the
war itself, by healing and saving the sick and wounded soldiers,—
and this by the sacred bands of the Sanitary Commission. I wish that
the various beneficent institutions which are springing up, like joyful
plants of wholesomeness, all over this country, should all be
remembered as within the sphere of this committee,—almost all of
them are represented here,—and that within this little band that has
gathered here to-day, should grow friendship. The interests that grow
out of a meeting like this should bind us with new strength to the old
eternal duties.
XXVIII
SPEECH

AT THE SECOND ANNUAL MEETING OF THE


FREE RELIGIOUS ASSOCIATION, AT TREMONT
TEMPLE FRIDAY, MAY 28, 1869

Thou metest him by centuries,


And lo! he passes like the breeze;
Thou seek’st in globe and galaxy,
He hides in pure transparency;
Thou ask’st in fountains and in fires,
He is the essence that inquires.

SPEECH
AT SECOND ANNUAL MEETING OF THE FREE RELIGIOUS ASSOCIATION

Friends: I wish I could deserve anything of the kind expression of


my friend, the President, and the kind good will which the audience
signifies, but it is not in my power to-day to meet the natural
demands of the occasion, and, quite against my design and my will, I
shall have to request the attention of the audience to a few written
remarks, instead of the more extensive statement which I had hoped
to offer them.
I think we have disputed long enough. I think we might now
relinquish our theological controversies to communities more idle
and ignorant than we. I am glad that a more realistic church is
coming to be the tendency of society, and that we are likely one day
to forget our obstinate polemics in the ambition to excel each other in
good works. I have no wish to proselyte any reluctant mind, nor, I
think, have I any curiosity or impulse to intrude on those whose ways
of thinking differ from mine. But as my friend, your presiding officer,
has asked me to take at least some small part in this day’s
conversation, I am ready to give, as often before, the first simple
foundation of my belief, that the Author of Nature has not left himself
without a witness in any sane mind: that the moral sentiment speaks
to every man the law after which the Universe was made; that we
find parity, identity of design, through Nature, and benefit to be the
uniform aim: that there is a force always at work to make the best
better and the worst good.[218] We have had not long since
presented us by Max Müller a valuable paragraph from St.
Augustine, not at all extraordinary in itself, but only as coming from
that eminent Father in the Church, and at that age, in which St.
Augustine writes: “That which is now called the Christian religion
existed among the ancients, and never did not exist from the planting
of the human race until Christ came in the flesh, at which time the
true religion which already existed began to be called Christianity.” I
believe that not only Christianity is as old as the Creation,—not only
every sentiment and precept of Christianity can be paralleled in other
religious writings,—but more, that a man of religious susceptibility,
and one at the same time conversant with many men,—say a much-
travelled man,—can find the same idea in numberless
conversations. The religious find religion wherever they associate.
When I find in people narrow religion, I find also in them narrow
reading. Nothing really is so self-publishing, so divulgatory, as
thought. It cannot be confined or hid. It is easily carried; it takes no
room; the knowledge of Europe looks out into Persia and India, and
to the very Kaffirs. Every proverb, every fine text, every pregnant
jest, travels across the line; and you will find it at Cape Town, or
among the Tartars. We are all believers in natural religion; we all
agree that the health and integrity of man is self-respect, self-
subsistency, a regard to natural conscience. All education is to
accustom him to trust himself, discriminate between his higher and
lower thoughts, exert the timid faculties until they are robust, and

You might also like