ebook img

PHP and Algorithmic Thinking for the Complete Beginner: Learn to Think Like a Programmer PDF

881 Pages·2014·11.976 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview PHP and Algorithmic Thinking for the Complete Beginner: Learn to Think Like a Programmer

PHP and Algorithmic Thinking for the Complete Beginner Kindle Edition Copyright © by Aristides S. Bouras and Loukia V. Ainarozidou http://www.bouraspage.com PHP is a copyright of the PHP Group. The following are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries: Microsoft, Windows, IntelliSense, SQL Server, VBA, Visual Basic, and Visual C#. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, mechanical or electronic, including photocopying, recording, or by any information storage and retrieval system, without written permission from the authors. Warning and Disclaimer This book is designed to provide information about learning “Algorithmic Thinking,” mainly through the use of PHP scripting language. Every effort has been taken to make this book compatible with all previous releases of PHP, and it is almost certain to be compatible with any future releases of PHP. The information is provided on an “as is” basis. The authors shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the files that may accompany it. This book is dedicated to our adorable kids, Bill and Aphrodite, as they “force” us to become better and better every day. Contents at a Glance Preface………….. ............................................................................................................................ 21 Section 1 Introductory Knowledge .................................................................................... 27 Chapter 1 How a Computer Works .................................................................................... 28 Chapter 2 PHP .......................................................................................................................... 35 Chapter 3 Software Packages to Install .......................................................................... 38 Review Questions in “Introductory Knowledge” ........................................................... 48 Section 2 Getting Started with PHP ................................................................................... 49 Chapter 4 Introduction to Basic Algorithmic Concepts .............................................. 50 Chapter 5 Variables and Constants .................................................................................. 65 Chapter 6 Handling Input and Output ............................................................................. 76 Chapter 7 Operators .............................................................................................................. 83 Chapter 8 Trace Tables ......................................................................................................... 98 Chapter 9 Using NetBeans IDE .......................................................................................... 107 Review Questions in “Getting Started with PHP” ........................................................ 123 Section 3 Sequence Control Structures .......................................................................... 125 Chapter 10 Introduction to Sequence Control Structures ....................................... 126 Chapter 11 Manipulating Numbers ................................................................................ 135 Chapter 12 Complex Mathematical Expressions ........................................................ 145 Chapter 13 Exercises with a Quotient and a Remainder ......................................... 152 Chapter 14 Manipulating Strings .................................................................................... 163 Review Questions in “Sequence Control Structures” ................................................. 177 Section 4 Decision Control Structures ............................................................................ 178 Chapter 15 Introduction to Decision Control Structures ......................................... 179 Chapter 16 The Single-Alternative Decision Structure ............................................ 195 Chapter 17 The Dual-Alternative Decision Structure ............................................... 205 Chapter 18 The Multiple-Alternative Decision Structure ........................................ 218 Chapter 19 The Case Decision Structure ....................................................................... 227 Chapter 20 Nested Decision Control Structures .......................................................... 237 Chapter 21 Tips and Tricks with Decision Control Structures ............................... 250 Chapter 22 Flowcharts with Decision Control Structures ....................................... 295 Chapter 23 More Exercises with Decision Control Structures ................................ 318 Review Questions in “Decision Control Structures” ................................................... 364 Section 5 Loop Control Structures ................................................................................... 365 Chapter 24 Introduction to Loop Control Structures ................................................ 366 Chapter 25 The Pre-Test Loop Structure ....................................................................... 371 Chapter 26 The Post-Test Loop Structure ..................................................................... 388 Chapter 27 Counted Loop Structures ............................................................................. 403 Chapter 28 Nested Loop Control Structures ................................................................. 424 Chapter 29 Tips and Tricks with Loop Control Structures ...................................... 438 Chapter 30 Flowcharts with Loop Control Structures .............................................. 488 Chapter 31 More Exercises with Loop Control Structures ....................................... 514 Review Questions in “Loop Control Structures” .......................................................... 575 Section 6 Arrays ...................................................................................................................... 577 Chapter 32 Introduction to Arrays .................................................................................. 578 Chapter 33 One-Dimensional Arrays ............................................................................. 587 Chapter 34 Two-Dimensional Arrays ............................................................................. 604 Chapter 35 Tips and Tricks with Arrays ....................................................................... 632 Chapter 36 Flowcharts with Arrays ................................................................................ 654 Chapter 37 More Exercises with Arrays ........................................................................ 668 Review Questions in “Arrays” ........................................................................................... 767 Section 7 Subprograms ........................................................................................................ 769 Chapter 38 Introduction to Subprograms .................................................................... 770 Chapter 39 User-Defined Functions ................................................................................ 774 Chapter 40 User-Defined Procedures ............................................................................. 788 Chapter 41 Tips and Tricks with Subprograms .......................................................... 800 Chapter 42 Flowcharts with Subprograms .................................................................. 838 Chapter 43 More Exercises with Subprograms ........................................................... 855 Review Questions in “Subprograms” .............................................................................. 874 Index.............................................................................................................................................. 875 Table of Contents Preface………….. ............................................................................................................................ 21 About the Authors ................................................................................................................................. 22 Acknowledgments ................................................................................................................................. 23 How This Book is Organized ............................................................................................................. 23 Who Should Buy This Book? ............................................................................................................. 23 Where to Find Answers to Review Questions and Exercises ............................................. 24 How to Report Errata........................................................................................................................... 24 Conventions Used in This Book ....................................................................................................... 25 Section 1 Introductory Knowledge .................................................................................... 27 Chapter 1 How a Computer Works .................................................................................... 28 1.1 Introduction ............................................................................................................................. 29 1.2 What is Hardware? ................................................................................................................ 29 1.3 What is Software? .................................................................................................................. 30 1.4 How a Computer Executes (Runs) a Program ........................................................... 30 1.5 Compilers and Interpreters ............................................................................................... 30 1.6 What is Source Code? ........................................................................................................... 31 1.7 Review Questions: True/False ......................................................................................... 31 1.8 Review Questions: Multiple Choice ............................................................................... 32 Chapter 2 PHP .......................................................................................................................... 35 2.1 What is PHP? ............................................................................................................................ 36 2.2 What is the Difference Between a Script and a Program? ................................... 36 2.3 Why You Should Learn PHP .............................................................................................. 36 2.4 How PHP Works ..................................................................................................................... 37 Chapter 3 Software Packages to Install .......................................................................... 38 3.1 XAMPP ........................................................................................................................................ 39 3.2 How to Set Up XAMPP .......................................................................................................... 39 3.3 NetBeans .................................................................................................................................... 41 3.4 How to Set Up NetBeans IDE ............................................................................................ 42 Review Questions in “Introductory Knowledge” ........................................................... 48 Section 2 Getting Started with PHP ................................................................................... 49 Chapter 4 Introduction to Basic Algorithmic Concepts .............................................. 50 4.1 What is an Algorithm? ......................................................................................................... 51 4.2 The Algorithm for Making a Cup of Tea ....................................................................... 51 4.3 Properties of an Algorithm ................................................................................................ 51 4.4 Okay About Algorithms. But What is a Computer Program Anyway? ............ 52 4.5 The Party of Three! ............................................................................................................... 52 4.6 The Three Main Stages Involved in Creating an Algorithm ................................ 52 4.7 Flowcharts ................................................................................................................................ 53 Exercise 4.7-1 Finding the Average Value of Three Numbers ...................................... 55 4.8 What are ”Reserved Words”? ........................................................................................... 56 4.9 What is the Difference Between a Statement and a Command? ....................... 57 4.10 What is Structured Programming? ................................................................................ 57 4.11 The Three Fundamental Control Structures .............................................................. 57 Exercise 4.11-1 Understanding Control Structures Using Flowcharts ........................ 58 4.12 Your First PHP Script ........................................................................................................... 59 4.13 What is the Difference Between Syntax Errors and Logic Errors? .................. 59 4.14 Commenting Your Code ...................................................................................................... 60 4.15 User-Friendly Programs and Scripts ............................................................................. 61 4.16 Review Questions: True/False ......................................................................................... 61 4.17 Review Questions: Multiple Choice ............................................................................... 63 Chapter 5 Variables and Constants .................................................................................. 65 5.1 What is a Variable? ................................................................................................................ 66 5.2 What is a Constant? ............................................................................................................... 68 5.3 How Many Types of Variables and Constants Exist in PHP? .............................. 71 5.4 Rules for Naming Variables in PHP ................................................................................ 71 5.5 Rules for Naming Constants in PHP ............................................................................... 72 5.6 What Does the Phrase “Declare a Variable” Mean? ................................................ 72 5.7 How to Declare Variables in PHP .................................................................................... 73 5.8 How to Declare Constants in PHP ................................................................................... 73 5.9 Review Questions: True/False ......................................................................................... 73 5.10 Review Questions: Multiple Choice ............................................................................... 74 5.11 Review Exercises.................................................................................................................... 75 Chapter 6 Handling Input and Output ............................................................................. 76 6.1 Which Statement Outputs Messages and Results on a User’s Screen? .......... 77 6.2 How to Output Special Characters ................................................................................. 78 6.3 Which Statement Lets the User Enter Data? .............................................................. 80 6.4 Review Questions: True/False ......................................................................................... 82 6.5 Review Questions: Multiple Choice ............................................................................... 82 Chapter 7 Operators .............................................................................................................. 83 7.1 The Value Assignment Operator ..................................................................................... 84 7.2 Arithmetic Operators ........................................................................................................... 86 7.3 What is the Precedence of Arithmetic Operators? .................................................. 87 7.4 Compound Assignment Operators ................................................................................. 88 Exercise 7.4-1 Which PHP Statements are Syntactically Correct? .............................. 89 Exercise 7.4-2 Finding Variable Types ..................................................................................... 90 7.5 Incrementing/Decrementing Operators ..................................................................... 90 7.6 String Operators ..................................................................................................................... 91 Exercise 7.6-1 Concatenating Names ........................................................................................ 92 7.7 Review Questions: True/False ......................................................................................... 92 7.8 Review Questions: Multiple Choice ............................................................................... 93 7.9 Review Exercises.................................................................................................................... 95 Chapter 8 Trace Tables ......................................................................................................... 98 8.1 What is a Trace Table? ......................................................................................................... 99 Exercise 8.1-1 Creating a Trace Table ....................................................................................100 Exercise 8.1-2 Swapping Values of Variables .....................................................................100 Exercise 8.1-3 Swapping Values of Variables – A Second Approach .......................103 Exercise 8.1-4 Creating a Trace Table ....................................................................................103 Exercise 8.1-5 Creating a Trace Table ....................................................................................104 8.2 Review Questions: True/False ...................................................................................... 105 8.3 Review Exercises................................................................................................................. 105 Chapter 9 Using NetBeans IDE .......................................................................................... 107 9.1 Creating a New PHP Project ........................................................................................... 108 9.2 Writing and Executing a PHP Script ........................................................................... 110 9.3 What ”Debugging” Means ................................................................................................ 114 9.4 Debugging PHP Scripts with NetBeans IDE ............................................................ 114 9.5 Review Exercises................................................................................................................. 121 Review Questions in “Getting Started with PHP” ........................................................ 123 Section 3 Sequence Control Structures .......................................................................... 125 Chapter 10 Introduction to Sequence Control Structures ....................................... 126 10.1 What is the Sequence Control Structure? ................................................................ 127 Exercise 10.1-1 Calculating the Area of a Parallelogram .................................................127 Exercise 10.1-2 Calculating the Area of a Circle ...................................................................128 Exercise 10.1-3 Calculating Fuel Economy .............................................................................128 Exercise 10.1-4 Where is the Car? Calculating Distance Traveled ..............................129 Exercise 10.1-5 Kelvin to Fahrenheit ........................................................................................130 Exercise 10.1-6 Calculating Sales Tax........................................................................................131 Exercise 10.1-7 Calculating a Sales Discount .........................................................................131 Exercise 10.1-8 Calculating the Sales Tax Rate and Discount ........................................132 10.2 Review Exercises................................................................................................................. 133 Chapter 11 Manipulating Numbers ................................................................................ 135 11.1 Introduction .......................................................................................................................... 136 11.2 Useful Mathematical Functions .................................................................................... 136 Exercise 11.2-1 Calculating the Distance Between Two Points ....................................140 Exercise 11.2-2 How Far Did the Car Travel? ........................................................................141 11.3 Review Questions: True/False ...................................................................................... 142 11.4 Review Questions: Multiple Choice ............................................................................ 142 11.5 Review Exercises................................................................................................................. 143 Chapter 12 Complex Mathematical Expressions ........................................................ 145 12.1 Writing Complex Mathematical Expressions ......................................................... 146 Exercise 12.1-1 Representing Mathematical Expressions in PHP ...............................146 Exercise 12.1-2 Writing a Mathematical Expression in PHP ..........................................147 Exercise 12.1-3 Writing a Complex Mathematical Expression in PHP ......................147 12.2 Review Exercises................................................................................................................. 149 Chapter 13 Exercises with a Quotient and a Remainder ......................................... 152 13.1 Introduction .......................................................................................................................... 153 Exercise 13.1-1 Calculating the Quotient and Remainder of Integer Division ......153 Exercise 13.1-2 Finding the Sum of Digits ...............................................................................154 Exercise 13.1-3 Displaying an Elapsed Time .........................................................................159 Exercise 13.1-4 Reversing a Number.........................................................................................160 13.2 Review Exercises................................................................................................................. 161 Chapter 14 Manipulating Strings .................................................................................... 163 14.1 Introduction .......................................................................................................................... 164 14.2 The Position of a Character in a String ...................................................................... 164 14.3 Retrieving an Individual Character From a String ............................................... 165 Exercise 14.3-1 Displaying a String Backwards ...................................................................166 14.4 Useful String Functions .................................................................................................... 166 Exercise 14.4-1 Switching the Order of Names ....................................................................170 Exercise 14.4-2 Creating a Login ID ...........................................................................................171 Exercise 14.4-3 Creating a Random Word ..............................................................................172 14.5 Review Questions: True/False ...................................................................................... 173 14.6 Review Questions: Multiple Choice ............................................................................ 173 14.7 Review Exercises................................................................................................................. 175 Review Questions in “Sequence Control Structures” ................................................. 177 Section 4 Decision Control Structures ............................................................................ 178 Chapter 15 Introduction to Decision Control Structures ......................................... 179 15.1 What is a Decision Control Structure? ....................................................................... 180 15.2 What is a Boolean Expression? ..................................................................................... 180 15.3 How to Write Boolean Expressions ............................................................................ 180 Exercise 15.3-1 Filling in the Table ............................................................................................181 15.4 Logical Operators and Complex Boolean Expressions ....................................... 182 15.5 What is the Order of Precedence of Logical Operators? .................................... 183 15.6 What is the Order of Precedence of Arithmetic, Comparison, and Logical Operators? .............................................................................................................................. 183 Exercise 15.6-1 Filling in the Truth Table ...............................................................................184 Exercise 15.6-2 Calculating the Results of Complex Boolean Expressions .............185 Exercise 15.6-3 Converting English Sentences to Boolean Expressions ..................186 15.7 How to Negate Boolean Expressions ......................................................................... 188 Exercise 15.7-1 Negating Boolean Expressions ....................................................................189 15.8 Review Questions: True/False ...................................................................................... 190 15.9 Review Questions: Multiple Choice ............................................................................ 191 15.10 Review Exercises................................................................................................................. 192 Chapter 16 The Single-Alternative Decision Structure ............................................ 195 16.1 The Single-Alternative Decision Structure .............................................................. 196 Exercise 16.1-1 Trace Tables and Single-Alternative Decision Structures .............197 Exercise 16.1-2 The Absolute Value of a Number ...............................................................198 16.2 Review Questions: True/False ...................................................................................... 199 16.3 Review Questions: Multiple Choice ............................................................................ 200 16.4 Review Exercises................................................................................................................. 201 Chapter 17 The Dual-Alternative Decision Structure ............................................... 205 17.1 The Dual-Alternative Decision Structure ................................................................. 206 Exercise 17.1-1 Finding the Output Message.........................................................................206 Exercise 17.1-2 Trace Tables and Dual-Alternative Decision Structures ................207 Exercise 17.1-3 Who is the Greatest? ........................................................................................209 Exercise 17.1-4 Finding Odd and Even Numbers ................................................................211 Exercise 17.1-5 Weekly Wages .....................................................................................................212 17.2 Review Questions: True/False ...................................................................................... 213 17.3 Review Questions: Multiple Choice ............................................................................ 213 17.4 Review Exercises................................................................................................................. 214 Chapter 18 The Multiple-Alternative Decision Structure ........................................ 218 18.1 The Multiple-Alternative Decision Structure ......................................................... 219 Exercise 18.1-1 Trace Tables and Multiple-Alternative Decision Structures ........220 Exercise 18.1-2 Counting the Digits ...........................................................................................222 18.2 Review Questions: True/False ...................................................................................... 223 18.3 Review Exercises................................................................................................................. 224 Chapter 19 The Case Decision Structure ....................................................................... 227 19.1 The Case Decision Structure .......................................................................................... 228 Exercise 19.1-1 The Days of the Week ......................................................................................230 19.2 Review Questions: True/False ...................................................................................... 232 19.3 Review Exercises................................................................................................................. 233 Chapter 20 Nested Decision Control Structures .......................................................... 237 20.1 What are Nested Decision Control Structures? ..................................................... 238 Exercise 20.1-1 Trace Tables and Nested Decision Control Structures ....................239 Exercise 20.1-2 Positive, Negative or Zero? ...........................................................................241 20.2 A Mistake That You Will Probably Make! ................................................................. 242 20.3 Review Questions: True/False ...................................................................................... 246 20.4 Review Exercises................................................................................................................. 246 Chapter 21 Tips and Tricks with Decision Control Structures ............................... 250 21.1 Introduction .......................................................................................................................... 251 21.2 Choosing a Decision Control Structure ..................................................................... 251 21.3 Streamlining the Decision Control Structure ......................................................... 251 Exercise 21.3-1 “Shrinking” the Algorithm .............................................................................253 Exercise 21.3-2 “Shrinking” the PHP Script ............................................................................254 Exercise 21.3-3 “Shrinking” the Algorithm .............................................................................255 21.4 Logical Operators – to Use, or not to Use: That is the Question!.................... 258 Exercise 21.4-1 Rewriting the Code ...........................................................................................259 Exercise 21.4-2 Rewriting the Code ...........................................................................................260 21.5 Merging Two or More Single-Alternative Decision Structures ...................... 261 Exercise 21.5-1 Merging the Decision Control Structures ..............................................262 Exercise 21.5-2 Merging the Decision Control Structures ..............................................263 21.6 Replacing Two Single-Alternative Decision Structures with a Dual- Alternative One .................................................................................................................... 265 Exercise 21.6-1 “Merging” the Decision Control Structures ...........................................266 21.7 Put the Boolean Expressions Most Likely to be True First............................... 267 Exercise 21.7-1 Rearranging the Boolean Expressions ....................................................268 21.8 Converting a Case Decision Structure to a Multiple-Alternative Decision Structure, and Vice Versa ................................................................................................ 269 Exercise 21.8-1 Converting the PHP Script .............................................................................270 Exercise 21.8-2 Converting the PHP Script .............................................................................271 Exercise 21.8-3 Converting the PHP Script .............................................................................273 21.9 Converting a Multiple-Alternative Decision Structure to Nested Decision Control Structures, and Vice Versa ............................................................................. 273 Exercise 21.9-1 Converting the PHP Script .............................................................................274 Exercise 21.9-2 Converting the PHP Script .............................................................................276 21.10 Converting a Case Decision Structure to Nested Decision Control Structures, and Vice Versa .............................................................................................. 277 Exercise 21.10-1 Converting the PHP Script .............................................................................278 Exercise 21.10-2 Converting the PHP Script .............................................................................279 21.11 What is Code Indentation and Why is it so Important? ..................................... 280 21.12 Using the “From Inner to Outer” Method in Decision Control Structures . 281 21.13 Review Questions: True/False ...................................................................................... 283 21.14 Review Questions: Multiple Choice ............................................................................ 284 21.15 Review Exercises................................................................................................................. 287 Chapter 22 Flowcharts with Decision Control Structures ....................................... 295 22.1 Introduction .......................................................................................................................... 296 22.2 Converting PHP Scripts to Flowcharts ...................................................................... 296 Exercise 22.2-1 Designing the Flowchart ................................................................................297 Exercise 22.2-2 Designing the Flowchart ................................................................................298 Exercise 22.2-3 Designing the Flowchart ................................................................................299 Exercise 22.2-4 Designing the Flowchart ................................................................................301 22.3 Converting Flowcharts to PHP Scripts ...................................................................... 302 Exercise 22.3-1 Writing the PHP Script ....................................................................................303 Exercise 22.3-2 Writing the PHP Script ....................................................................................305 Exercise 22.3-3 Writing the PHP Script ....................................................................................306

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.