-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA27dynamicmemoryallocation.cpp
More file actions
85 lines (70 loc) · 1.78 KB
/
A27dynamicmemoryallocation.cpp
File metadata and controls
85 lines (70 loc) · 1.78 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
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
// Dynamic memory allocation
// By Emily Dayanghirang and Alina Corpora
#include <iostream>
using namespace std;
void display(float *p, int sizeArr);
float* expandArray(float *p, int &size);
bool insertAt(float *p, float item, int index, int count);
int main()
{
int size = 5,
index = 0,
count = size;
float *ptr = nullptr,
item = 0.0;
// Initialize dynamic array
ptr = new float[size]{1.2, 2.6,
3.2,
4.7,
5.2};
cout << "The list:\n";
display(ptr, size);
cout << "\nThe floating number you want to "
<< "insert at the beginning of the list: ";
cin >> item;
ptr = expandArray(ptr, size);
if(insertAt(ptr, item, index, count))
{
cout << "\nThe new list:\n";
display(ptr, size);
}
return 0;
}
void display(float *p, int sizeArr)
{
for (int index = 0; index < sizeArr; index++)
cout << *(p + index) << " ";
cout << endl;
}
float* expandArray(float *p, int &size)
{
int newSize = size * 2;
float *newArr = nullptr;
newArr = new float[newSize];
for(int index = 0; index < size; index++)
{
// Copy the old array to the new array
*(newArr + index) = *(p + index);
}
// Set the rest of the array elements to 0.0
for(int index = size; index < newSize; index++)
{
*(newArr + index) = 0.0;
}
size = newSize;
delete [] p;
return newArr;
}
bool insertAt(float *p, float item, int index, int count)
{
if(index < 0 || index > count)
return false;
// Move contents
for(int i = count; i > index; i--)
{
*(p+i) = *(p+i-1);
}
// Insert item into the list
*(p+index) = item;
return true;
}