least significant digit first int a[] = {5, 2, 8, 1, 2, 8, 0, 2, 5, 7, 2, 4, 1, 9, 6, 3, 4, 6, 9, 1, 0, 8}; int b[] = {4, 2, 7, 8, 3, 6, 2, 5, 2, 1, 3, 5, 7, 3, 2, 5, 9, 9, 9, 1, 6, 6}; a represents the number 8019643691427520821825 addition: 5 2 8 1 2 8 0 ... 4 2 7 8 3 6 2 ... - - - - - - - result 9 4 5 0 6 4 3 ... carry 1 1 1 subtraction: 5 2 8 1 2 8 0 ... 4 2 7 8 3 6 2 ... - - - - - - - raw 1 0 1 -7 -2 1 -2 fixed 1 0 1 2 8 1 8 ... borrow 3 -1 -1 -1 small subtraction when result is negative 4 6 3 5 5 2 7 6 8 9 ------------- 2 9 6 6 5 56692 is not the right answer, it should be negative -1 -1 -1 -1 switch order to find correct answer 2 7 6 8 9 4 6 3 5 5 ------------- - 8 0 3 3 4 correct answer 2 9 6 6 5 answer subtraction produced notice that all pairs except for the first (4,5) (3,6) (3,6) (0,9) add up to 9 so subtract the digits we got from 9: (nines' complement) 9 9 9 9 9 2 9 6 6 5 ------------- 7 0 3 3 4 add one (that makes it tens' complement) 8 0 3 3 4 make negative - 8 0 3 3 4 and it is the correct answer multiply 2 7 6 8 4 6 3 5 wts 1 10 100 1000 10000 1000000 1000000 10000000 2x4 2x6 2x3 2x5 4x7 7x6 7x3 6x4 6x6 ..... 8x4 In the column whose weight is 10 to the power of N (i.e. column N) multiply and add the digits from columns (0,N) and (1,N-1) and (2,N-2) and ... and (N-1,1) and (N,0) ------------------------------------------------- pointers made two ways, one dangerous the other not: int * a = new int[99]; int * b = new int[1]; int * c = new int; a[3] = 12; good b[0] = 12; good c = 12; XXXXXX * c = 12; good dangerous int x; int * y = & x; * y = 12; same as x = 12 int * f(int x, int y) { int z = x * y; return & z; } void g(int a, int b) { int w = a * (b + 3); ... } int * h(int m, int n) { int * v = new int; * v = m + n; return v; } int main() { int * p = f(3, 4); int * q = h(3, 7); ... g(12, 5); cout << * p << "\n"; 96 printed cout << * q << "\n"; all good ... fragmentation of memory, - = unused memory location on heap a new int 5 b new int 12 c new int 3 d new int 15 e new int 4 f new int 7 delete c delete e g new int 8 aaaaabbbbbbbbbbbb---ddddddddddddddd----fffffff-- ------------------------------------------------ very basic linked list struct link // for a linked list of strings { string data; link * next; link(string s, link * n = NULL) { data = s; next = n; } }; link * L = new link("ant"); L = new link("bat", L); L = new link("cat", L); L = new link("dog", L); L = new link("emu", L); void print_whole_linked_list(link * ptr) { while (ptr != NULL) { cout << ptr->data << "\n"; ptr = ptr->next; } reverse(link * ptr) { link * rev = NULL; while (link != NULL) { link * temp = ptr; ptr = ptr->next; temp->next = rev; rev = temp; } bool find(link * ptr, string s) { while (ptr != NULL) { if (ptr->data == s) return true; ptr = ptr->next; } return false; } -------------------------------------- adapted to be a memory for numeric variables struct memlink { string var; double val; memlink * next; memlink(string a, double b, memlink * n = NULL) { var = a; val = b; next = n; } }; memlink * d = NULL; ... d = new memlink("pi", 3.14159, d); d = new memlink("x", -987.6, d); bool __________default_for_lookup____; double lookup(string v, memlink * ptr, bool & found = __________default_for_lookup____) { while (ptr != NULL) { if (ptr->var == v) { found = true; return ptr->val; } ptr = ptr->next; } found = false; return 0.0; } example use bool ok; double value = lookup("x", d, ok); if (! ok) say not found else use value double addup(memlink * ptr) { double total = 0.0; while (ptr != NULL) { total += ptr->val; ptr = ptr->next; } return total; } a more general version with a function as its parameter double reduce(memlink * ptr, double combiner(double x, double y), double init) { double total = init; while (ptr != NULL) { total = combiner(total, ptr->val); ptr = ptr->next; } return total; } example double mul(double x, double y) { return x * y; } double product = reduce(d, mul, 1.0); double apply_to_all(memlink * ptr, double operation(double x)) { while (ptr != NULL) { ptr->val = operation(ptr->val); ptr = ptr->next; } } example double add_five_percent(double x) { return x * 1.05; } apply_to_all(d, add_five_percent); double apply_to_all_update(memlink * ptr, void operation(double & x)) { while (ptr != NULL) { operation(ptr->val); ptr = ptr->next; } } example void add_ten_percent(double & x) { x *= 1.05; } apply_to_all_update(d,add_ten_percent); void print_int(double & x) { cout << x << "\n"; } apply_to_all_update(d, print_int); double reduce(memlink * ptr, double combiner(double, double), double init) { double total = init; ... { total = combiner(total, ptr->val); ... ------------------------------------------------ mutual recursion needs prototypes void g(int); void f(int x) { if (x > 10) g(x); else if (x > 0) f(x - 1); else cout << "x"; } void g(int y) { if (y > 0) g(y - 1); f(y / 2); } template class linked_list { protected: struct link { T data; link * next; link(const T & x, link * n = NULL) { data = x; next = n; } }; link * first; public: linked_list() { first = NULL; } void add_to_front(T x) { first = new link(x, first); } template T find(bool match(T, Q), Q wanted) { link * ptr = first; while (ptr != NULL) { if (match(ptr->data, wanted)) return ptr->data; ptr = ptr->next; } example struct person { string fname, lname; int ssn, zip; } linked_list D; D.add_to_front(person("Jill", "Jones", 111111111, 33333)); bool compare(person p, string s) { return p->lname == s; }