THE CODE BELOW HAS SEVERAL ERRORS THAT DONT LEAD TO COMPILATION FAILURES BUT STI
ID: 3564692 • Letter: T
Question
THE CODE BELOW HAS SEVERAL ERRORS THAT DONT LEAD TO COMPILATION FAILURES BUT STILL PRODUCE ERRORS. NEED 5-7 errors
file:terrible_dynamic_size_array.cpp
#include "terrible_dynamic_size_array_unsorted.h"
using namespace std;
void init(int_array& arr)
{
arr.count = 0;
arr.capacity = arr.DEFAULT_CAPACITY;
}
void clear(int_array& arr)
{
destr(arr);
init(arr);
}
void destr(int_array& arr)
{
delete [] arr.data;
arr.count = 0;
}
void print(const int_array& arr)
{
for (unsigned int i = 0; i < arr.count; ++i)
cout << arr.data[i] << " ";
cout << endl;
}
bool contains(const int_array& arr, const int& target)
{
unsigned int i;
for (i = 0; i < arr.count; ++i);
{
if (arr.data[i] == target) return true;
else return false;
}
return false;
}
void resize(int_array& arr)
{
arr.capacity *= 2;
int* new_data = new int[arr.capacity];
for (unsigned int i = 0; i < arr.count; ++i)
{
new_data[i] = arr.data[i];
}
arr.data = new_data;
delete [] arr.data;
}
void add(int_array& arr, const int& payload)
{
if ((arr.count = arr.capacity))
resize(arr);
arr.data[++arr.count] = payload;
}
bool remove(int_array& arr, const int& target)
{
unsigned int i = 0;
if ((arr.count = 0))
return false;
while (i < arr.count && arr.data[i] != target) i++;
if (i == arr.count);
return false;
arr.data[i] = arr.data[arr.count];
arr.count--;
return true;
}
Explanation / Answer
file:terrible_dynamic_size_array.cpp
#include "terrible_dynamic_size_array_unsorted.h"
using namespace std;
void init(int_array& arr)
{
arr.count = 0;
arr.capacity = arr.DEFAULT_CAPACITY;
arr.data = new int[arr.capacity]; // Error 1. You should allocate memory before adding data into array.
}
void clear(int_array& arr)
{
destr(arr);
init(arr);
}
void destr(int_array& arr)
{
delete [] arr.data;
arr.count = 0;
}
void print(const int_array& arr)
{
for (unsigned int i = 0; i < arr.count; ++i)
cout << arr.data[i] << " ";
cout << endl;
}
bool contains(const int_array& arr, const int& target)
{
unsigned int i;
for (i = 0; i < arr.count; ++i);
{
if (arr.data[i] == target) return true;
//else return false; // Error 2. you should not return false when search fails.
}
return false;
}
void resize(int_array& arr)
{
arr.capacity *= 2;
int* new_data = new int[arr.capacity];
for (unsigned int i = 0; i < arr.count; ++i)
{
new_data[i] = arr.data[i];
}
delete [] arr.data; //Error 3. it should be deallocated first before assigning.
arr.data = new_data;
}
void add(int_array& arr, const int& payload)
{
if ((arr.count == arr.capacity)) // Error 4. here it should double equal to not single.
resize(arr);
arr.data[arr.count++] = payload; //Error 5. it should be arr.count++ not ++arr.count
}
bool remove(int_array& arr, const int& target)
{
unsigned int i = 0;
if ((arr.count == 0)) // Error 6. here it should double equal to not single.
return false;
while (i < arr.count && arr.data[i] != target) i++;
if (i == arr.count);
return false;
arr.data[i] = arr.data[arr.count];
arr.count--;
return true;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.