-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA25reversedynamicarray.cpp
54 lines (42 loc) · 1.13 KB
/
A25reversedynamicarray.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
// Dynamic Array Using Pointers
// Reverse the order of the contents of an array
// By Alina Corpora and Emily Dayanghirang
#include <iostream>
using namespace std;
float* reverse(float *array1, int arraySize);
void display(float *array, int arraySize);
const int SIZE = 5;
int main()
{
float array1[SIZE] = {1,22,31,40,41};
float* ptr;
display(array1, SIZE);
// Allocate ptr to the start of an array on the heap
// Dynamically Allocate Memory
ptr = new float[SIZE];
ptr = reverse(array1, SIZE);
display(ptr,SIZE);
delete [] ptr;
return 0;
}
void display(float *array, int arraySize)
{
for(int index = 0; index < arraySize; index++)
{
cout << *(array + index) << "\t";
}
cout << endl;
}
float* reverse(float *array1, int arraySize)
{
float *array2 = nullptr;
array2 = new float[arraySize];
for (int index = 0; index < arraySize; index++)
*(array2+index) = *(array1+index);
for (size_t i = 0; i < arraySize / 2; i++) {
int x = array2[i];
array2[i] = array2[arraySize - i - 1];
array2[arraySize - i - 1] = x;
}
return array2;
}