Incidence matrix

Hello. I am challeging myself in one intresting task: I need to find out if there could be a way from one Vertex to another in Incidence matrix. So I arranged that matrix should be 4*4 and elements are going to be input by user. Bellow my test code which is doing something mystical in real life. Smbdy can you help me and tell where I am wrong and how to make this code working? I will really appreciate any advice. PS sorry for my engrish ^^
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
  #include<iostream>

int main()
{using namespace std;
int M[4][4];
int i, j;
cout<<"Fullfill the massiv: "<<'\n';
for(i=1; i<=4; i++)
{
 for(j=1; j<=4; j++)
  {
cout<<"M["<<i<<"]["<<j<<"] = ";
cin>>M[i][j];
  }
}
cout<<"enter x"<<'\n';
cin>>i;
cout<<"enter y"<<'\n';
int y;
cin>>y;
do{
 for(j=1; j<=4; j++)
 if( M[i][j]=1) {
 int m = j;
 for(i=1; i<=4; i++)
  if( M[i][m]=1) {
 int  n = i;
    for(j=1; j<=4; j++)
     M[n][j]=1;
     break;}
}}while (i!=y);
cout<<"win";
return(0);
}
All your for loops should be from index 0 to 3 instead of 1 to 4.

e.g.
for( I = 0; I < 4; i++ )
Last edited on
well the numeration indeed changed but the main thing about working not. after this change my program is not going longer y input. It just stops after this. Whats wrong then? http://clip2net.com/s/3sRDlZJ
Line 30 is the break instruction, this will quit out of the for loop regardless of the value of the variable. You can remove it as I can't see it's point in being there.

Oops, reformatted your code with indentation to make it a bit clearer. The break is probably ok, but your if statements need to use == rather than =.
Last edited on
Line 17: x is i?
Line 25 blows away the i value that you input at line 17.
Line 17 blows away the j values for the loop at line 22.
ok. I changed everything and now code is like this
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
44
45
46
47
#include<iostream>

int main()
{using namespace std;
int M[4][4];
int i, j;
cout<<"Fullfill the massiv: "<<'\n';
for(i=0; i<=3; i++)
{
 for(j=0; j<=3; j++)
  {
cout<<"M["<<i<<"]["<<j<<"] = ";
cin>>M[i][j];
 }
}
cout<<"enter x"<<'\n';
cin>>i;
cout<<"enter y"<<'\n';
int y;
int n = 0;
cin>>y;
if(i==y)
 cout<<"u are win";
else
 for(j=0; j<=3; j++) {
 n++;
 if(n==5)
 cout<<"looser";
 else {
 if(M[i][j]==1)
   {
    if(i==y)
     cout<<"win"<<'\n';
    else
     {
      if(i==3)
       i = 0;
      else
       i++;
      }
    }
else
break;
  }
}
return (0);
}
.

Something wrong with it anyway :D
Here is a pic of algorithm which I used http://clip2net.com/clip/m0/7f34f-clip-35kb.jpg?nocache=1. The magic is that algorithm works and the code is not. I guess I am wrong in using cycles. Please advice!
Last edited on
ok. I changed everything

Your loops still go from 1 to 4 instead of 0 to 3.
Here is a pic of algorithm which I used

When I click on that link it says that the picture has been archived. Can you provide another? It's hard to know what's wrong with the code when we can't tell how it's supposed to work.
I am sorry I am very new and dont know how attach img on this forum :( Try to copy the link from topic exact to the browther or here is another one on google drive https://drive.google.com/file/d/0B9Av2HTX9CbyNldIcVl0Zkc1NXlGdXgwZjdFUU54VEJlRVlZ/view?usp=sharing . Hope it will work
It's hard for me to say what the problem is. I don't understand how the algorithm you showed is supposed to work, so it's hard to say if there's a problem with the code or the algorithm itself.
Topic archived. No new replies allowed.