how to look for the index of a vector in an array

int D[5][3] ={

{1, 0, 0},

{0, 1, 0},

{0, 0, 1},

{0, 0, 0},

{0, 0, 0},

};

int classnum=0;

int Sy[1][3]={0 ,0 ,1};


for(int i=0 ;i <5 ;i++)
{
for(int j=0 ;j < 3 ;j++)

{
if(Sy[0][j]== D[i][j])

{
classnum = i;

}
}
}
cout<<"\nclassnum = "<<classnum;

it always shows the classnum=4,it should rather be 2.what is the error i am making?
you would benefit by drawing the flow diagram and doing an step-by-step execution

Suppose that i=4, then we have
1
2
3
4
5
6
7
	for (int j = 0; j < 3; j++)
	{
		if (Sy[0][j] == D[4][j])
		{
			classnum = 4;
		}
	}
when j=0, the condition is if (Sy[0][0] == D[4][0]), replacing if( 0 == 0 ) which is true, and classnum is set to 4.


You are checking if any element matches, when you should check that they all match.
if you could help me with this...?
Um... he did.
You could use a bool variable as a flag. Set it to true at the start of each pass of the outer loop .

If any element is not equal, set the flag to false. If it is still set to true after checking all three elements, you have a match.
Topic archived. No new replies allowed.