-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicIntArray.cpp
More file actions
90 lines (72 loc) · 1.59 KB
/
Copy pathDynamicIntArray.cpp
File metadata and controls
90 lines (72 loc) · 1.59 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
86
87
88
//
// Simple_Pointers.cpp
// CommandLineTool
//
// Created by Csabi on 29/03/2018.
// Copyright © 2018 Tom Mitchell. All rights reserved.
//
#include <iostream>
#include "DynamicIntArray.hpp"
using namespace std;
Array::Array(){
ptrArr = new int [0];
size = 0;
nextIndex = 0;
}
Array::~Array(){
delete [] ptrArr;
}
void Array::add(int itemValue){
size++;
if (nextIndex == size){
int *tempArray = new int[size+1];
for (int i = 0; i<size; i++){
tempArray[i] = ptrArr [i];
}
delete [] ptrArr;
ptrArr = tempArray;
ptrArr[nextIndex] = itemValue;
nextIndex++;
}
else{
ptrArr[nextIndex] = itemValue;
nextIndex++;
}
}
void Array::removeLast() {
int* tempArray= new int[size-1];
for (int i =0; i<size-1; i++) {
tempArray[i] = ptrArr[i];
}
delete [] ptrArr;
ptrArr = tempArray;
size--;
nextIndex--;
}
int Array::getSize() {
return size;
}
int Array::get(int index) {
if (index >= nextIndex) {
cout<< "out of range, requested index doesnt exits";
return 0;
}
else {
return ptrArr[index];
}
}
bool Array::testArray(int n) {
Array* testArray = new Array();
if (testArray->getSize() == 0) {
testArray->add(n);
if (testArray->getSize() == 1) {
if (testArray->nextIndex == 1) {
if (testArray->get(0) == n) {
delete testArray;
return true;
}
}
}
}
return false;
}