I cannot identify my mistake here, a little help would be a favour | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 3

I cannot identify my mistake here, a little help would be a favour

https://code.sololearn.com/c2jUATE9vy3n/?ref=app

1st Nov 2017, 7:44 AM
Nitin Sahu
Nitin Sahu - avatar
5 Réponses
+ 4
It usually helps to organize your code in a more readable fashion, placing variables close as possible to the point of use. I made a few changes that allow for more dynamic array lengths, as most seem to be based on the input of the user, yet you were using a preset length of 10. Also, the only major problem I could see was with your while loop. You placed the incrementation variable count within an if statement that may not be entered, leading to a possible infinite loop. If you could explain what it is exactly you're trying to accomplish with this code then I may be able to help further. You may also want to convert your global int array c into a pointer and then set the length of a new array in main and point the pointer to that array instead, allowing for a more dynamic program. #include <iostream> using namespace std; int c[10], m = 0; void push(int b){ c[m] = b; m++; } int pop(){ int p = c[m]; m--; return p; } int main() { int n; cin >> n; int a[n][n]; for(int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin>>a[i][j]; } } bool arr[n]; for(int i = 0; i < n; i++) { arr[i] = 0; } int visit; cin >> visit; int count = 0 while(count != n){ if(arr[visit] != 1){ for (int i = 0; i < n; i++) { if(a[visit][i] == 1) { push(i); } } cout<<visit; arr[visit] = 1; visit = pop(); } count++; // This was moved out of the if statement } return 0; }
1st Nov 2017, 9:42 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
yea i had forgotten to add it in the tag list, now I updated it
1st Nov 2017, 8:27 AM
Nitin Sahu
Nitin Sahu - avatar
+ 1
Unfortunately, that doesn't really tell me much about what it is supposed to do and what the desired output is for a given input and as to how it should arrive at that output, so I don't think I can be of any real further assistance here without that information.
1st Nov 2017, 10:25 AM
ChaoticDawg
ChaoticDawg - avatar
0
Please put the language of your question in the tags.
1st Nov 2017, 8:26 AM
sneeze
sneeze - avatar
0
it's a program based on graphs and trees, i.e. deep first search Algorithm and yea i made the change still image not getting the desired output @ChaoticDwag
1st Nov 2017, 10:07 AM
Nitin Sahu
Nitin Sahu - avatar