0

hi there im a beginner in c++

well ive never used flag before and i was told that i can use flag to this:

i need to fill this shape using flag coding

well here is the code:

#include <iostream>

using namespace std;
int polygon[10][10] = 
{
    {1,1,1,1,1,1,1,1,1,1},
    {1,1,1,2,2,2,2,1,1,1},
    {1,1,1,2,1,1,2,1,1,1},
    {1,2,2,2,1,1,2,2,2,1},
    {1,2,1,1,1,1,1,1,2,1},
    {1,2,1,1,1,1,1,1,2,1},
    {1,2,1,1,1,1,1,1,2,1},
    {1,2,1,1,1,1,1,1,2,1},
    {1,2,2,2,2,2,2,2,2,1},
    {1,1,1,1,1,1,1,1,1,1},
};

int main()
{
    int row,column;
    char c='y';

    for (int row = 0; row<10; row++)
    { 
        for (int column=0; column <10; column++)
        {
            if(polygon[row][column]==1) cout << " ";
            else if(polygon[row][column]==2) 
            {
                cout << "+";
            }
            else 
                cout << " ";
        }
        cout << "\n";
    }
    system("pause");
}

this code will print out a squared shape with a top any ideas in how can i fill in this shape using the flag coding

for example in the array i can go like if row and column reached the first 2 it will start filling the shape with a 0 and when it meets the other 2 in the other side it stops and starts a new line till the shape is filled

i dont know how to use flag here i just know the concept

can anyone help please

while( polygon[row][column] == 2)
{
    row+=0;
    if (row == 2)
    {
     // in this part i need to go to the next line
    }
}

my friend gave me hints of doing something like this in order to fill the shape but i didn't quite get her

Lu Yas
  • 137
  • 2
  • 5
  • 16
  • Please format your code. Is this homework? I'm tagging it as such. – sashoalm Nov 28 '11 at 11:47
  • 9
    I'm not familiar with the concept of "flag coding". Is your code an example of that? Can you show a link? – stefaanv Nov 28 '11 at 11:52
  • please dont mix tabs and spaces, that screws up the formatting - next time. – AndersK Nov 28 '11 at 11:53
  • 4
    I'd generally suggest that if you go into Google and type a phrase (in quotes) like "flag coding" and don't get any sensible hits defining it--such as on Wikipedia...*then* it's not reasonable to expect people on the internet to know what you are talking about. Anyway, this thing I saw is in the same area of inquiry should it interest you...but a heavy-handed solution perhaps: http://stackoverflow.com/questions/8039896/having-a-matrix-mxn-of-integers-how-to-group-them-into-polygons-with-boost-geome – HostileFork says dont trust SE Nov 28 '11 at 12:28
  • The second loop you posted is an infinite loop, and the `row += 0;` statement does nothing. Well, infinite provided that `polygon[row][column] == 2`. – Brandon Buck Nov 28 '11 at 14:24

3 Answers3

0

What you can do is have a flag that says "am I in the shape?". You'll need to turn on this flag every time you encounter a border of the shape and turn it off if it is on and you encounter another border.

Edit:

The horizontal borders are a challenge. The only way to really know that using regular scanning is to scan until the end of the line and only if there's another border, you were in the shape.

If you can assume the first border you encounter is horizontal it is easier, but there's still a problem recognizing the bottom horizontal border.

You can solve that by first scanning from the top, finding the first line with a border. Now you can know there's no "in shape" area in that line. Than you can scan from the bottom finding the last line with a border. Now you can know that this line has no "in shape" area. All that is left is to scan between these two lines with the algorithm suggested before.

selalerer
  • 3,766
  • 2
  • 23
  • 33
  • In the second line, after the four border characters, are you inside the shape or not? How do you know, only taking the second line into account? – stefaanv Nov 28 '11 at 12:05
  • What im looking for was something like this while( polygon[row][column] == 2) { row+=0; if (row == 2) { // in this part i need to go to the next line } } but i dont know how to do that my class mate just gave me hints on whats the flag is like this – Lu Yas Nov 28 '11 at 14:12
  • in the second part of my question thats what i meant by flag in here it goes like if row read 2 then the 1s in there will turn to 0s till it reaches 2..i dont know if im right but thats what am trying to do – Lu Yas Nov 28 '11 at 14:19
0

What you could do assuming that a canvas must contain a simple closed polygon is: from the border, change all outside fields to '3', "flagging" them as outside fields:

- start at a corner and go round the edges (assuming polygon can be on the edge).     
    is the field 1?  
      - make it a 3.  
      -  repeat this for all (not diagonal) neighbouring fields in a recursive way.
- when printing, use a different character for 
    - 1 (inside)
    - 2 (border)
    - other (outside)
stefaanv
  • 14,072
  • 2
  • 31
  • 53
0

In programming, the term "flag" is generally represented by a boolean type. A flag, in the sense of programming, is like a switch, it's either on (true) or off (false). I notice that your shape consists of 1s and 2s and can only guess when your friend suggested that you use "flag coding" they meant that you use the bool data type instead. Alternatively, in C++ 0 will evaluate to false in a conditional (if (0) is the same as if (false)) so instead of 1 and 2 you could use 0 for 1 (meaning "not part of the shape") and 1 in place of your 2 (meaning "part of the shape).

From here, your comparisons will be a bit simpler, instead of if (polygon[row][column] == 2) you could write if (polygon[row][column]) which would only evaluate to true if the value at polygon[row][column] was non-zero.

Brandon Buck
  • 7,177
  • 2
  • 30
  • 51