PD 10 SelfType
PD 10 SelfType
// ============================================
// Self-defined data types (p. 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] = {i};
for(int i = 0; i < 100; i++)
cout << A[i].x << " " << A[i].y
<< " " << A[i].z << "\n";
return 0;
}
// ============================================
// 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 << "\n";
reflect(a);
cout << a.x << " "
<< a.y << "\n";
return 0;
}
// ============================================
// Self-defined data types (p. 14)
struct Point {
int x;
int y;
};
int main() {
Point a[10];
cout << sizeof(Point) << " " << sizeof(a) << "\n";
cout << &a << "\n";
for(int i = 0; i < 10; i++)
cout << &a[i] << " " << &a[i].x << " " << &a[i].y << "\n";
Point* b = new Point[20];
cout << sizeof(b) << "\n";
delete [] b;
b = NULL;
return 0;
}
// ============================================
// Self-defined data types (p. 22)
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
clock_t sTime = clock();
for(int i = 0; i < 1000000000; i++)
;
clock_t eTime = clock();
cout << sTime << " " << eTime << "\n";
return 0;
}
// ============================================
// Self-defined data types (p. 28)
struct Point
{
int x;
int y;
double distOri()
{
return sqrt(pow(x, 2) + pow(y, 2));
}
};
int main()
{
Point a = {3, 4};
cout << a.distOri();
return 0;
}
// ============================================
// Self-defined data types (p. 29)
struct Point
{
int x;
int y;
double distOri();
};
double Point::distOri() // scope resolution
{ // is required
return sqrt(pow(x, 2) + pow(y, 2));
}
// ============================================
// Self-defined data types (p. 32)
struct Point
{
int x;
int y;
void reflect();
void print();
};
void Point::reflect()
{
int temp = x;
x = y;
y = temp;
}
void Point::print()
{
cout << x << " " << y << "\n";
}
int main()
{
Point a = {10, 20};
Point b = {5, 2};
a.print();
b.print();
a.reflect();
a.print();
b.print();
return 0;
}
// ============================================
// Self-defined data types (p. 37)
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int rn = 0;
for(int i = 0; i < 10; i++)
{
rn = rand();
cout << rn << " ";
}
return 0;
}
// ============================================
// Self-defined data types (p. 39)
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
srand(0);
int rn = 0;
for(int i = 0; i < 10; i++)
{
rn = rand();
cout << rn << " ";
}
return 0;
}
// ============================================
// Self-defined data types (p. 41)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(nullptr)); // good
int rn = 0;
for(int i = 0; i < 10; i++)
{
rn = rand();
cout << rn << " ";
}
return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int rn = 0;
for(int i = 0; i < 10; i++)
{
srand(time(nullptr)); // bad
rn = rand();
cout << rn << " ";
}
return 0;
}
// ============================================
// Self-defined data types (p. 42)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int rn = 0;
for(int i = 0; i < 10; i++)
{
rn = ((rand() % 10)) + 100;
cout << rn << " ";
}
return 0;
}
// ============================================
// Self-defined data types (p. 43)
struct Randomizer
{
int a;
int b;
int c;
int cur;
int rand();
};
int Randomizer::rand()
{
cur = (a * cur + b) % c;
return current;
}
// ============================================
// Self-defined data types (p. 44)
int main()
{
Randomizer r1 = {10, 4, 31, 0};
for(int i = 0; i < 10; i++)
cout << r1.rand() << " ";
cout << "\n";
Randomizer r2 = {10, 7, 32, 0};
for(int i = 0; i < 10; i++)
cout << r2.rand() << " ";
return 0;
}