2) Download the paint.cpp file provided below and use recursion to solve the fol
ID: 3564455 • Letter: 2
Question
2) Download the paint.cpp file provided below and use recursion to solve the following prompt:
You are writing code for an image manipulation software. Your current goal is to create a fill bucket tool for the software (see flood fill: https://en.wikipedia.org/wiki/Flood_fill). To test the logic of your fill algorithm, you have created a 20x20 two-dimensional integer array to hold the values '0' (representing an arbitrary color) or '1' (representing a different arbitrary color) in each position of the array. Using recursion to move into other spaces, write two functions for a four-directional (up/down/left/right) fill for both values of '0' and '1' based on a selected array position (with positions ranging from 0-19 on both axes, the top left position listed as (0,0), the bottom right as (19,19), the x-axis being horizontal, and the y-axis being vertical).
void fillOne(int temp[20][20], int x, int y){
//insert recursion code here to fill spaces of '0' with '1'
}
void fillZero(int temp[20][20], int x, int y){
//insert recursion code here to fill spaces of '1' with '0'
}
To complete this prompt, the function you write must change the value at the (x,y) position passed in and call on itself to repeat this process on the positions above (x,y-1), below (x,y+1), to the left (x-1,y) and to the right (x+1,y) of the initial point (x,y). This process should continue for each of the subsequent positions passed into the function unless the position passed in does not need to be changed, stopping the recursive cycle from continuing on spaces around that point. Make sure that when initially attempting a value fill on a position with the same value as the attempted fill no changes are made. Assume the image does not wrap around (meaning the algorithm does not search array positions on the other side of the image when an image border has been reached). Remember to perform checks to prevent elements outside the array's boundaries from being called, otherwise out of bound exceptions will occur when running the program. To test your logic in certain situations, you may decide to write a custom image setting function "customPatternReset" (this function is optional).
*entirely separate means that no elements (not integers) exist in both lists
Output Format:
Provide the modified paint.cpp file and a text file with your console output created based on the following inputs:
Option 1; x = 10; y = 4
Option 2; x = 9; y = 4Option 1; x = 13; y = 13
Option 1; x = 10; y = 5
Option 2; x = 10; y = 19
Option 2; x = 9; y = 5
Also provide the entire code for Lab 3 (not just the rewritten function) along with another text file with the program's output.
PAINT
#include <iostream>
using namespace std;
void print(int temp[20][20]);
void defaultPatternReset(int temp[20][20]);
void customPatternReset(int temp[20][20]); //optional
void fillOne(int temp[20][20], int x, int y);
void fillZero(int temp[20][20], int x, int y);
void fillOne(int temp[20][20], int x, int y){
//insert recursion code here to fill spaces of '0' with '1'
}
void fillZero(int temp[20][20], int x, int y){
//insert recursion code here to fill spaces of '1' with '0'
}
void customPatternReset(int temp[20][20]){
//optional
}
int main() {
int screen[20][20] = { 0 };
defaultPatternReset(screen);
cout << "Initial conditions:" << endl;
print(screen);
while(true){
int choice = -1, x = -1, y = -1;
cout << " Choose one of the following options:" << endl;
cout << "1) Fill 1 at/from a specified point." << endl;
cout << "2) Fill 0 at/from a specified point." << endl;
cout << "3) Reset to the default pattern." << endl;
cout << "4) Reset to a custom pattern (optional)." << endl;
cout << "5) Exit." << endl;
cout << " Your choice: ";
cin >> choice;
if(choice == 5)
break;
else if(choice == 1 || choice == 2){
cout << "X value: ";
cin >> x;
cout << "Y value: ";
cin >> y;
if(x > -1 && x < 20 && y > -1 && y < 20)
choice == 1 ? fillOne(screen, x, y) : fillZero(screen, x, y);
else{
cout << "Error: This position is out of bounds." << endl;
continue;
}
}
else if(choice == 3)
defaultPatternReset(screen);
else if(choice == 4)
customPatternReset(screen);
if(choice > 0 && choice < 5)
print(screen);
else
cout << "Invalid selection." << endl;
}
return 0;
}
void print(int temp[20][20]){
cout << " ";
for(int i = 0; i < 20; i++)
cout << i%10;
cout << endl;
for(int y = 0; y < 20; y++){
cout << " " << y%10 << " ";
for(int x = 0; x < 20; x++)
cout << temp[x][y];
}
cout << endl;
}
void defaultPatternReset(int temp[20][20]){
for(int i = 0; i < 20; i++)
for(int j = 0; j < 20; j++)
temp[i][j] = 0;
for(int i = 0; i < 20; i++){
temp[i][i] = 1;
temp[i][19-i] = 1;
}
for(int i = 0; i < 2; i++){
temp[i+9][7] = 1;
temp[i+9][12] = 1;
temp[7][i+9] = 1;
temp[12][i+9] = 1;
}
}
Explanation / Answer
#include <iostream>
using namespace std;
void print(int temp[20][20]);
void defaultPatternReset(int temp[20][20]);
void customPatternReset(int temp[20][20]); //optional
void fillOne(int temp[20][20], int x, int y);
void fillZero(int temp[20][20], int x, int y);
void fillOne(int temp[20][20], int x, int y){
//insert recursion code here to fill spaces of '0' with '1'
int xCoord[]={0,-1,0,1,0}, yCoord[]={0,0,1,0,-1};
if(temp[y][x]==0){
temp[y][x] = 1;
for(int i = 1; i <= 4; i++)
if(temp[y+yCoord[i]][x+xCoord[i]]==0 && xCoord[i]+x>-1 && xCoord[i]+x<20 && yCoord[i]+y>-1 && yCoord[i]+y<20)
fillOne(temp, xCoord[i]+x, yCoord[i]+y);
}
}
void fillZero(int temp[20][20], int x, int y){
//insert recursion code here to fill spaces of '1' with '0'
int xCoord[]={0,-1,0,1,0}, yCoord[]={0,0,1,0,-1};
if(temp[y][x]==1){
temp[y][x] = 0;
for(int i = 1; i <= 4; i++)
if(temp[y+yCoord[i]][x+xCoord[i]]==1 && xCoord[i]+x>-1 && xCoord[i]+x<20 && yCoord[i]+y>-1 && yCoord[i]+y<20)
fillZero(temp, xCoord[i]+x, yCoord[i]+y);
}
}
void customPatternReset(int temp[20][20]){
//optional
}
int main() {
int screen[20][20] = { 0 };
defaultPatternReset(screen);
cout << "Initial conditions:" << endl;
print(screen);
while(true){
int choice = -1, x = -1, y = -1;
cout << " Choose one of the following options:" << endl;
cout << "1) Fill 1 at/from a specified point." << endl;
cout << "2) Fill 0 at/from a specified point." << endl;
cout << "3) Reset to the default pattern." << endl;
cout << "4) Reset to a custom pattern (optional)." << endl;
cout << "5) Exit." << endl;
cout << " Your choice: ";
cin >> choice;
if(choice == 5)
break;
else if(choice == 1 || choice == 2){
cout << "X value: ";
cin >> x;
cout << "Y value: ";
cin >> y;
if(x > -1 && x < 20 && y > -1 && y < 20)
choice == 1 ? fillOne(screen, x, y) : fillZero(screen, x, y);
else{
cout << "Error: This position is out of bounds." << endl;
continue;
}
}
else if(choice == 3)
defaultPatternReset(screen);
else if(choice == 4)
customPatternReset(screen);
if(choice > 0 && choice < 5)
print(screen);
else
cout << "Invalid selection." << endl;
}
return 0;
}
void print(int temp[20][20]){
cout << " ";
for(int i = 0; i < 20; i++)
cout << i%10;
cout << endl;
for(int y = 0; y < 20; y++){
cout << " " << y%10 << " ";
for(int x = 0; x < 20; x++)
cout << temp[y][x];
}
cout << endl;
}
void defaultPatternReset(int temp[20][20]){
for(int i = 0; i < 20; i++)
for(int j = 0; j < 20; j++)
temp[i][j] = 0;
for(int i = 0; i < 20; i++){
temp[i][i] = 1;
temp[i][19-i] = 1;
}
for(int i = 0; i < 2; i++){
temp[i+9][7] = 1;
temp[i+9][12] = 1;
temp[7][i+9] = 1;
temp[12][i+9] = 1;
}
}
Option 1; x = 10; y = 4
01234567890123456789
0 11111111111111111111
1 01111111111111111110
2 00111111111111111100
3 00011111111111111000
4 00001111111111110000
5 00000111111111100000
6 00000011111111000000
7 00000001111110000000
8 00000000100100000000
9 00000001011010000000
0 00000001011010000000
1 00000000100100000000
2 00000001011010000000
3 00000010000001000000
4 00000100000000100000
5 00001000000000010000
6 00010000000000001000
7 00100000000000000100
8 01000000000000000010
9 10000000000000000001
Option 2; x = 9; y = 4
01234567890123456789
0 00000000000000000000
1 00000000000000000000
2 00000000000000000000
3 00000000000000000000
4 00000000000000000000
5 00000000000000000000
6 00000000000000000000
7 00000000000000000000
8 00000000000000000000
9 00000001011010000000
0 00000001011010000000
1 00000000100100000000
2 00000001011010000000
3 00000010000001000000
4 00000100000000100000
5 00001000000000010000
6 00010000000000001000
7 00100000000000000100
8 01000000000000000010
9 10000000000000000001
Option 1; x = 13; y = 13
01234567890123456789
0 00000000000000000000
1 00000000000000000000
2 00000000000000000000
3 00000000000000000000
4 00000000000000000000
5 00000000000000000000
6 00000000000000000000
7 00000000000000000000
8 00000000000000000000
9 00000001011010000000
0 00000001011010000000
1 00000000100100000000
2 00000001011010000000
3 00000010000001000000
4 00000100000000100000
5 00001000000000010000
6 00010000000000001000
7 00100000000000000100
8 01000000000000000010
9 10000000000000000001
Option 1; x = 10; y = 5
01234567890123456789
0 11111111111111111111
1 11111111111111111111
2 11111111111111111111
3 11111111111111111111
4 11111111111111111111
5 11111111111111111111
6 11111111111111111111
7 11111111111111111111
8 11111111111111111111
9 11111111111111111111
0 11111111111111111111
1 11111111100111111111
2 11111111011011111111
3 11111110000001111111
4 11111100000000111111
5 11111000000000011111
6 11110000000000001111
7 11100000000000000111
8 11000000000000000011
9 10000000000000000001
Option 2; x = 10; y = 19
01234567890123456789
0 11111111111111111111
1 11111111111111111111
2 11111111111111111111
3 11111111111111111111
4 11111111111111111111
5 11111111111111111111
6 11111111111111111111
7 11111111111111111111
8 11111111111111111111
9 11111111111111111111
0 11111111111111111111
1 11111111100111111111
2 11111111011011111111
3 11111110000001111111
4 11111100000000111111
5 11111000000000011111
6 11110000000000001111
7 11100000000000000111
8 11000000000000000011
9 10000000000000000001
Option 2; x = 9; y = 5
01234567890123456789
0 00000000000000000000
1 00000000000000000000
2 00000000000000000000
3 00000000000000000000
4 00000000000000000000
5 00000000000000000000
6 00000000000000000000
7 00000000000000000000
8 00000000000000000000
9 00000000000000000000
0 00000000000000000000
1 00000000000000000000
2 00000000011000000000
3 00000000000000000000
4 00000000000000000000
5 00000000000000000000
6 00000000000000000000
7 00000000000000000000
8 00000000000000000000
9 00000000000000000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.