Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions