
Appendix A: Code and Pseudocode A.1 Introduction The focus of this textbook is not just on teaching how to perform the computations of the mathematical tools and numerical methods that will be presented, but also on demonstrating how to implement these tools and methods as computer software. It is indeed necessary for a modern engineer to be able not just to understand the theory behind numerical methods or to use a calculator in which they are preprogrammed, but to be able to write the software to compute a method when it is not available or to verify this software when it is given. Programming has become a fundamental part of a successful engineering career. Most people today write software in a language in the C programming language family. This is a very large and diverse family that includes C++, C#, Objective-C, Java, Matlab, Python, and countless other languages. Writing out each algorithm in every language, one is likely to use would be an endless task! Instead, this book presents the algorithms in pseudocode. Pseudocode is a middle-ground between English and programming, that makes it possible to plan out a program’s structure and logical steps in a way that is understandable to humans and easily translatable to a programming language without being tied to one specific language over all others. In fact, writing out complex algorithms in pseudocode is considered an important step in software development projects and an integral part of a software system’s documentation. For example, consider a software program that takes in the length of the side of a square and computes and displays the area and perimeter of that square. The pseudocode of that program could be the one presented in Fig. A.1. Note the use of an arrow to assign values to the variables Side, Perimeter, and Area. This is done to avoid confusion with the equal sign, which could be interpreted as either an assignment or an equality test. Note as well the use of human terms for commands, such as Input and Display. These commands are used to abstract away the technical details of specific languages. This pseudocode © Springer International Publishing Switzerland 2016 307 R. Khoury, D.W. Harder, Numerical Methods and Modelling for Engineering, DOI 10.1007/978-3-319-21176-3 308 Appendix A: Code and Pseudocode Fig. A.1 Pseudocode of the square program Fig. A.2 Code of the square program in C++ (top), Matlab (middle), and Python (bottom) will never run on a computer, nor is it meant to. But it is simple to translate into a variety of programming languages: All the functions in Fig. A.2 will run in their respective programming environ- ments. Notice how the pseudocode human commands Input and Display were replaced by the language-specific commands cin and cout in C++, input and disp in Matlab, and input and print in Python, and how unique language- specific actions needed to be added to each program, such as the explicit declaration of the variable type int in C++, the square-bracketed array display in Matlab, or the Appendix A: Code and Pseudocode 309 int command to convert the user input into an integer in Python. These language- specific technical details are simplified away using pseudocode, in order to keep the reader’s focus on the “big picture,” the language-independent functionalities of the algorithm. A.2 Control Statements Control statements are programming commands that allow developers to control the execution flow of their software. A software program can have different execution paths, with each path having different commands that the computer will execute. When the execution reaches a control statement, a condition is evaluated, and one of the paths is selected to be executed based on the result of the condition. While the specific syntax that must be obeyed to use a control statement will differ a lot from one programming language to the next, there are two basic control statements that are universal to all languages in the C program- ming language family and always behave in the same way. They are the IF statement and the WHILE statement. Note that, by convention for clarity, these control statements are written in uppercase in the pseudocode. A.2.1 IF Control Statements The IF control statement evaluates an expression and, if the expression is true, it executes a set of commands; otherwise the commands are skipped entirely. It is also possible to use subsequent ELSE IF control statements to evaluate alternative conditions in the case that the first condition is false. In fact, it is possible to use an unlimited sequence of ELSE IF statements after an initial IF; each one will be evaluated in turn until one is found to be true, at which point its set of commands will be executed and all subsequent statements will be ignored. Finally, it is possible (and often recommended) to include a final ELSE statement that executes unconditionally if all preceding IF and ELSE IF statements evaluated as false. That ELSE statement defines the default behavior of the program. Consider the example pseudocode given in Fig. A.3, which will display one of the five different messages depending on the value input by the user. The control block of code begins with the initial IF word and ends at the final END IF line (this line doesn’t execute anything in the program, but is simply used to mark explicitly the end of the IF code). There are five possible paths in the program, four of which have explicit conditions that need to be evaluated, while the fifth is an unconditional default behavior. Each condition causes a unique set of commands to be run (in this example, one display command each); each set of commands is marked by being tabulated to the right and ends when the next control statement begins. Given a new user value, each condition is evaluated in turn, and as soon as one is found to be true 310 Appendix A: Code and Pseudocode Fig. A.3 Pseudocode using an IF control statement (or correct) the corresponding lines of code are executed, then the program jumps to the END IF line without checking the other conditions. That is why it is not necessary to re-check that previous conditions are false in later conditions; if the later conditions are being checked at all, then all previous conditions must have been false. For example, in the line “ELSE IF (Value < 10)”, it is not necessary to check that the value is greater than zero before displaying that the value is positive, since there has already been the line “IF (Value < 0)” which must have been evaluated as false. A negative value would have evaluated to true and led to the execution of the code corresponding to that condition and consequently would have never reached the less-than-10 evaluation. The only way a program will reach the less-than-10 line is if the value is not less than zero and not equal to 0 and not equal to 1. All C family programming languages will have the IF and ELSE control statements, and the ELSE IF control can be written in two words (as in C++ and Java), one word (ELSEIF, as in Matlab) or an abbreviation (ELIF, as in Python). The condition may be required to be between parentheses (C, C++, C#) or not (Matlab, Python). And the code to be executed might be required to be between curly brackets (C++, unless the code is exactly one line) or tabulated (Python) or require no special markers at all. The END IF termination of the block can be marked by closing the curly brackets (C++, Java) or de-tabulating the lines (Python), or by an explicit END command (Matlab). These variations are illustrated in Fig. A.4, which gives three functional implementations of the pseudocode of Fig. A.3. Finally, some languages offer alternative controls as well, namely the SWITCH-CASE control which is useful when the value of the same variable is evaluated in all conditions, and the ?:operator for cases where only two outcomes are possible. These additional controls provide fundamentally the same function- alities as the IF command, but are made available to ease code writing in some common special cases. Appendix A: Code and Pseudocode 311 Fig. A.4 IF control statement implemented in C++ (top), Matlab (middle), and Python (bottom) A.2.2 WHILE Control Statements The WHILE control statement is used to create loops in the program by executing a block of code over and over again multiple times. Just like the IF command, it will evaluate a condition and execute a block of code if that condition is true. But unlike 312 Appendix A: Code and Pseudocode Fig. A.5 Pseudocode using a WHILE control statement the IF command, once the block of code is completed, the condition will be evaluated again and, if it is still true, the block of code will run again. This will go on until the condition evaluates as false. Note that if the condition is initially false, then the block of code will not be executed even once. Consider the example pseudocode in Fig. A.5, which is meant to display sequences of numbers. The user inputs a value, and the program will display all numbers from 1 until that value. This is done by using a WHILE control statement that evaluates whether the value to display is less than the user-specified target. If it is, the code inside the WHILE is executed: the value is displayed and incremented by 1.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages25 Page
-
File Size-