1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#include <iostream>
#include <memory>
#include <string>
#include <vector>
int product (int x, int y) {
int result = x * y;
return result;
}
void product (int &result, int x, int y) { result = x * y; }
void product (int *result, int x, int y) { *result = x * y; }
int *new_product (int x, int y) {
int *result = new int;
*result = x * y;
return result;
}
std::auto_ptr<int> smart_product (int x, int y) {
std::auto_ptr<int> result(new int);
*result = x * y;
return result;
}
void array_product (int array[], int n, int factor) {
for (int i = 0; i < n; ++i) array[i] *= factor;
}
void array_product (int result[], const int array[], int n, int factor) {
for (int i = 0; i < n; ++i) result[i] = array[i] * factor;
}
struct Array { int values[10]; };
Array array_product (const int array[], int n, int factor) {
assert(n < 10);
Array result;
for (int i = 0; i < n; ++i) result.values[i] = array[i] * factor;
return result;
}
int *new_array_product (const int array[], int n, int factor) {
int *result = new int[n];
for (int i = 0; i < n; ++i) result[i] = array[i] * factor;
return result;
}
std::vector<int> vec_array_product (const int array[], int n, int factor) {
std::vector<int> result(n);
for (int i = 0; i < n; ++i) result[i] = array[i] * factor;
return result;
}
int main () {
using std::cout;
using std::endl;
cout << product(2, 1) << endl;
{
int n;
product(n, 2, 2);
cout << n << endl;
product(&n, 2, 3);
cout << n << endl;
}
{
int *p = new_product(2, 4);
cout << *p << endl;
delete p;
}
cout << *smart_product(2, 5) << endl;
int array[] = { 6, 7, 8, 9, 10 };
enum { num_elements = sizeof(array) / sizeof(array[0]) };
array_product(array, num_elements, 2);
const int array2[num_elements] = { 11, 12, 13, 14, 15 };
array_product(array, array2, num_elements, 2);
}
|