COMPUTER SCIENCE - GBV

9
COMPUTER SCIENCE A Breadth-First Approach with С John Impagliazzo Paul Nagin Hofstra University Hempstead, New York ® John Wiley & Sons New York Chichester Brisbane Toronto Singapore

Transcript of COMPUTER SCIENCE - GBV

Page 1: COMPUTER SCIENCE - GBV

COMPUTER SCIENCE

A Breadth-First Approach with С

John Impagliazzo Paul Nagin Hofstra University

Hempstead, New York

® John Wiley & Sons

New York Chichester Brisbane Toronto Singapore

Page 2: COMPUTER SCIENCE - GBV

Contents

1 Introduction to Computing Systems 1.1 A Panorama of Computer Science 3

Introduction, 3 1.1.1 The Elements of Computing, 3 1.1.2 The Social Context of Computing, 5 1.1.3 End-User Applications, 7

Exercises 1.1, 8 1.2 Computer Hardware and Software 10

Introduction, 10 1.2.1 Preliminaries, 10 1.2.2 Peripheral Input and Output Devices, 11 1.2.3 More on Hardware Basics, 15 1.2.4 Software Basics, 17 1.2.5 Communications Basics, 18

Exercises 1.2, 19 1.3 Computing Perspectives 22

Introduction, 22 1.31 Evolution of Computer Hardware and

Software, 22 1.3-2 von Neumann and Non-von Neumann

Architectures, 28 1-3-3 Professional Roles, 32 1.3-4 The Future of Computing, 32

Exercises 1.3, 33

2 Problem-Solving Concepts 2.1 The Analytic Approach 38

Introduction, 38 2.1.1 Problem-Solving Approaches, 38 2.1.2 The Analytic Approach, 39 2.1.3 Unsolvable Problems, 42

Exercises 2.1, 42 2.2 The Algorithmic Approach 44

Introduction, 44 2.2.1 Algorithms, 44 2.2.2 Phases of Algorithmic Problem Solving, 48 2.2.3 Sequence, 48 2.2.4 Selection, 51 2.2.5 Repetition, 54

Page 3: COMPUTER SCIENCE - GBV

CONTENTS

2.2.6 Conditional Looping, 56 Exercises 2.2, 59

3 Elements of the C Language 3 1 Background and Structure of С 64

Introduction, 64 3-1.1 History of C, 64 3-1.2 A Simple Program in C, 65 3-1-3 A High-Quality С Program, 67

Exercises 3-1, 71 3-2 Data Types, Variables, and Input /Output 73

Introduction, 73 3-2.1 int Data Type, 73 3-2.2 float and double Data Types, 74 3-2.3 char Data Type, 74 3-2.4 Naming Conventions, 75 3-2.5 Symbolic Constants, 76 3-2.6 Variables, 77 3-2.7 Input and Output, 78

Exercises 3-2, 80 3.3 Arithmetic in С 82

Introduction, 82 3-3-1 Operator Precedence, 82 3-3-2 Integer Arithmetic, 84 3-3-3 Mixed-Mode Arithmetic, 85 3-3-4 Assignment Statements, 86 3-3-5 Increment and Decrement Operators, 89 3-3-6 Type Casting, 90

Exercises 3-3, 91

4 Computer Logic and Architecture 4.1 Number Systems 95

Introduction, 95 4.1.1 Binary Codes: ASCII and EBCDIC, 95 4.1.2 Positional Notation, 97 4.1.3 Binary to Decimal Conversion, 99 4.1.4 Decimal to Binary Conversion, 103 4.1.5 Negative Numbers in Twos Complement Form, 106 4.1.6 Addition and Multiplication, 108 4.1.7 Subtraction and Division, 112

Exercises 4.1, 114 4.2 Logic and Computers 115

