Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help me fix my draft code please. My solution does not pass the tests for add() nor the constructor that receives a string. Focus on

Help me fix my draft code please. My solution does not pass the tests for add() nor the constructor that receives a string. Focus on getting add() to work first. I am not adding the two blank columns between the letters. My add() should take care of two situations: 1) when you start with an empty string and add a letter (there are no blank columns before the letter) and 2) when you add a letter to an existing string (you must add the blank columns and then the letter).

Focus on adding the two blank columns between the letters. ONLY CHANGE OR MODIFY CANVAS.CPP. NO CHANGES ON CANVAS.H AND MAIN.CPP

YOU ONLY NEED TO CHANGE CANVAS.CPP

------------------canvas.cpp----------------

#include #include "canvas.h" #include

using namespace std;

int maxr;

Canvas::Canvas(int width){ _width =width; C = new char*[_width]; // setting the double pointer to point to cols for (int hop2 = 0; hop2

Canvas::Canvas(char x) { _width = 5; C = new char*[_width]; for (int j = 0; j

//spaces for(int hop1=0;hop1

C[1][2]='#'; C[2][2]='#'; C[3][2]='#';

}

if((x=='B')||(x=='b')) { //Letter B for(int x1=0;x1

if((x=='C')||(x=='c')){ //Letter C for(int x1=1;x1

}

if((x=='D')||(x=='d')){ //Letter D for(int x1=0;x1

Canvas::~Canvas() { if (C != nullptr) { for (int i = 0; i

string Canvas::to_string() { //add spaces string result; if (_width != 0) { for (int i = 0; i

void Canvas::replace(char old_char, char new_char) {//replace # with @ if (_width == 0) { return; } for (int i = 0; i

int Canvas::width() { return _width; }

// Constructor for Canvas class Canvas::Canvas(string s) { // Set the max number of rows for each character canvas maxr = 5; // Calculate width of the canvas _width = (s.length() * 5) + ((s.length()) * 2); // added space for two columns between each letter // If the input string is empty, set C to null and return if (_width == 0) { C = nullptr; return; } // Allocate memory for the 2D canvas C = new char*[_width]; for ( int j = 0; j

// Add a character to the end of the canvas void Canvas::add(char x) { // Calculate the new width of the canvas after adding a character int newWidth = _width + 7; // Allocate memory for the new canvas char** newC = new char*[newWidth]; for ( int i = 0; i

// Copy the existing canvas to the new canvas for (int i = 0; i

// Add the new character to the end of the canvas Canvas charCanvas(x); for (int i = 0; i

// Delete the old canvas and set C to the new canvas delete[] C; C = newC; _width = newWidth; }

----------------canvas.h------------

image text in transcribedimage text in transcribed

-------------main.cpp-------------------------

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

eturns the entire canvas as a single string, consisting of each row // of the canvas, followed by the newline character (' ). // If the canvas is empty, returns an empty string. string to_string(); // Replaces every instance in the canvas of old_char with new_char. // For instance, if old_char is '\#' and new char is '@', then: // / \#\#\# @@ // \#\# # // \#\#\#\#\# becomes //\#\# # // \#\# # // void replace(char old_char, char_new_char); // Destructor. Deallocates all of the memory allocated by the canvas. Canvas ( ); // Allocates a canvas containing the sequence of characters // in the string with 2 columns of space between each pair // of adjacent characters. For instance, Canvas("BADCAB") // should yield: // // \#\#\#\# \#\#\# \#\#\#\# \#\#\#\# \#\#\# \#\#\#\# // ############ // \#\#\#\# \#\#\#\#\#\# \# \# \# \# \#\#\#\#\# \#\#\#\# // \#\#\#\# \# \# \#\#\#\# \#\#\#\# \# \# \#\#\#\# /1 // Any characters in s not from { 'A', 'B', 'C', 'D' } should be // replaced with empty 55 space, just like previous constructor. // If the string is empty (null string), the canvas is empty // (pointer is NULL and _width is ) Canvas(string s); // Adds a character to the Canvas's sequence of characters. void add (char x); private: // A canvas is represented as a 20 char array, i.e. // an array of pointers to char (sub)arrays. // Each subarray corresponds to a COLUMN of the image. char** C; int_width; "u\---@. + "u \- + \ ." ()[dad 7sa// // Test Canvas(string) Canvas C12("ADD"); test ( C12.width ()==19); + "\# \# \# \# \# \# " + "\#\#\#\#\# \# ### " + "\# \# \# \# \# \# " + "\# \# \#\#\#\# \#\#\#\# ); Canvas C13("VBAD!"); test ( C13.width ()==33); test( C13.to_string ()==string(" \#\#\#\# ###### n" ) + " ###### \# # n" + " \#\#\#\# \#\#\#\#\# \# \# + " ###### \#n" + " \#\#\#\# \# ####(n"); Canvas C14("DAD CAB"); test(C14.width ()==47); + "\# ########### " + "\#\#\#\# \# \#\#\#\#\# #####(n"); cout "Assignment complete." endl

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

Describe the types of power that effective leaders employ

Answered: 1 week ago

Question

Describe how leadership styles should be adapted to the situation

Answered: 1 week ago