Advertisement
Guest User

Untitled

a guest
Sep 12th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7. int size = 2;
  8. int* arr;
  9. arr = new int[size];
  10. char userInput= 'z';
  11. int element= 0;
  12. int index = 0;
  13.  
  14.  
  15. for (int i = 0; i < 2; i++) {
  16. arr[i] = NULL;
  17. }
  18. while (userInput != 'e') {
  19. char userInput = 'z';
  20. cout << "(p): Print array" << endl;
  21. cout << "(a): Add Element" << endl;
  22. cout << "(d): Delete Element" << endl;
  23. cout << "(r): Return size" << endl;
  24. cout << "(e): Exit" << endl;
  25. cout << endl;
  26. cout << "Enter Option: ";
  27.  
  28. cin >> userInput;
  29. switch (userInput) {
  30. case 'p':
  31. case 'P':
  32. if (index == 0) {
  33. cout << "No Elements"<<endl;
  34. }
  35. else
  36. {
  37. for (int i = 0; i < element; i++) {
  38. cout << arr[i] << ", ";
  39. }
  40. cout << endl;
  41. }
  42.  
  43.  
  44.  
  45. //for (int i = 0; i < get)
  46.  
  47. break;
  48.  
  49. case 'a':
  50. case 'A':
  51.  
  52. int x;
  53. cout << "Enter Element: ";
  54. cin >> x;
  55.  
  56. if (index == size) {
  57. int * temp;
  58. temp = new int[size=size*2];
  59. for (int i = 0; i < size; i++) {
  60. temp[i] = arr[i];
  61.  
  62. // i think this is the code to copy the array
  63.  
  64. }
  65. delete[] arr;
  66. temp[index] = x;
  67.  
  68. index++;
  69. arr = temp; //and have the arr pointer now point to the new array;
  70. element++;
  71.  
  72. cout << "Array expanded" << endl;
  73.  
  74. }
  75. else {
  76. arr[index] = x;
  77. index++;
  78. element++;
  79.  
  80. }
  81.  
  82.  
  83.  
  84. break;
  85.  
  86. case 'd':
  87. case 'D':
  88. cout << "Enter Element: ";
  89. int value;
  90. cin >> value;
  91.  
  92. for (int i = 0; i < size; i++)
  93. {
  94. if (arr[i] == value)
  95.  
  96. {
  97.  
  98. index = i;
  99. int * temp;
  100. temp = new int[size];
  101. for (int i = 0; i < size; i++) {
  102.  
  103.  
  104. if (index <= i) {
  105. temp[i] = arr[i+1];
  106. }
  107. else {
  108. temp[i] = arr[i];
  109. }
  110.  
  111.  
  112.  
  113. }
  114.  
  115. delete[] arr;
  116. arr = temp;
  117. element = element -1;
  118. index=element;
  119.  
  120.  
  121. }
  122.  
  123.  
  124.  
  125. }
  126.  
  127.  
  128. break;
  129.  
  130. case 'r':
  131. case 'R':
  132. cout << "S: "<< size << ", "<<"E: "<<element<<endl;
  133.  
  134.  
  135. break;
  136.  
  137. case 'e':
  138. case 'E':
  139. cout << "Exiting" << endl;
  140. delete[] arr; //delallocation to orevent memory leak
  141.  
  142. exit(0);
  143. break;
  144.  
  145.  
  146.  
  147. for (int i = 0; i < size; i++) {
  148. if ((arr[i]) != NULL) {
  149. element = element + 1;
  150. }
  151. } //checks the number of elements
  152. if (element <= size/2) {
  153. int * temp;
  154. temp = new int[size = size / 2];
  155. for (int i = 0; i < size; i++) {
  156. temp[i] = arr[i];
  157.  
  158. // i think this is the code to copy the array
  159.  
  160. }
  161. delete[] arr;
  162. arr = temp; //and have the arr pointer now point to the new array;
  163.  
  164. cout << "Array shrunk" << endl;
  165. }
  166. }
  167. }
  168.  
  169. return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement