Question: C++ Error printing out : The code prints out an extra character at the end : This is what's printing right now The correct output

C++ Error printing out : The code prints out an extra character at the end :

This is what's printing right now

C++ Error printing out : The code prints out an extra character

The correct output supposed to be this :

Expected answer: 4 as: aaaa 8 as: aaaaaaaa 12 as: aaaaaaaaaaaa 16 as: aaaaaaaaaaaaaaaa

Current code:

#include

using namespace std;

int current_size = 4;

int resize_step = 4;

//This functions add the value x at the last position in the array

//The count of elements is indicated by the variable count

//If the array is already at maximum it resizes

//The array starts at current_size size and resizes in resize_step intervals

void add_number(char*& arr, int count, char x)

{

//Your code starts here

arr = new char[current_size];

for (int i = 0; i

{

//arr[i] = x;

if (count >= current_size)

{

current_size += resize_step;

char* array2 = new char[current_size - resize_step];

for (int j = 0; j

{

array2[j] = arr[j];

}

delete[](arr);

arr = array2;

}

arr[i] = x;

}

}

int main() {

char* arr = new char[current_size];

char x = 'a';

int element = 8;

add_number(arr, element, x);

for (int i = 0; i

{

cout

}

return 0;

}

al Scope) maino CS Microsoft Visual Studio Debug Console aaaa

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!