1D array doubt

Feb 21, 2014 at 7:29am
Why do we use flag in a 1D array ?
Feb 21, 2014 at 9:13am
What do you mean? give a code example please.
Feb 21, 2014 at 9:14am
Well, ask yourself what a 1D array really is... It's a row of bits, isn't it?

1
2
3
4
5
  bit 1│ bit 2 │ bit 3 │ bit 4 │ bit 5 │ bit 6 │ bit 7 │ bit 8
┌──────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┐
│      │       │       │       │       │       │       │       │
│      │       │       │       │       │       │       │       │ 
└──────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┘


So, what data type do we use to represent a bit? Well, we call it a flag, it can be high or low, but it's really just a single bit.

Is this what you were looking for? If you give us some code maybe we can be more specific?
Last edited on Feb 21, 2014 at 9:14am
Feb 21, 2014 at 3:46pm
THANKS FOR YOUR CONCERN!!!!
The full code is (this full code is basically to check the equality of matrices)
:
:
void main();
{ clrscr();
int A[3][3],B[3][3],r,c;
cout<<"1st matrix rowwise";
:
:
for(r=0;r<3;r++)
{ for(c=0;c<3;c++)
{cin>>B[r][c];
}// loop to check equality
int flag=0;
for(r=0;r<3;r++)
{for(c=0;c<3;c++)
{if(a[r][c]!=B[r][c])
{flag=1;break;} //The part where my doubt starts
}
if(flag==1) break ; // this whole thing till end
}
if(flag)
cout<<"matrices r unequal ";
else
cout<<"matrices are equal";
}

Last edited on Feb 21, 2014 at 3:47pm
Feb 21, 2014 at 4:25pm
First, please use code tags when posting code.

If this really full code? What is : meant to mean here? Also, these are 2 dimentional arrays (or 3, depends on how you look at them.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void main();
{
    clrscr();
    int A[3][3],B[3][3],r,c;
    cout<<"1st matrix rowwise";
    :
    :
    for(r=0;r<3;r++)
    {
        for(c=0;c<3;c++)
        {
            cin>>B[r][c];
        }

        // loop to check equality
        int flag=0;
        for(r=0;r<3;r++)
        {
            for(c=0;c<3;c++)
            {
                //The part where my doubt starts
               if(a[r][c]!=B[r][c])
               {
                  flag=1;
                  break;
               }
            }
           
            if(flag==1) break ; // this whole thing till end 
        }
        if(flag)
           cout<<"matrices r unequal ";
        else 
           cout<<"matrices are equal";
    }
}


OK, so flag is used like a Boolean, either true or false. Basically it is set to 0 (false) and then the program starts scanning through arrays 'a' and 'B', if a mismatch is found then flag is set to 1 (true) and the loops break out.

Summed up, flag is used to break out all of the loops and retain the status of the match, if false (0) no mismatches were found, if true (1) mismatches were found.

Does this make sense now?

I'll recode it a little to make it more intuitive:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void main();
{
    clrscr();
    int A[3][3],B[3][3],r,c;
    cout<<"1st matrix rowwise";
    :
    :
    for(r=0;r<3;r++) // loop #1
    {
        for(c=0;c<3;c++)
        {
            cin>>B[r][c];
        }

        // loop to check equality
        bool found_mismatch = flase;
        for(r=0;r<3;r++) // loop # 2
        {
            for(c=0;c<3;c++) // loop #3
            {
                //The part where my doubt starts
               if(a[r][c]!=B[r][c])
               {
                  found_mismatch = true;
                  break; // exit loop #3 right NOW!
               }
            }
           
            if(found_mismatch)
            {
                break ; // exit loop #2 right NOW!
            }
        }
        if(found_mismatch)
        {
           cout<<"matrices r unequal ";
        }
        else 
        {
           cout<<"matrices are equal";
        }
    } // continue to next iteration of loop #1
}
Last edited on Feb 21, 2014 at 4:31pm
Feb 22, 2014 at 12:08pm
Thanks a lot for ur help!!!!
Last edited on Feb 22, 2014 at 12:12pm
Topic archived. No new replies allowed.