68
Appendix D Companion C++ Examples D.1 Introduction It is necessary to be multilingual in computer languages today. Since C++ is often used in the OOP literature it should be useful to have C++ versions of the same code given earlier in F90. In most cases these examples have the same variable names and the line numbers are usually very close to each other. This appendix will allow you to flip from F90 examples in Chapter 4 of the main body of the text to see similar operations in C++. [ 1] #include <iostream.h> // system i/o files [ 2] #include <math.h> // system math files [ 3] main () [ 4] // Examples of simple arithmetic in C++ [ 5] f int Integer Var 1, Integer Var 2; // user inputs [ 7] int Mult Result, Div Result, Add Result [ 8] int Sub Result, Mod Result; double Pow Result, Sqrt Result; [10] cout << "Enter two integers: "; [11] cin >> Integer Var 1, Integer Var 2; [13] Add Result = Integer Var 1 + Integer Var 2; [14] cout << Integer Var 1 << " + " << Integer Var 2 << " = " [15] << Add Result << endl; [16] Sub Result = Integer Var 1 - Integer Var 2 ; [17] cout << Integer Var 1 << " - " << Integer Var 2 << " = " [18] << Sub Result << endl; [19] Mult Result = Integer Var 1 * Integer Var 2 ; cout << Integer Var 1 << " * " << Integer Var 2 << " = " [21] << Mult Result << endl; [22] Div Result = Integer Var 1 / Integer Var 2 ; [23] cout << Integer Var 1 << " / " << Integer Var 2 << " = " [24] << Div Result << endl; [25] Mod Result = Integer Var 1 % Integer Var 2; // remainder [26] cout << Integer Var 1 << " % " << Integer Var 2 << " = " [27] << Mod Result << endl; [28] Pow Result = pow ((double)integer Var 1, (double)integer Var 2); [29] cout << Integer Var 1 << " ˆ " << Integer Var 2 << " = " [30] << Pow Result << endl; [31] Sqrt Result = sqrt( (double)integer Var 1 ); [32] cout << "Square root of " << Integer Var 1 << " is " [33] << Sqrt Result << endl; [34] g // end main, Running produces: [35] // Enter two integers: 25 4 [36] // 25 + 4 = 29 [37] // 25-4 = 21 [38] // 25 * 4 = 100 [39] // 25 / 4 = 6, note integer [41] // 25 % 4 = 1 [42] // 25 ˆ 4 = 390625 [43] // Square root of 25 = 5 Figure D.1: Typical Math and Functions in C++ cfl2001 J.E. Akin 69
// system i/o files [ 3] // Examples of a simple loop in C++ [ 5] int Integer Var; [ 7] for (Integer Var = 0; Integer Var < 5; Integer Var ++) [ 8] f cout << "The loop variable is: " << Integer Var << endl; [10] g // end for [11] cout << "The final loop variable is: " << Integer Var << endl; [13] [14] g // end main // Running produces: [15] // The loop variable is: 0 [16] // The loop variable is: 1 [17] // The loop variable is: 2 [18] // The loop variable is: 3 [19] // The loop variable is: 4 // The final loop variable is: 5 <- NOTE Figure D.2: Typical Looping Concepts in C++ [ 1] #include <iostream.h> // system i/o files [ 3] // Examples of simple array indexing in C++ [ 5] int MAX = 5, loopcount; int Integer Array[5] ; [ 7] // or, int Integer Array[5] = f10, 20, 30, 40, 50 g; [ 8] Integer Array[0] = 10 ; // C arrays start at zero [10] Integer Array[1] = 20 ; Integer Array[2] = 30 ; [11] Integer Array[3] = 40 ; Integer Array[4] = 50 ; [13] for ( loopcount = 0; loopcount < MAX; loopcount ++) [14] cout << "The loop counter is: " << loopcount [15] << " with an array value of: " << Integer [16] // end for loop [17] cout << "The final loop counter is: " << loopcount << endl ; [18] [19] g // end main [21] // Running produces: [22] // The loop counter is: 0 with an array value of: 10 [23] // The loop counter is: 1 with an array value of: 20 [24] // The loop counter is: 2 with an array value of: 30 [25] // The loop counter is: 3 with an array value of: 40 [26] // The loop counter is: 4 with an array value of: 50 [27] // The final loop counter is: 5 Figure D.3: Simple Array Indexing in C++ [ 1] #include <iostream.h> // system i/o files [ 3] // Examples of relational "if" operator, via C++ [ 5] int Integer Var 1, Integer Var 2; // user inputs [ 7] cout << "\nenter two integers: "; [ 8] cin >> Integer Var 1, Integer Var 2; [10] if ( Integer Var 1 > Integer Var 2 ) [11] cout << Integer Var 1 << " is greater than " << Integer Var 2; [13] if ( Integer Var 1 < Integer Var 2 ) [14] cout << Integer Var 1 << " is less than " << Integer Var 2; [15] [16] if ( Integer Var 1 == Integer Var 2 ) [17] cout << Integer Var 1 << " is equal to " << Integer Var 2; [18] [19] g // end main [21] // Running with 25 and 4 produces: 25 4 [22] // Enter two integers: [23] // 25 is greater than 4 Figure D.4: Typical Relational Operators in C++ cfl2001 J.E. Akin 70
[ 3] // Illustrate a simple if-else logic in C++ [ 5] int Integer Var; [ 7] cout << "Enter an integer: "; [ 8] cin >> Integer Var; [10] if ( Integer Var > 5 && Integer Var < 10 ) [11] f cout << Integer Var << " is greater than 5 and less than 10" [13] << endl; g [14] else [15] f [16] cout << Integer Var << " is not greater than 5 and less than 10" [17] << endl; g // end of range of input [18] [19] g // end program main [21] // Running with 3 gives: 3 is not greater than 5 and less than 10 [22] // Running with 8 gives: 8 is greater than 5 and less than 10 Figure D.5: Typical If-Else Uses in C++ cfl2001 J.E. Akin 71
[ 3] // Examples of Logical operators in C++ [ 5] int Logic Var 1, Logic Var 2; [ 7] cout << "Enter logical value of A (1 or 0): "; [ 8] cin >> Logic Var 1; [10] cout << "Enter logical value of B (1 or 0): "; [11] cin >> Logic Var 2; [13] cout << "NOT A is " <<!Logic Var 1 << endl; [14] [15] if ( Logic Var 1 && Logic Var 2 ) [16] f [17] cout << "A ANDed with B is true " << endl; [18] g [19] else f [21] cout << "A ANDed with B is false " << endl; [22] g // end if for AND [23] [24] if ( Logic Var 1 Logic Var 2 ) [25] f [26] cout << "A ORed with B is true " << endl; [27] g [28] else [29] f [30] cout << "A ORed with B is false " << endl; [31] g // end if for OR [32] [33] if ( Logic Var 1 == Logic Var 2 ) [34] f [35] cout << "A EQiValent with B is true " << endl; [36] g [37] else [38] f [39] cout << "A EQiValent with B is false " << endl; [40] g // end if for EQV [41] [42] if ( Logic Var 1!= Logic Var 2 ) [43] f [44] cout << "A Not EQiValent with B is true " << endl; [45] g [46] else [47] f [48] cout << "A Not EQiValent with B is false " << endl; [49] g // end if for NEQV [50] [51] g // end main [52] // Running with 1 and 0 produces: [53] // Enter logical value of A (1 or 0): 1 [54] // Enter logical value of B (1 or 0): 0 [55] // NOT A is 0 [56] // A ANDed with B is false [57] // A ORed with B is true [58] // A EQiValent with B is false [59] // A Not EQiValent with B is true Figure D.6: Typical Logical Operators in C++ cfl2001 J.E. Akin 72
[ 1] // Program to find the maximum of a set of integers [ 2] #include <iostream.h> [ 3] #include <stdlib.h> // for exit [ 4] #define ARRAYLENGTH 100 [ 5] long integers[arraylength]; [ 7] // Function interface prototype [ 8] long maxint(long [], long); [10] // Main routine [11] main() f // Read in the number of integers [13] long i, n; [14] [15] cout << "Find maximum; type n: "; cin >> n; [16] if ( n > ARRAYLENGTH n < 0 ) f [17] cout << "Value you typed is too large or negative." << endl; [18] exit(1); [19] g // end if [21] for (i = 0; i < n; i++) f // Read in the user s integers [22] cout << "Integer " << (i+1) << ": "; cin >> integers[i]; cout [23] << endl; g // end for [24] cout << "Maximum: ", cout << maxint(integers, n); cout << endl; [25] g // end main [26] [27] // Find the maximum of an array of integers [28] long maxint(long input[], long input length) f [29] long i, max; [30] [31] for (max = input[0], i = 1; i < input length; i++) f [32] if ( input[i] > max ) f [33] max = input[i]; g // end if [34] g // end for [35] return(max); [36] g // end maxint // produces this result [37] // Find maximum; type n: 4 [38] // Integer 1: 9 [39] // Integer 2: 6 [40] // Integer 3: 4 [41] // Integer 4: -99 [42] // Maximum: 9 Figure D.7: Search for Largest Value in C++ cfl2001 J.E. Akin 73
[ 2] [ 3] // declare the interface prototypes [ 4] void Change ( int& Input Val); [ 5] void No Change ( int Input Val); [ 7] main () [ 8] // illustrate passing by reference and by value in C++ f [10] int Input Val; [11] cout << "Enter an integer: "; [13] cin >> Input Val; [14] cout << "Input value was " << Input Val << endl; [15] [16] // pass by value [17] No Change ( Input Val ); // Use but do not change [18] cout << "After No Change it is " << Input Val << endl; [19] // pass by reference [21] Change ( Input Val ); // Use and change [22] cout << "After Change it is " << Input Val << endl; [23] g [24] [25] void Change (int& Value) [26] f [27] // changes Value in calling code IF passed by reference [28] Value = 100; [29] cout << "Inside Change it is set to " << Value << endl; [30] g [31] [32] void No Change (int Value) [33] f [34] // does not change Value in calling code IF passed by value [35] Value = 100; [36] cout << "Inside No Change it is set to " << Value << endl; [37] g [38] // Running gives: [39] // Enter an integer: 12 [40] // Input value was 12 [41] // Inside No Change it is set to 100 [42] // After No Change it is 12 [43] // Inside Change it is set to 100 [44] // After Change it is 100 Figure D.8: Passing Arguments by Reference and by Value in C++ cfl2001 J.E. Akin 74
[ 3] // Compare two character strings in C++ [ 4] // Concatenate two character strings together [ 5] f char String1[40]; [ 7] char String2; [ 8] int length; [10] cout << "Enter first string (20 char max):"; [11] cin >> String1; [13] cout << "Enter second string (20 char max):"; [14] cin >> String2; [15] [16] // Compare [17] if (!strcmp(string1, String2) ) f [18] cout << "They are the same." << endl; [19] g else f [21] cout << "They are different." << endl; [22] g // end if the same [23] [24] // Concatenate [25] strcat(string1, String2) ; // add onto String1 [26] [27] cout << "The combined string is: " << String1 << endl; [28] length = strlen( String1 ); [29] cout << "The combined length is: " << length << endl; [30] length = strlen( String1 ); [31] [32] g // end main [33] // Running with "red" and "bird" produces: [34] // Enter first string (20 char max): red [35] // Enter second string (20 char max): bird [36] // They are different. [37] // The combined string is: redbird [38] // The combined length is: 7 [39] // But, "the red" and "bird" gives unexpected results Figure D.9: Using Two Strings in C++ [ 1] #include <iostream.h> [ 2] #include <stdlib.h> [ 3] #include <math.h> // system math files [ 4] [ 5] main() // Convert a character string to an integer in C++ [ 7] f [ 8] char Age Char[5]; int age; [10] [11] cout << "Enter your age: "; cin >> Age Char; [13] [14] // convert with intrinsic function [15] age = atoi(age Char); [16] [17] cout << "Your integer age is " << age << endl; [18] cout << "Your hexadecimal age is " << hex << age << endl; [19] cout << "Your octal age is " << oct << age << endl; [21] g // end of main [22] [23] // Running gives: [24] // Enter your age: 45 [25] // Your integer age is 45. [26] // Your hexadecimal age is 2d. [27] // Your octal age is 55. Figure D.10: Converting a String to an Integer with C++ cfl2001 J.E. Akin 75
[ 2] [ 3] // Define structures and components in C++ [ 4] [ 5] struct Person // define a person structure type f [ 7] char Name; [ 8] int Age; g; [10] [11] struct Who Where // use person type in a new structure f [13] struct Person Guest; [14] char Address[40]; [15] g; [16] [17] // Fill a record of the Who Where type components [18] main () [19] f struct Who Where Record; [21] [22] cout << "Enter your name: "; [23] cin >> Record.Guest.Name; [24] [25] cout << "Enter your city: "; [26] cin >> Record.Address; [27] [28] cout << "Enter your age: "; [29] cin >> Record.Guest.Age; [30] [31] cout << "Hello " << Record.Guest.Age << " year old " [32] << Record.Guest.Name << " in " << Record.Address << endl; [33] g [34] // Running with input: Sammy, Houston, 104 gives [35] // Hello 104 year old Sammy in Houston [36] // [37] // But try: Sammy Owl, Houston, 104 for a bug Figure D.11: Using Multiple Structures in C++ cfl2001 J.E. Akin 76