Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you help me pass all test cases in c++. Please do not change the provided canvas.h file and the provided main.cpp file. You can

Can you help me pass all test cases in c++. Please do not change the provided canvas.h file and the provided main.cpp file. You can find these files below. Thank you. Only canvas.pp

image text in transcribed

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

#include #include "canvas.h" #include using namespace std;

Canvas::Canvas(int width) { _width = width; if (width == 0) { return; } C = new char*[5]; for (int i = 0; i

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

//destructor

Canvas::~Canvas() { for (int i = 0; i

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

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

#include #include #include #include "canvas.h"

using namespace std;

inline void _test(const char* expression, const char* file, int line) { cerr

#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))

int main() {

// Test Canvas(int)

Canvas C0(0); test(C0.width() == 0); test(C0.to_string() == string(""));

Canvas C1(3); test(C1.width() == 3); test(C1.to_string() == string(" ") + " " + " " + " " + " "); Canvas C2(4); test(C2.width() == 4); test(C2.to_string() == string(" ") + " " + " " + " " + " "); Canvas C3(7); test(C3.width() == 7); test(C3.to_string() == string(" ") + " " + " " + " " + " ");

// Test Canvas(char) Canvas C4('A'); test(C4.width() == 5); test(C4.to_string() == string(" ### ") + "# # " + "##### " + "# # " + "# # "); Canvas C5('B'); test(C5.width() == 5); test(C5.to_string() == string("#### ") + "# # " + "#### " + "# # " + "#### "); Canvas C6('C'); test(C6.width() == 5); test(C6.to_string() == string(" #### ") + "# " + "# " + "# " + " #### "); Canvas C7('D'); test(C7.width() == 5); test(C7.to_string() == string("#### ") + "# # " + "# # " + "# # " + "#### "); Canvas C8('E'); test(C8.width() == 5); test(C8.to_string() == string(" ") + " " + " " + " " + " ");

// Test replace() C5.replace('#', '@'); test(C5.width() == 5); test(C5.to_string() == string("@@@@ ") + "@ @ " + "@@@@ " + "@ @ " + "@@@@ "); C5.replace(' ', '-'); test(C5.width() == 5); test(C5.to_string() == string("@@@@- ") + "@---@ " + "@@@@- " + "@---@ " + "@@@@- "); C5.replace('-', '@'); test(C5.width() == 5); test(C5.to_string() == string("@@@@@ ") + "@@@@@ " + "@@@@@ " + "@@@@@ " + "@@@@@ "); C5.replace('@', '$'); test(C5.width() == 5); test(C5.to_string() == string("$$$$$ ") + "$$$$$ " + "$$$$$ " + "$$$$$ " + "$$$$$ "); C6.replace(' ', '*'); test(C6.width() == 5); test(C6.to_string() == string("*#### ") + "#**** " + "#**** " + "#**** " + "*#### "); C7.replace('#', '*'); test(C7.width() == 5); test(C7.to_string() == string("**** ") + "* * " + "* * " + "* * " + "**** "); C8.replace('#', ' '); test(C8.width() == 5); test(C8.to_string() == string(" ") + " " + " " + " " + " ");

cout

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

#ifndef CANVAS_H #define CANVAS_H

#include

using namespace std;

// In this homework, you'll manipulate ASCII art images // consisting of a rectangular grid of chararacter pixels. class Canvas { public: // Allocates a canvas of the given width and height 5 that // consists entirely of ' ' (space) chars. If the width is 0, // the canvas is empty (pointer is NULL and _width is 0) Canvas(int width);

// Allocates a canvas with width 5 and height 5 that looks like: // // ### #### #### #### // # # # # # # # // ##### or #### or # or # # // # # # # # # # // # # #### #### #### // // depending upon which character ('A', 'B', 'C', or 'D') is // given as a parameter. If some other character is given, // allocates a canvas of ' ' chars with width 5 and height 5. Canvas(char x);

// Returns the width of the canvas. int width();

// Returns 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();

private: // A canvas is represented as a 2D 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; };

#endif

image text in transcribed

In this homework, you'll implement a class that represents "ASCII art" images and operations on them, represented as two-dimensional char arrays allocated on the heap (using the new operator). The following files are given to you: 1. A C++ header file (canvas.h) declaring the Canvas class. 2. A C++ source file (main.cpp) containing a main() function with tests. Create a new C++ source file named canvas.cpp that implements the class declared in canvas.h so that canvas.cpp and the provided files compile into a program that runs with no failed tests. sh c make s test( C4. width ()==5 ) failed in file./main.cpp, line 40. signal: segmentation fault (core dumped)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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