24-311: Numerical Method C++ Programming Tutorial ====================================================================== How to compile and run a simple C++ code. 1. write/edit source code (*.cpp, *.C, *.c++, *.cxx, *.h, etc.) 2. Compile source code => object code (*.obj, *.o, etc.) => fix bugs 3. Link/build object code => executable code (*.exe, *, etc) => fix bugs 4. Run excecutable code => fix bugs Typical C++ source codes consist of: function definitions variable declarations statements -------------------------------------------------- // test01 <= this is a comment /* another form of comment */ #include #include void main(){ cout << "The volume of the box is "; cout << 11 * 9 * 40; cout << endl; cout << "The volume of the box is " << 11 * 9 * 40 << endl; cout << "The volume of the box is " << 11 * 9 * 40 << endl; printf("The volume of the box is %d \n", 11 * 9 * 40); } -------------------------------------------------- ====================================================================== How to declare variables. identifier: a name consisting of letter and digits, the first of which must be a letter, with underscore counting as a letter variable: - the name of a chunk of computer memory - each variable refers to a chunk of memory - data type: the size of the chunk and the way the bits in the chunk are interpreted character: char integer: short, int, long floating point: float, double, long double variable declaration -------------------------------------------------- void main(){ int height; int width; int length; ... } -------------------------------------------------- void main(){ int height, width, length; ... -------------------------------------------------- void main(){ int height = 5, width = 6, length = 7; ... -------------------------------------------------- ====================================================================== How to write statements that read input from your keyboard. -------------------------------------------------- #include void main(){ int height, width, length; cout << "Please type height, width, and length." << endl; cin >> height >> width >> length; cout << "The volume is " << height*width*length < #include void main(){ int h, w, l; ofstream write_file("output.txt"); write_file << "I hate Micro$oft." << endl; ifstream read_file("input.txt"); read_file >> h >> w >> l; cout << "Volume = " << h*w*l; } -------------------------------------------------- ====================================================================== How to define simple functions. DATA_TYPE FUNCTION_NAME (DATA_TYPE PARAMETER, ..., DATA_TYPE PARAMETER) { VARIABLE_DECLARATION ... VARIABLE_DECLARATION STATEMENT ... STATEMENT } -------------------------------------------------- #include void display_volume(int h, int w, int l); main(){ int height, width, length; cout << "Please type height, width, and length." << endl; cin >> height >> width >> length; display_volume(height, width, length); } void display_volume(int h, int w, int l){ int volume; volume = h*w*l; cout << "The volume is " << volume <, <, >=, <= The value of the expression using a numerical predicate can be: 0 False 1 True Use the and operator, &&, and the or operator, ||, to combine boolean expressions. the value of (2 < 3 && 3 > 4) is 0 the value of (2 < 3 || 3 > 4) is 1 A common error is to write = when you intend to check for equality. != means "Are two numbers not equal?" ! can appear alone, in which case the ! character denotes the "not" operator, e.g., the value of !0 is 1 and !1 is 0 the value of !(6 < 3) is 1 the value of !(6!=3) is 0 If you want to force the conversion of a value of one type into the corresponding value of another type, then you must "cast" the value by the following pattern: (DATA_TYPE) EXPRESSION You must cast one to machtch the type of the other if you want to compare two numbers of different types. ====================================================================== How to write conditional statements. When boolean expression of an if statement evaluates to any integer other than 0, C++ considers the expression to be true and excecute the embedded statement. if (BOOLEAN_EXPRESSION) IF_TRUE_STATEMENT else IF_FALSE_STATEMENT Compound statement { statement_1 ... statement_n } The value-producing conditional-operator expression: BOOLEAN_EXPRESSION ? IF_TRUE_EXPRESSION : IF_FALSE_EXPRESSION -------------------------------------------------- #include main(){ int change; cout << "Temparature change = "; cin >> change; cout << "The temperature has changed by " << change << (change == 1 ? "degree" : "degrees") << endl; } -------------------------------------------------- ====================================================================== How to write iterative statements. while (BOOLEAN_EXPRESSION) EMBEDDED_STATEMENT for (ENTRY_EXPRESSION; BOOLEAN_EXPRESSION; CONTINUATION EXPRESSION) EMBEDDED_STATEMENT If you want to increment or decrement the value of a variable by 1: ++VARIABLE_NAME --VARIABLE_NAME -------------------------------------------------- #include main(){ int i; for (i=0; i<5; i++) cout << i << endl; } -------------------------------------------------- ====================================================================== How to work with arrays of numbers. -------------------------------------------------- #include void main(){ const int n = 5; float v[n] = {3.0, 4.0, 5.0, 6.0, 7.0}; for(int i = 0; i < n; i++) cout << "v[" << i << "] = " << v[i] << endl; } -------------------------------------------------- #include void main(){ const int n = 3, m = 4; float v[n][m] = {{1.0, 2.0, 3.0, 4.0}, {5.0, 6.0, 7.0, 8.0}, {9.0, 10.0, 11.0, 12.0}}; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) cout << "v[" << i << "][" << j << "] = " << v[i][j] << endl; } -------------------------------------------------- #include void display_matrix(float v[3][4]); void main(){ float v[3][4] = {{1.0, 2.0, 3.0, 4.0}, {5.0, 6.0, 7.0, 8.0}, {9.0, 10.0, 11.0, 12.0}}; display_matrix(v); } void display_matrix(float v[3][4]){ for(int i = 0; i < 3; i++) for(int j = 0; j < 4; j++) cout << "v[" << i << "][" << j << "] = " << v[i][j] << endl; } --------------------------------------------------