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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include <iostream>
#include <string>
using namespace std;
void printElement(int array[], int count);
void addElement(int* array, int &size, int &count);
void exit(int* array, int &flag);
void deleteElement(int* array, int &size, int &count);
void resize(int* array,int count, int &size, int newCapacity);
int main()
{
int choice;
int count = 0;
int size = 2;
int flag = 1;
int *array = new int[size];
cout << "1. Print Elements" << endl;
cout << "2. Add Element" << endl;
cout << "3. Delete Elements" << endl;
cout << "4. Return Size" << endl;
cout << "5. Exit" << endl;
while (flag == 1)
{
cout << "Your Selection : ";
cin >> choice;
switch (choice)
{
case 1:
printElement(array, count);
break;
case 2:
addElement(array, size, count);
break;
case 3:
deleteElement(array, size, count);
break;
case 4:
cout << "Size of Aray is " << size << " and there is " << count << " Element inside" << endl;
break;
case 5:
exit(array, flag);
break;
default:
cout << "Error! Only enter 1-6 " << endl;
break;
}
}
system("pause");
return 0;
}
void printElement(int array[],int count)
{
cout << "Elements: ";
for (int i = 0; i < count; i++)
{
cout << array[i];
if (i >= 0&&i<count-1)
cout << ", ";
}
cout << endl;
}
void addElement(int* array,int &size, int &count)
{
if (count == size) // if size of array is not big enough double size
{
resize(array, count, size, size * 2);
}
int value;
int i;
cout << "Element to add: "; //input value
cin >> value;
for (i = 0; i < count; i++) // assigns value to highest position
{
if (value < array[i])
break;
}
for (int j= count - 1; j >= i; j--) //shifts all posititions to right
array[j + 1] = array[j];
array[i] = value;
count++; //counts element added
}
void deleteElement(int* array, int &size, int &count)
{
int index, value;
cout << "Element to delete: ";
cin >> value;
if (count < 1)
cout << "Error there are no values to erase in array" << endl;
else
{
for (index = 0; index <= count; index++)
{
if (array[index] == value)
break;
else if (!(array[index] == value))
{
cout << "Error " << value << " is not inside the array" << endl;
break;
}
}
if (index < count) //found the element
{
for (int i = index + 1; i < count; i++) //shift element left
array[i - 1] = array[i];
count--;
if (count < size / 2) //less than half occupied, shrink
{
resize(array,count, size, size / 2);
}
}
}
}
void exit(int* array,int &flag)
{
delete[] array;
array = NULL;
flag = 0;
}
void resize(int* array,int count, int &size, int newCapacity)
{
int* temp = new int[newCapacity];
for (int i = 0; i < size; i++)
temp[i] = array[i];
size = newCapacity;
delete[] array;
array = NULL;
temp = 0;
array = temp;
cout << "Array size expanded to " << size << endl;
}
|