Lecture 1.
Logical operator for AND and OR
1. X=y=z=17 is valid, evaluate from right to left 2. C++ has integer division and floating point division 31/2/2.0 = (31/2)/2.0, integer first
} else {... } Note: compared to python, there is no : and tab requirement
...break; case 4: ...break; Case 5: ...break; ... default: .... (No break) }
}
{ Function code... }
Example: Int computeFactorial( int num ); Name should be descriptive for its purpose
Singly linked list is an art. I really like the way it designed. So efficient in terms of memory and so easy to get append and everything is dynamic.
Here is a code example for how it should work. Kind of weird when I first wrote this code. =============================================================================== #include <iostream> using namespace std; struct List { int num; List *next; }; int i; List* Create() { List *p = NULL; List *q = NULL; List *head = NULL; head = NULL; for (i=0; i<3; i++) { p = new List; cin >> p->num; if (head == NULL) { head = p; } else { q->next = p; } q=p; }; if (head != NULL) { q->next = NULL; } return head; }; void displayList(List *ll) { while (ll != NULL) { cout << ll->num << endl; ll = ll->next; } } int main() { List *head; head = Create(); displayList(head); return 0; } Introduction Multi-Dimensional array is always interesting to explore in different languages, since they are the core of scientific computing. The example code below will guide you for this tour. Implementation - 1. 1D array with pointer int * ptr = new int [num]; #include <iostream> using namespace std; int main() { // 1d pointer int *ptr = new int[3]; cout << *(ptr+1) << " " << ptr << endl; cout << "1d over "<< endl; delete ptr; =========================================================================== === Print Out === ================= 0 0xa66010 // this 0xa66010 is value of ptr without referencing id over =========================================================================== // explain: // -- // 1. ptr is the address // 2. ptr[0] does not mean (*ptr)[0], it is illegal. // ptr[0] = *(ptr+0) 2. 2D array with pointer int** ptr = new int* [num]; // 2d pointer int **p = new int*[3]; int i,j; cout << "p before fully initialized " << p << endl; cout << " the pointer below is not fully initialized, so it gives 0 as INIT-Value " << endl; for (i = 0; i < 3; i++ ) { cout << " |address/value of header-row pointer it belongs| --> " << *(p+i) <<endl; } =========================================================================== === Print Out === ================= p before fully initialized 0xa66010 the pointer below is not fully initialized, so it gives 0 as INIT-Value |address/value of header-row pointer it belongs| --> 0 |address/value of header-row pointer it belongs| --> 0 |address/value of header-row pointer it belongs| --> 0 p after fully initialized 0xa66010 // it means, p is unchanged 0 // but *(p+i) changes after fully initialized =========================================================================== for (i = 0; i < 3; i++ ) { p[i] = new int[4]; } cout << "p after fully initialized " << p << endl; cout << **p << endl; for (i = 0; i < 3; i++ ) { cout << " == " << endl; cout << "p:" << p << endl; cout << *p+i << " " << p[i] << endl; cout << "first stride: " << sizeof(int) << endl; cout << " -- " << endl; } == p:0xa66010 // still p is not changed 0xa66030 0xa66030 // the first one: (*p)+i // the second one: *(p+i) {also = p[i]} // pointer + 1, always means to the next stride location // but *(p+1), if p is a pointer as well, then it will jump to the next "row". Keep in mind that 2D array is still a 1D array in terms of memory address. first stride: 4 -- == p:0xa66010 0xa66034 0xa66050 first stride: 4 -- == p:0xa66010 0xa66038 0xa66070 first stride: 4 -- // show the 2d for (i=0; i<3;i++) { for (j=0; j<4;j++) cout << p[i][j] << " value from pointers-law --> " << *(*(p+i)+j) << " |address/value of header-row pointer it belongs| --> "<< *(p+i) << " |address/value of integer it belongs| --> " << *(p+i)+j << endl; } for (i = 0; i < 3; i++ ) { delete[] p[i]; // delete the pointer pointed to array, you need delete[] } // explain: // -- // 1. *(p+i) = p[i]: means 'i-th element of a array of pointers', intotal there are 3 element for this array // 2. p is only making sense after fully initialized, which means *p ~= an address delete[] p; // same as above, p points to the address of row-head of 2D array // =========================================================================== === Print Out === ================= 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca030 |address/value of integer it belongs| --> 0x9ca030 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca030 |address/value of integer it belongs| --> 0x9ca034 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca030 |address/value of integer it belongs| --> 0x9ca038 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca030 |address/value of integer it belongs| --> 0x9ca03c 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca050 |address/value of integer it belongs| --> 0x9ca050 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca050 |address/value of integer it belongs| --> 0x9ca054 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca050 |address/value of integer it belongs| --> 0x9ca058 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca050 |address/value of integer it belongs| --> 0x9ca05c 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca070 |address/value of integer it belongs| --> 0x9ca070 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca070 |address/value of integer it belongs| --> 0x9ca074 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca070 |address/value of integer it belongs| --> 0x9ca078 0 value from pointers-law --> 0 |address/value of header-row pointer it belongs| --> 0x9ca070 |address/value of integer it belongs| --> 0x9ca07c ============================================================================ } Important notes
It has been a while since I transfer from Fortran to C++. Fortran is very good at making easy to write HPC code. The ability of module is so easy to use. However C++ involves a lot of class and object stuff which annoys me..
1. Single file program solution However, a quick and dirty way for declaring global variable for a single file program, is just simply declare variable before the main function. 2. Multi file program solution However, I found an article talking about global variables using namespace to define a subdomain where constants can be achieved as long as you call the cpp function and the extern command for those variables. But remember, not to using global variable arrays like you do in fortran, because C++ seems to be really don't like this way..Because fortran don't have reference method to pass a large variable. Another note is that namespace shouldn't be defined after "using namespace..." Ref http://www.learncpp.com/cpp-tutorial/42-global-variables/ #include <boost/timer/timer.hpp>
int main() { boost::timer::auto_cpu_timer t; return 0; } // the cpu timer is deleted, then it counts the total time CLI: g++ -I/usr/include/x86_64-linux-gnu main.cpp -L/usr/lib/x86_64-linux-gnu -lboost_timer -lboost_system
|
AuthorShaowu Pan Archives
December 2017
Categories
All
|