
C/C++ Programming Tutorial A A.1 Introduction This tutorial introduces a minimal subset of C/C++ that should be sufficient to complete the optional progamming exercises in this book. The programs here run from the command line—the Command Prompt applica- tion in Windows, or the Terminal application in MacOS X or other flavors of UNIX. The discussion here assumes some basic knowledge of how to use the command line. You should know at a minimum how to start and exit the command line application, and how to list files and folders (dir on Windows, and ls on MacOS X or other UNIX systems). You should also know how to parse file pathnames on your system (for example, “C:\Files\test.cpp” on Windows which means that the file test.cpp is in the top- level folder “My Files” which is on the drive labelled “C”. On MacOS X we could have “/home/myname/test.cpp” which means that test.cpp is in the folder “myname” which is in the enclosing top-level folder “home”.) C/C++ is a commonly used computer programming language. C++ is an extra layer on top of an older language called C. We will not use this extra layer except for the routines for reading and writing to the computer screen (input-output or I/O). C/C++ is a compiled language. This means that programming proceeds in two steps. First, we write the source program in C/C++. This is in a plain text file with the filename ending in the extension .cpp (e.g., test.cpp). Second, we compile the source program by running a compiler such as g++. If using g++, we could type in a command such as the following at the command prompt: g++ test.cpp -o test.exe © Springer Nature Singapore Pte Ltd. 2020 299 S. Lynn, Valuation for Accountants, Springer Texts in Business and Economics, https://doi.org/10.1007/978-981-15-0357-3 300 A C/C++ Programming Tutorial This command would read the source program test.cpp and create an executable file (a set of machine-readable instructions) called test.exe. We can directly run the file test.exe by entering its name at the command prompt: test.exe On some UNIX platforms such as MacOS X, we may need to first set the user permissions to make the file runnable, by issuing the following command at the command prompt: chmod u+x test.exe We may also need to specify the path when running the executable, as follows: ./test.exe where . is UNIX shorthand for the current directory (folder). This is because UNIX systems are often configured not to look for executables in the current directory unless this directory is specifically indicated when calling the executable. A.2 Getting the C/C++ Compiler While any C/C++ compiler should work fine with our programs, it is recommended that you get the GNU C/C++ compiler gcc (or its near-equivalent Clang). These work similarly on all major platforms. We will assume the GNU C/C++ compiler in the examples that follow. To get the GNU C/C++ compiler for Windows, you can install mingw (minimal- ist GNU for Windows), currently available at http://www.mingw.org. An alternative is Cygwin, which is currently available at http://www.cygwin.com. To install the gcc compiler on Mac OS X (actually Clang), you would install the command line tools for XCode, from the Apple developer website at http:// developer.apple.com. On other UNIX-based systems such as Linux, the GNU compiler or equivalent is typically a standard part of the platform and is likely to be pre-installed. If not pre- installed, it should be easy to install using the standard package installation process. Check your system documentation. You can also install gcc (actually Clang) on a smartphone running Android by installing the “termux” app, which produces a UNIX command prompt. Once installed, you can use the following command to install gcc: pkg install clang It is also desirable on Termux to install a user-friendly text editor such as nano on Termux to edit programs (pkg install nano). A.3 Program Format 301 A.3 Program Format All the programs in our exercises follow a consistent format. They ask the user to input various parameters for some model, say a binomial lattice model for option valuation. Then they run the model with these parameters. Finally they print out the model output, say the option value. All the action takes place at the command prompt. So to run these programs, open a command prompt window in Windows, or run the Terminal app in MacOS X (or Linux etc.). To illustrate the basic format of a program, we will start with a simple program that does nothing. Let us call this program donothing.cpp. A.3.1 The #include Directive The opening lines of the program are calls to include various standard C source files in our program so that we can use the standard routines defined in these files. We will consistently include two standard files: iostream, which defines the standard C++ I/O routines, and “math.h” which defines several math functions (in plain C) that we will use in our programs. To include a standard C/C++ source file, we use the invocation: #include <... > Task Try to write out the commands to include iostream and math.h, before looking at the solution given below: Solution #include <iostream > #include <math.h> A.3.2 The Program Body C programs are built up out of functions. A function has a standard format as follows: Type_Of_Return_Value name_of_function ( list_of_inputs ){ C/C++ statement_ending_in_semicolon ; C/C++ statement_ending_in_semicolon ; ... } Every runnable program must have a function called main. By convention, this function returns a value of type “int” or integer (a positive or negative whole number). 302 A C/C++ Programming Tutorial Task Create and compile a program donothing.cpp with our standard headers followed by an empty main function (the input list is blank and there are no C/C++ statements in the body). This must be a plain text file. So do not use a word-processor like MS Word to create the file. Use a text editor like Notepad on Windows or TextEdit on the Mac. After saving, compile and check that your program works by running the executable donothing.exe. Be careful that C/C++ is case-sensitive. This means that “main” is not the same as “Main” or “mAin” or “MAIN”. Solution (donothing.cpp) #include <iostream > #include <math.h> int main(){ } Compiler Command g++ donothing.cpp -o donothing.exe A.3.3 Comments Comments are memos to the human reader (you, the programmer) that are ignored by the compiler. Comments in C are enclosed within the symbols /∗ ...∗/. In addition, C++ allows comments that start with the symbol // and run to the end of the current line. Task Edit donothing.cpp to include C- and C++-style comments that say “This program does nothing”. You can put them anywhere in the program. Solution (donothing.cpp) #include <iostream > #include <math.h> // This program does nothing int main(){ / ∗ This program does nothing ∗ / } A.4 Printing Text to the Screen 303 A.4 Printing Text to the Screen Strings of text are enclosed in double inverted commas (double quotes) as in “hello”. These are called strings in computer programming jargon. The C++ command to print a string to the screen is: std::cout «... The prefix std signifies that cout is a command that is defined in the standard namespace (“std”). This is to avoid any conflict in case cout is defined somewhere else in your program. The items to be printed to the screen are separated by the symbol “«”. You can remember this by thinking of an open mouth spitting out something (output). The items to be printed to the screen could include strings, variables (to be discussed soon), mathematical expressions (to be discussed later), and a special command to signify end of line: std::endl. For example, if we have a variable named x and we want to print out “The value of x is: ” followed by the value of x, followed by a new line, we would include the C++ statement: std :: cout << ‘‘The value of x is : ’’ << x << std :: endl ; Do not forget the semicolon. In the body of a function, every C/C++ statement must end with a semicolon. Task Create and run a program called helloworld.cpp (executable helloworld.exe) that prints the text “Hello World!” followed by a new line. Solution (helloworld.cpp) #include <iostream > #include <math.h> int main(){ std :: cout << "Hello world !" << std :: endl ; } 304 A C/C++ Programming Tutorial A.5 Variables A variable is an item that can take on different values in the course of a program. In C/C++, although a variable can take on different values, the values must all be of the same type, for example whole number or decimal. We need to declare the type of a variable before we use that variable. We will just consider two types in our minimal subset of C/C++ (there are many others): int an integer, a whole number which could be positive or negative, e.g., -1, 2, 3, 0, 500 are all integers. double a decimal, e.g., 7.89, 0.1453, -1.0, or -1.859. To declare the type of a variable, we type int or double followed by the name of the variable and ending of course in a semicolon. For example, to declare an integer called x, we type int x; To declare a double named y, we type double y; We can declare multiple variables of the same type by separating the variable names with commas.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages39 Page
-
File Size-