-
Notifications
You must be signed in to change notification settings - Fork 0
/
WILJ1C
136 lines (63 loc) · 2.1 KB
/
WILJ1C
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
Script started on Mon Feb 24 11:59:55 2014
admiral% cat WILJ1C.cpp
#include <iostream>
using namespace std;
int *read_Array(const int *, int);
void displayArray(const int *, int);
int main()
{
// define array size
const int SIZE = 20;
// define the array
int array1[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
, 16, 17, 18, 19, 20};
// define a pointer for the duplicate array
int *dup;
// duplicate the array
dup = read_Array(array1, SIZE);
// display the original array
cout << "Here are the original arrays contents:\n";
displayArray(array1, SIZE);
// display the new array
cout << "\nHere is the new array:\n";
displayArray(dup, SIZE + 1);
// free the dynamically allocated memory and
// set the pointer to 0
delete dup;
dup = 0;
system("pause");
return 0;
}
int *read_Array(const int *arr, int size)
{
int *newArray;
//validate the size. if 0 or a negative
// number was pass, return null
if (size <= 0)
return NULL;
const int nsize = size + 1;
// allocate a new array
newArray = new int [nsize];
// copy the array's contents to the
// new array
for (int index = size; index > 0; --index)
*(newArray + index) = *(arr + index - 1);
*newArray = 0;
// return a pointer to the new array
return newArray;
}
void displayArray(const int *arr, int size)
{
for (int index = 0; index < size; index++)
cout << *(arr + index) << " ";
cout << endl;
}
admiral% g++ wil WILJ1C.cpp
admiral% a.out WILJ1C.cpp
Here are the original arrays contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Here is the new array:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
sh: pause: not found
admiral% ^D
script done on Mon Feb 24 12:00:25 2014