Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a c++ program implementing a class that represents “ASCII art” images and

ID: 3883453 • Letter: C

Question

Create a c++ program implementing a class that represents “ASCII art” images and operations on them, respresented as two-dimensional char arrays allocated on the heap (using the new keyword).

Create a new C++ source file named canvas.cpp that implements the Canvas class, so that canvas.cpp and the provided files compile into a program that runs with no failed tests.

"simulate" a 2D array by a 1D array of pointers to 1D arrays (see the figure in Canvas.h file).

During development, your program might segfault. Be sure your IDE will tell you if your program ended due to a failed test versus a segfault.

The main.cpp and canvas.h are provided, these files are not to be edited only used to impement canvas.cpp file.

Only use basic c++ statements: if/else, for loops, basic arrays, pointers(using *), reference and dereference(using ampersand: &) ,new and delete.

main.cpp file:

canvas.h file:

Explanation / Answer

#include<string>

#include<cstdlib>

#include<iostream>

#include "canvas.h"

void Canvasa::Canvas(int Width,int height)

{

char*temp;

temp - (char*)malloc(sizeof(char)*Width);

C = (char**)malloc(sizeof(temp)*height);

}

void Canvas::Canvas(char X)

{

char*temp;

int i;

temp =(char*)malloc(sizeof(char)*5);

C = (char*)malloc(sizeof(temp)*5);

width = 5;

height = 5;

string s1,s2,s3,s4,s5;

if(X=='A')

{

s1+ = "###";

s2+ = "##";

s3+ = "#####";

s4+ = "##";

s5+ = "##";

}

else if(X=='B')

{

s1+ = "####";

s2+ = "# #";

s3+ = "#####";

s4+ = "# #";

s5+ = "####";

}

else if(X=='C')

{

s1+ = "####";

s2+ = "#";

s3+ = "";

s4+ = "#";

s5+ = "####";

}

else if(X=='D')

{

s1+ = "####";

s2+ = "# #";

s3+ = "# #";

s4+ = "# #";

s5+ = "####";

}

for(i=0;i<5;i++)

C[0][i]= s1[i];

for(i=0;i<5;i++)

C[1][i] = s2[i];

for(i=0;i<5;i++)

C[2][i] = s3[i];

for(i=0;i<5;i++)

C[3][i] = s4[i];

for(i=0;i<5;i++)

C[4][i] = s5[i];

}

void Canvas::Canvas(string S)

{

int len = S.lenght();

char*temp;

int j = 0;

temp = (char*)malloc(sizeof(char)*5*len);

C = (char**)malloc(sizeof(temp)*5);

string s1,s2,s3,s4,s5;

width = 5*len;

height = 5;

for(int i-0;i<len;i++,j=j+5)

{

char X = S[i];

if(X=='A')

{

s1+ = "###";

s2+ = "# #";

s3+ ="#####";

s4+ = "# #";

s5+ = "# #";

}

else if(X=='B')

{

s1+ = "####";

s2+ = "# #";

s3+ = "#####";

s4+ = "# #";

s5+ = "####";

}

else if(X=='C')

{

s1+ = "####";

s2+ = "#";

s3+ = "";

s4+ = "#";

s5+ = "####";

}

else if(X=='D')

{

s1+ = "####";

s2+ = "# #";

s3+ = "# #";

s4+ = "# #";

s5+ = "####";

}

else

{

s1+ = "";

s2+ = "";

s3+ = "";

s4+ = "";

s5+ = "";

}

}

for(j=o;j<len*5;j++)

C[0][j] = s1[j];

for(j=0;j<len*5;j++)

C[1][j] = s2[j];

for(j=0;j<len*5;j++)

C[2][j] = s3[j];

for(j=0;j<len*5;j++)

C[3][j] = s4[j];

for(j=0;j<len*5;j++)

C[4][j] = s5[j];

}

int Canvas::Width()

{

return Width;

}

int Canvas::Height()

{

return height;

}

sring Canvas::to_string()

{

int row = 0,col;

string ans;

while(row<height)

{

col = 0;

while(col<width)

{

if(C[row+3][col+1]=='#')

ans +='A';

else if(C[row+2][col+2]=='#')

ans +='B'

else if(C[row+2][col]=='#')

ans + = 'D';

else

ans + ='C';

col = col + 5;

}

ans + ='/n';

row = row + 5;

}

return ans;

}

void Canvas::replace(char old_char,char new_char)

{

inti,j;

for(i=0;i<width;i++)

{

if(C[i][j] == old_char)

C[i][j] = new_char;

}

}

}

void Canvas::replace(char old_char,char new_char)

{

int i,j;

for(i=0;i<width;i++)

{

for(j=0;j<height;j++)

{

if(C[i][j] == old_char)

C[i][j]=new_char;

}

}

}

Canvas::~Canvas()

{

int i;

for(i=height-1;i>=0;i--)

delete(C[i]);

width = 0;

height = 0;

}