Introduction, 115 4.2.1 Logic and Binary Systems, 116 4.2.2 Truth Tables: NOT, AND, OR, XOR, 117 4.2.3 Logic Gates, 119 4.2.4 Logic Functions Using Gates, 120 4.2.5 Equivalent Circuits, 122 4.2.6 Relational and Logical Operators in C, 126

Exercises 4.2, 130

Page 4: COMPUTER SCIENCE - GBV

CONTENTS XVI i

4.3 Machine Representation of Numbers 132 Introduction, 132 4.3-1 Integers and Real Numbers, 132 4.3-2 Floating-Point Numbers, 134 4.3-3 Precision and Accuracy, 138 4.3-4 Machine and Program Considerations, 140

Exercises 4.3, 141

5 Functions and Control Structures 143 5.1 Functions 145

Introduction, 145 5.1.1 Writing Functions, 145 5.1.2 Functions with Arguments, 147 5.1.3 Function Prototypes, 151

Exercises 5.1, 152 5.2 Selection Structures 154

Introduction, 154 5.2.1 One-Way Selection Using the if Statement, 154 5.2.2 Two-Way Selection Using the if...else

Statement, 157 5.2.3 Multiple Selection Using the if Ladder, 161 5.2.4 Multiple Selection Using the switch Statement, 165

Exercises 5-2, 170 5.3 Looping Structures 172

Introduction, 172 5-3-1 Conditional Looping Using the while Statement, 172 5-3-2 Conditional Looping Using the do...while

Statement, 174 5-3-3 Iterative Looping Using the for Statement, 177 5.3-4 Nested Loops, 180

Exercises 5-3, 186

6 Operating Systems 191 6.1 Windows to Hardware 193

Introduction, 193 6.1.1 The System Executive, 193 6.1.2 Timesharing, 195 6.1.3 Scheduling Algorithms, 198 6.1.4 Protection and Security, 200

Exercises 6.1, 201 6.2 System Tools and Virtual Memory 202

Introduction, 202 6.2.1 Editors and Debuggers, 203 6.2.2 Assemblers, Compilers, and Interpreters, 203 6.2.3 Linkers and Loaders, 205 6.2.4 Virtual Memory, 206 6.2.5 Pages and Page Frames, 206 6.2.6 Other Considerations with Virtual Memory, 209 6.2.7 Large-Scale and Personal Computers, 211

Exercises 6.2, 212

Page 5: COMPUTER SCIENCE - GBV

xviii CONTENTS

7 Arrays 214 7.1 Overview of Arrays 216

Introduction, 216 7.1.1 Array Basics, 216 7.1.2 Using Arrays, 218 7.1.3 Initializing Arrays, 224

Exercises 7.1, 225 7.2 Arrays and Functions 227

Introduction, 227 7.2.1 Using Arrays with Functions, 227

Exercises 7.2, 238 7.3 Higher-Dimensional Arrays 240

Introduction, 240 7.3-1 Matrices, 240 7.3-2 Passing Multidimensional Arrays, 245

Exercises 7.3, 250 7.4 String Processing 251

Introduction, 251 7.4.1 Fundamentals of Strings, 251 7.4.2 String Input/Output, 252 7.4.3 Built-in String Functions, 253 7.4.4 Examples of Character and String Processing, 255

Exercises 7.4, 260

8 Data Communications 262 8.1 Communicat ions Overview 264

Introduction, 264 8.1.1 Data Communications and Standards, 264 8.1.2 Digital and Analog Transmission, 266 8.1.3 Modems, 268 8.1.4 Communications Media and Modalities, 269 8.1.5 Protocols, 272

Exercises 8.1, 275 8.2 Parity and Error in Communicat ions 277

Introduction, 277 8.2.1 Parity, 277 8.2.2 Error Detection and Correction, 280 8.2.3 Hamming Codes and Error Detection, 281 8.2.4 Error-Correcting Codes, 282

Exercises 8.2, 291

9 Pointers and Function Parameters 294 9.1 Pointers 296

Introduction, 296 9.1.1 An Overview of Pointer Variables, 297

Exercises 9.1, 299 9.2 Functions with Output Parameters 302

Introduction, 302 9.2.1 Input and Output Parameters, 302 9.2.2 Inout Parameters, 306

Page 6: COMPUTER SCIENCE - GBV

CONTENTS X I X

9.2.3 Variable Scope, 309 Exercises 9.2, 311

10 Software Engineering 314 10.1 The Software Engineering Approach 316

Introduction, 316 10.1.1 Tools for Algorithmic Design, 316 10.1.2 Design Strategies: Bottom-Up or Top-Down, 317 10.1.3 Formal Problem Statement, 318 10.1.4 The Waterfall Model, 319 10.1.5 Program Maintenance, 320

Exercises 10.1, 321 10.2 The Software Life Cycle: Two Applications 322

Introduction, 322 10.2.1 First Application: Grading Problem, 323 10.2.2 Second Application: Payroll Calculations, 326

Exercises 10.2, 335

11 Data Structures 338 11.1 Tools for Creating Data Structures 340

Introduction, 340 11.1.1 The Array Revisited, 341 11.1.2 The enum Specifier, 342 11.1.3 The typedef Specifier, 343 11.1.4 The struct Specifier, 344

Exercises 11.1, 350 11.2 ADT Stacks 352

Introduction, 352 11.2.1 The ADT Stack, 354 11.2.2 Implementing the Stack as an Array, 358 11.2.3 Postfix Arithmetic Using a Stack, 364

Exercises 11.2, 366 11.3 ADT Queues 368

Introduction, 368 11.3-1 The ADT Queue, 369 11.3-2 Implementing the Queue as a Circular

Array, 373 Exercises 11.3, 381

12 Databases 12.1 An Overview of Files and Databases 385

Introduction, 385 12.1.1 Records, 385 12.1.2 Files, 387 12.1.3 Databases, 394 12.1.4 Database Entities and Attributes, 395

Exercises 12.1, 397 12.2 Logical Database Models 399

Introduction, 399 12.2.1 Overview of logical Models, 399 12.2.2 The Entity-Relationship Model, 400

383

Page 7: COMPUTER SCIENCE - GBV

X X CONTENTS

12.23 The Hierarchical and Network Models, 401

12.2.4 The Relational Model, 404 12.2.5 A Database Program, 407

Exercises 12.2, 418

13 Dynamic Lists 421 13-1 Memory Allocation and Dynamically Linked Stacks 423

Introduction, 423 13.1.1 The ADT Stack Revisited, 424 13-1.2 Tools for Dynamic Memory Allocation, 425 13 13 The Dynamic Stack: An Informal Tour, 426 13-1.4 Implementing the ADT Stack, 430 131-5 Dynamically Allocating Arrays, 434

Exercises 13.1, 439 13-2 Dynamically Linked Queues 440

Introduction, 440 13-2.1 The ADT Queue Revisited, 441 13.2.2 Implementing the ADT Queue, 442

Exercises 13-2, 447 153 Generalized Linked Lists 449

Introduction, 449 133-1 ADT Linked Lists, 449 133-2 Implementing the ADT Linked-List Functions, 451 13-33 Other Linked Structures, 459

Exercises 133, 461

14 Programming Languages 463 14.1 Overview of Programming Languages 465

Introduction, 465 14.1.1 Historical Perspectives, 465 14.1.2 Grammars, Syntax, and Semantics, 468 14.1.3 What Makes a Language Tick?, 471

Exercises 14.1, 472 14.2 Language Paradigms 474

Introduction, 474 14.2.1 Imperative Languages, 475 14.2.2 Object-Oriented Languages, 476 14.2.3 Applicative Languages, 479 14.2.4 Declarative Languages, 481 14.2.5 Reflections, 483

Exercises 14.2, 484

15 Recursive Algorithms 486 15.1 Thinking Recursively 488

