For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error.
Printing empty array -- next line should be blank
Testing append: Shouldn't crash!
Should print 100 through 110 below, with 110 on a new
line:
100
101 102 103 104 105 106 107 108 109
110
Checking capacity of new array:
OK
Append test #2:
Should print 100 through 120 below, on 3 separate lines:
100
101 102 103 104 105 106 107 108 109
110 111 112 113 114 115 116 117 118
119 120
Checking capacity of new array:
OK
Testing at:
Should print 109 103 105 100 118
109 103 105 100 118
Should print your chosen value and not crash:
-1111
Testing sum: Should print 2310
2310
Testing remove. If you don't see 'Uh-oh,' that's a good sign.
Checking capacity of new array:
OK
Eliminating elements, one by one, from array, and testing
resize.
Resize error! Removed 105, capacity is 26, should be 25
Resize error! Removed 106, capacity is 26, should be 25
Resize error! Removed 107, capacity is 26, should be 25
Resize error! Removed 108, capacity is 21, should be 20
Resize error! Removed 109, capacity is 21, should be 20
Resize error! Removed 110, capacity is 21, should be 20
Resize error! Removed 111, capacity is 17, should be 25
Resize error! Removed 112, capacity is 17, should be 25
Resize error! Removed 113, capacity is 14, should be 25
Resize error! Removed 114, capacity is 12, should be 20
Resize error! Removed 115, capacity is 10, should be 20
Resize error! Removed 116, capacity is 8, should be 20
Resize error! Removed 117, capacity is 7, should be 16
Resize error! Removed 118, capacity is 6, should be 16
Resize error! Removed 119, capacity is 5, should be 12
Resize error! Removed 120, capacity is 4, should be 12
Remove and resize test passed if no errors reported above.
Adding 152 to array. Final print should print just 152.
152
Rather than
Main.cpp #include <iostream> #include "dynamicarray.h" using namespace std; int main( ) { DynamicArray a; cout << "Printing empty array -- next line should be blank\n"; a.print(); // Loop to append 100 through 110 to a (11 elements total) cout << "\nTesting append: Shouldn't crash!\n"; // a.append(100); for (int i = 100; i < 111; i++) { a.append(i); } cout << "\nShould print 100 through 110 below, with 110 on a new line:\n"; a.print(); cout << "\nChecking capacity of new array:\n"; if (a.cap() == 20) cout << "OK\n"; else cout << "Error: cap is " << a.cap() << ", should be 20\n"; // Loop to append 111 through 120 (21 elements total): cout << "\nAppend test #2:\n"; for (int i = 111; i < 121; i++) { a.append(i); } cout << "\nShould print 100 through 120 below, on 3 separate lines:\n"; a.print(); cout << "\nChecking capacity of new array:\n"; if (a.cap() == 40) cout << "OK\n"; else cout << "Error: cap is " << a.cap() << ", should be 40\n"; //declaration of indices int indices[] = { 9, 3, 5, 0, 18 }; //Loop to access some arbitrary elements of a cout << "\nTesting at:\n"; cout << "Should print 109 103 105 100 118\n"; for (int i = 0; i < 5; i++) { cout << a.at(indices[i]) << " "; } cout << endl; // Save me from myself cout << "Should print your chosen value and not crash:\n"; cout << a.at(10000000) << endl; // Print sum cout << "\nTesting sum: Should print 2310\n"; cout << a.sum() << endl; // Test remove: // // Remove (a) 102 // // (b) 122 (should return false) cout << "\nTesting remove. If you don't see 'Uh-oh,' that's a good sign.\n"; if (!a.remove(102)) cout << "Uh-oh, can't remove 102\n"; if (a.remove(122)) cout << "Uh-oh, shouldn't be able to remove 111\n"; // More tests: first, add 102 back in a.append(102); cout << "\nChecking capacity of new array:\n"; if (a.cap() == 40) cout << "OK\n"; else cout << "Error: cap is " << a.cap() << ", should be 40\n"; cout << "\nEliminating elements, one by one, from array, and testing resize.\n"; int capacities[] = {40, // len is 20 32, 32, 32, 32, // len is 19, 18, 17, 16 25, 25, 25, // len is 15, 14, 13 20, 20, 20, // len is 12, 11, 10, 25, 25, 25, // len is 15, 14, 13 20, 20, 20, // len is 12, 11, 10, 16, 16, 12, 12, 10, // len is 9, 8, 7, 6, 5 10, 10, 10, 10, 10 // 4, 3, 2, 1, 0 }; for (int i = 0; i < 21; i++) { a.remove(100 + i); if (a.cap() != capacities[i]) { cout << "Resize error! Removed " << 100 + i << ", capacity is " << a.cap() << ", should be " << capacities[i] << endl; } } cout << "\nRemove and resize test passed if no errors reported above.\n"; cout << "\nAdding 152 to array. Final print should print just 152.\n"; a.append(152); a.print(); cout << endl; return 0; } dynamicarray.h class DynamicArray { public: DynamicArray(); ~DynamicArray(); void append(int newVal); int at(int index); int sum(); bool remove(int valToDelete); void print(); int cap() { return capacity; } private: int* arr; int len; // Number of elements actually populated int capacity; // New variable - this is the current capacity of the array }; dynamicarray.cpp #include <iostream> #include "dynamicarray.h" using namespace std; const int INITIAL_CAP = 10; DynamicArray::DynamicArray() { capacity = INITIAL_CAP; arr = new int[capacity]; len = 0; } DynamicArray::~DynamicArray() { if(arr) { delete[] arr; } } // Print all array elements void DynamicArray::print() { for (int i = 0; i < len; i++) { cout << arr[i] << " "; if (i % 9 == 0) { cout << endl; // newline every 10 elements } } cout << endl; } //Append function void DynamicArray::append(int num) { if (len < capacity) { arr[len++] = num; } else { capacity = capacity * 2; int *arr1 = new int[capacity]; for(int s=0;s<len;s++) arr1[s] = arr[s]; delete[] arr; arr = arr1; arr[len++] = num; } } int DynamicArray::at(int index) { if(index>=len) return -1111; else return arr[index]; } int DynamicArray::sum() { int sum=0; for(int s=0;s<len;s++) sum+=arr[s]; return sum; } bool DynamicArray::remove(int valToDelete) { for(int i=0;i<len;i++) { if (arr[i] == valToDelete) { for(int s=i;s<len;s++) arr[s] = arr[s+1]; len--; if (len < (capacity/2)) { float x = capacity/5; capacity = capacity-x; int *arr1 = new int[capacity]; for(int s=0;s<len;s++) arr1[s] = arr[s]; delete[] arr; arr = arr1; } return true; } } return false; }
For me, there are no compilation error occuring. Please see below screenshot. I am getting the output as mentioned by you.
Please let me know if any other issue/concern.
Hope it helps. Thanks!
Get Answers For Free
Most questions answered within 1 hours.