// ============================================ // Week 7: Self-defined data types (p. 4) void vector(int x1, int y1, int x2, int y2, int& rx, int& ry) { rx = x2 - x1; ry = y2 - y1; } int main() { int x1 = 0, x2 = 0; int y1 = 10, y2 = 20; int rx = 0, ry = 0; vector (x1, y1, x2, y2, rx, ry); cout << rx << " " << ry << endl; return 0; } // ============================================ // Week 7: Self-defined data types (p. 7) Point vector (Point a, Point b) // Point as parameters { Point vecXY; vecXY.x = B.x - A.x; vecXY.y = B.y ¡V A.y; return vecXY; // return a Point } int main() { Point a = {0, 0}, b = {10, 20}; Point vecAB = vector(a, b); cout << vecAB.x << " "; cout << vecAB.y << endl; return 0; } // ============================================ // Week 7: Self-defined data types (p. 11) struct Point { int x; int y; int z; }; int main() { Point A[100]; for (int i = 0; i < 50; i++) A[i] = {20}; for (int i = 0; i < 100; i++) cout << A[i].y << " " << A[i].z << endl; return 0; } // ============================================ // Week 7: Self-defined data types (p. 12) struct Point { int x; int y; }; void reflect (Point& a) { int temp = a.x; a.x = a.y; a.y = temp; } int main() { Point a = {10, 20}; cout << a.x << " " << a.y << endl; reflect (a); cout << a.x << " " << a.y << endl; return 0; } // ============================================ // Week 7: Self-defined data types (p. 14) struct Point { int x; int y; }; int main() { Point a[10]; cout << sizeof (Point) << " " << sizeof (a) << endl; cout << &a << endl; for (int i = 0; i < 10; i++) cout << &a[i] << " " << &a[i].x << " " << &a[i].y << endl; Point* b = new Point[20]; cout << sizeof (b) << endl; delete [] b; b = NULL; return 0; } // ============================================ // Week 7: Self-defined data types (p. 23) #include #include using namespace std; int main() { clock_t sTime = clock(); for(int i = 0; i < 1000000000; i++) ; clock_t eTime = clock(); cout << sTime << " " << eTime << endl; return 0; } // ============================================ // Week 7: Self-defined data types (p. 28) struct Point { int x; int y; double distOri() { double dist = sqrt(pow(x, 2) + pow(y, 2)); return dist; } }; int main() { Point a = {3, 4}; cout << a.distOri(); return 0; } // ============================================ // Week 7: Self-defined data types (p. 29) struct Point { int x; int y; double distOri (); }; double Point::distOri () // scope resolution { // is required double dist = sqrt (pow (x, 2) + pow (y, 2)); return dist; } // ============================================ // Week 7: Self-defined data types (p. 32) struct Point { int x; int y; void reflect(); }; void Point::reflect() { int temp = x; x = y; y = temp; } int main() { Point a = {10, 20}; cout << a.x << " " << a.y << endl; a.reflect(); cout << a.x << " " << a.y << endl; return 0; }