-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.cpp
77 lines (59 loc) · 1.53 KB
/
pattern.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
66
67
68
69
70
71
72
73
74
75
76
77
// C++ program to dynamically allocate and manage memory
#include <iostream>
using namespace std;
int * increaseStruct(int *array, int &size, int increment); //This function is used when additional memory is required
int * deleteExtra(int *array, int &size, int index); //Function remove any extra memory keeping only sufficient memory
int main() {
// Declaration of Dynammic memory
int size = 10, iter = 0; //iter (iteration) represent index value
int *array = new int[size];
char permission;
do
{
if(iter == size)
{
array = increaseStruct(array, size, 1);
}
cout << "\nEnter number " << (iter+1) << ": ";
cin >> array[iter];
iter++;
cout << "\nDo you want to enter another number(y/n)? ";
cin >> permission;
}
while(permission == 'y' || permission == 'Y');
if(iter < size - 1)
{
array = deleteExtra(array, size, iter);
}
cout << endl << endl << endl;
iter = 0;
for (int i = 0; i < size; i++)
{
cout << "\nIndex " << iter + 1 << " is " << array[iter];
iter++;
}
return 0;
}
int * increaseStruct(int *array, int &size, int increment)
{
int new_size = size + increment;
int *new_array = new int[new_size];
for (int i = 0; i < size; i++)
{
new_array[i] = array[i];
}
delete [] array;
size = new_size;
return new_array;
}
int * deleteExtra(int *array, int &size, int index)
{
int *new_array = new int[index + 1];
for (int i = 0; i < index; i++)
{
new_array[i] = array[i];
}
delete [] array;
size = index;
return new_array;
}