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: 3882919 • 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. Submit the source file canvas.cpp

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, while loops, basic arrays and pointers(*).

main.cpp file:

canvas.h file:

Explanation / Answer

#include <string>

#include <cstdlib>

#include <iostream>

#include "canvas.h"

void Canvas :: 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.length();

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=0;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;

}

string 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 += ' ';

row = row + 5;

}

return ans;

}

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;

}