fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Array{
  4. private:
  5. int sizze,length,*ptr;
  6. public:
  7. // Creation
  8. Array(int size) {
  9. ptr=new int[size];
  10. length=0;
  11. sizze=size;
  12. }
  13. // Fill
  14. void Fill() {
  15. cout<<"enter the number of elements which you want to fill : ";
  16. int x;cin>>x;
  17. if (x>sizze or x<0) {
  18. cout<<"Invalid count (must be between 0 and "<<sizze<<")\n";
  19. return ;
  20. }
  21. for (int i=0;i<x;i++) {
  22. cout<<"enter the element "<<i+1<<" : \n";
  23. cin>>ptr[i];
  24. }
  25. }
  26. // Append : push new element in the end of array
  27. void Append(int value) {
  28. if (length==sizze) {
  29. cout<<"the array is full\n";
  30. return ;
  31. }
  32. ptr[length]=value;
  33. length++;
  34. }
  35. // insert : insert the value in index the index is 0-based
  36. void insert(int value,int index) {
  37. if (index<0 or index>=length) {
  38. cout << "Index out of bounds\n";
  39. return ;
  40. }
  41. for (int i=length-1;i>=index;i--) {
  42. ptr[i+1]=ptr[i];
  43. }
  44. ptr[index]=value;
  45. length++;
  46. }
  47. // Search return the nearst index if the value was found otherwise return -1
  48. int search(int value) {
  49. int index=-1;
  50. for (int i=0;i<length;i++) {
  51. if (ptr[i]==value) {
  52. index=i;
  53. break;
  54. }
  55. }
  56. return index;
  57. }
  58. // delete the value in index index is 0-based
  59. void Delete(int index) {
  60. if (index<0 or index>=length) {
  61. cout << "Index out of bounds\n";
  62. return ;
  63. }
  64. for (int i=index+1;i<length;i++) {
  65. ptr[i-1]=ptr[i];
  66. }
  67. length--;
  68. }
  69. void Enlarge(int new_size) {
  70. int *old=ptr;
  71. ptr=new int[new_size];
  72. for (int i=0;i<min(length,new_size);i++) {
  73. ptr[i]=old[i];
  74. }
  75. delete [] old;
  76. }
  77. int get_size() {
  78. return sizze;
  79. }
  80. int get_length() {
  81. return length;
  82. }
  83. void merge(Array &other) {
  84. int *old=ptr;
  85. int New_size=other.get_size()+sizze;sizze=New_size;
  86. ptr=new int[New_size];
  87. for (int i=0;i<length;i++)ptr[i]=old[i];
  88. delete []old;
  89. for (int i=0;i<other.get_length();i++)ptr[length++]=other.ptr[i];
  90. }
  91. void Display() {
  92. for (int i=0;i<length;i++)cout<<ptr[i]<<" ";
  93. cout<<"\n";
  94. }
  95. };
  96. int main() {
  97. Array a(5);
  98. a.Fill();
  99. a.Append(42);
  100. a.insert(99, 1);
  101. cout << "Index of 42: " << a.search(42) << "\n";
  102. a.Delete(0);
  103. a.Enlarge(10);
  104.  
  105. Array b(3);
  106. b.Append(7);
  107. b.Append(8);
  108. a.merge(b);
  109.  
  110. cout << "Size: " << a.get_size() << ", Length: " << a.get_length() << "\n";
  111. return 0;
  112. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
enter the number of elements which you want to fill : Invalid count (must be between 0 and 5)
Index out of bounds
Index of 42: 0
Size: 8, Length: 2