-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays-and-dynamic-memory.cpp
65 lines (47 loc) · 1.03 KB
/
arrays-and-dynamic-memory.cpp
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
// This program passes an array to a function to manipulate, create a new array and return the pointer to the new array
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
int* arrayManip(int[], const int);
int main()
{
const int SIZE = 5;
int data[SIZE] = { 0,1,0,1,0 };
cout << "Old data: ";
for (int count = 0; count < SIZE; count++)
{
cout << data[count];
if (count != SIZE - 1)
{
cout << " - ";
}
}
cout << endl;
cout << "New data: ";
// Retrieve the newly created array from the pointer and use it
int* array = arrayManip(data, SIZE);
for (int count = 0; count < SIZE + 1; count++)
{
cout << array[count];
if (count != SIZE)
{
cout << " - ";
}
}
cout << endl;
delete[] array;
return 0;
}
int* arrayManip(int data[], const int SIZE)
{
const int NEW_SIZE = SIZE + 1;
// Create a new array and store in the pointer
int* array = new int[NEW_SIZE];
array[0] = 0;
for (int count = 0; count < SIZE; count++)
{
array[count + 1] = data[count];
}
return array;
}