-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArray_populating_using_pointers.cpp
More file actions
58 lines (42 loc) · 1.29 KB
/
Array_populating_using_pointers.cpp
File metadata and controls
58 lines (42 loc) · 1.29 KB
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
#include <iostream>
using namespace std;
//Prototype method
double *copy_and_reverse(double *a, int alength);
int main(int argc, char** argv)
{
//Declare an array of doubles and give it a length
double a[5] = {4.5, 3.3, 2.1, 1.3, 6.7};
int alength = 5;
//Call in the function that will combine the two arrays, the original and reverse into a new array double the original length
double *finalarray = copy_and_reverse(a, alength);
cout << "The combined array values are: " << endl;
//Print out the values
for(int i = 0; i < (alength * 2); i++)
{
cout << finalarray[i] << ", ";
}
//De-allocate memory
delete[] finalarray;
return 0;
}//end main
double *copy_and_reverse(double *a, int alength)
{
//Make new array length double the size of the original
int blength = (alength * 2);
//Use dynamic memory
double *b = new double[blength];
//Populate the first half of the double sized array with the original values
for(int i = 0; i < alength; i++)
{
b[i] = a[i];
}
//Use minus one because arrays start at 0
int j = alength - 1;
//Populate the second half of the double sized array with the reversed values
for(int k = alength; k < blength; k++)
{
b[k] = a[j];
j--;
}
return b;
} //End *copy_and_reverse