Introduction, 488 15.1.1 Examples of Recursive Functions, 488 151.2 Recursion and Reversal, 491 15.1-3 How Recursion Works: The Run-Time

Stack, 494 Exercises 15 1, 496

Page 8: COMPUTER SCIENCE - GBV

CONTENTS XXJ

15.2 Applying Recursion 498 Introduction, 498 15.2.1 Applications of Recursion with ADTs, 498 15-2.2 An Application from Medical Imaging, 504 15.2.3 Final Comments on Recursion, 507

Exercises 15.2, 508

16 Searching and Sorting Algorithms 510 16.1 Linear and Binary Search 512

Introduction, 512 16.1.1 Linear Search: Unordered Data Structures, 512 16.1.2 Binary Search: Ordered Data Structures, 516

Exercises 16.1, 519 16.2 Sorting Functions 520

Introduction, 520 16.2.1 Simple Sorts: Selection and Insertion, 521 16.2.2 Advanced Sorting: Shell Sort and

Quicksort, 529 16.2.3 The Best Sort of All?, 533

Exercises 16.2, 535

17 Numerical Algorithms 538 17.1 Zeros of Functions 540

Introduction, 540 17.1.1 Functions and Their Zeros, 540 17.1.2 The Bisection Method, 544 17.1.3 The Secant Method, 550 17.1.4 The Newton-Raphson Algorithm, Abbreviated, 555 17.1.5 Reflections, 561

Exercises 17.1, 562 17.2 Solving Systems of Equations 564

Introduction, 564 17.2.1 Two Equations in Two Unknowns, 564 17-2.2 Gaussian Elimination, 566 17.2.3 Matrix Reduction of Equations, 568 17-2.4 Gaussian Elimination with Backward Substitution, 573 17-2.5 Gauss-Seidel Iteration, 578

Exercises 17.2, 582

17.3 Differentiation and Integration 584 Introduction, 584 173-1 The Meaning of Differentiation, 585 17.3-2 Algorithms for Approximating Derivatives, 589 17.3-3 The Meaning of Integration, 593 17.3-4 Algorithms for Integrals, 595 17.3-5 Reflections, 603

Exercises 17.3, 604

18 Theoretical Perspectives in Computing 608 18.1 Finite-State and Turing Machines 610

Introduction, 610 18.1.1 Finite-State Machines, 610

Page 9: COMPUTER SCIENCE - GBV

xxii CONTENTS

18.1.2 Graphs and Finite-State Machines, 613 18.1.3 Turing Machines, 615 18.1.4 Turing Machines and Computability, 618

Exercises 18.1, 620 18.2 Algorithmic Efficiency and Complexity 622

Introduction, 622 18.2.1 Two Search Algorithms, 622 18.2.2 Big-O Notation, 627 18.2.3 Analysis of Search Algorithms, 631 18.2.4 Further Thoughts About Complexity, 637

Exercises 18.2, 638 19 Artificial Intelligence 641

19.1 Areas of Inquiry 643 Introduction, 643 19.1.1 Basic Issues, 643 19.1.2 Expert Systems, 645 19.1.3 Natural-Language Processing, 648

Exercises 19.1, 649 19.2 Case Study: Computer Vision 650

Introduction, 650 19.2.1 The Digital Image, 652 19.2.2 The Low-Level Module, 655 19.2.3 An Example of Low-Level Processing, 658 19.2.4 High-Level Module, 664

Exercises 19.2, 666 APPENDIXES 669 A-l Common Functions in stdio.h File 670 A-2 Common Functions in math.h File 671 A-3 Common Functions in ctype.h File 672 A-4 Common Functions in stdlib.h File 673 A-5 Common Functions in string.h File 674

B. Escape Sequences 675 С Keywords in С 676 D. С Operators: Precedence and Associativity 681 E. ASCII / EBCDIC Character Codes 682 F. Derivation of Simpson's Rule 690

ANSWERS TO SELECTED EXERCISES 693

PHOTO CREDITS 727

INDEX 729