Question

Using C++ In a paint program, a “flood fill” fills all empty pixels of a drawing...

Using C++

In a paint program, a “flood fill” fills all empty pixels of a drawing with a given color, stopping when it reaches occupied pixels. In this exercise, you will implement a simple variation of this algorithm, flood-filling a 10 × 10 array of integers that are initially 0.

Prompt for the starting row and column.

Push the (row, column) pair on a stack.

You will need to provide a simple Pair class.

Repeat the following operations until the stack is empty:

Pop off the (row, column) pair from the top of the stack.

If it has not yet been filled, fill the corresponding array location with numbers 1, 2, 3, and so on (to show the order in which the square is filled.)

Push the coordinates of any unfilled neighbors in the north, east, south, or west direction on the stack.

When you are done, print the entire array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Given below is the code for the question. Please do rate the answer if it helped. Thank you.


#include <iostream>
#include <iomanip>
using namespace std;
const int MAX_ROWS = 10;
const int MAX_COLS = 10;
struct Pair
{
   int row;
   int col;
   Pair(int r = 0, int c = 0){
       row = r;
       col = c;
   }
};

class Stack
{
   private:
       Pair data[MAX_ROWS*MAX_COLS];
       int top;
   public:
       Stack()
       {
           top = 0;
       }
       void push(Pair p){
           data[top] = p;
           top++;
       }
      
       bool empty(){
           return top == 0;
       }
      
       Pair pop(){
           return data[--top];
       }
};

bool isColored(int pixels[][MAX_COLS], Pair p){
   if(pixels[p.row][p.col] != 0)
       return true;
   else
       return false;
}


bool isValid(Pair p){
   if(p.row >= 0 && p.row < MAX_ROWS && p.col >= 0 && p.col < MAX_COLS)
       return true;
   else
       return false;
}

int main(){
   Stack s;
   int pixels[MAX_ROWS][MAX_COLS] = {0};
   int row, col, count = 1;
   Pair p;
  
   cout << "Enter starting row (0-9): ";
   cin >> row;
   cout << "Enter starting column (0-9): ";
   cin >> col;
   s.push(Pair(row, col));
   while(!s.empty()){
       p = s.pop();
       if(!isColored(pixels, p)){
           pixels[p.row][p.col] = count;
           count++;
       }
      
       Pair north(p.row-1, p.col);
       Pair east(p.row, p.col +1 );
       Pair south(p.row+1, p.col);
       Pair west(p.row, p.col-1);
      
       if(isValid(north) && !isColored(pixels, north))
           s.push(north);
       if(isValid(east) && !isColored(pixels, east))
           s.push(east);
       if(isValid(south) && !isColored(pixels, south))
           s.push(south);
       if(isValid(west) && !isColored(pixels, west))
           s.push(west);
      
   }
  
  
   //display the pixel numbering
   for(int i = 0; i < MAX_ROWS; i++){
       for(int j = 0; j < MAX_COLS; j++){
           cout << setw(5) << pixels[i][j];
       }
       cout << endl;
   }
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Using C++ In a paint program, a “flood fill” fills all empty pixels of a drawing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • Please develop the following code using C programming and using the specific functions, instructi...

    Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...

  • import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow...

    import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...

  • import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow...

    import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...

  • Read about Cokes strategy in Africa in the article below and discuss the ethics of selling...

    Read about Cokes strategy in Africa in the article below and discuss the ethics of selling soft drinks to very poor people. Is this an issue that a company like Coke should consider? Africa: Coke's Last Frontier Sales are flat in developed countries. For Coke to keep growing, Africa is it By Duane Stanford Piles of trash are burning outside the Mamakamau Shop in Uthiru, a suburb of Nairobi, Kenya. Sewage trickles by in an open trench. Across the street,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT