Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. void findcycles(int** array, char* cyc, int colsCnt, int arraylen){
  2. int ind = 0;
  3.  
  4. int *visited = new int[colsCnt];
  5. visited[colsCnt] = {0};
  6.  
  7. int *fx = new int[colsCnt];
  8. for(int i = 0; i<colsCnt; i++){
  9. fx[i] = array[1][i];
  10. }
  11.  
  12. for(int i = 1; i<colsCnt; i++){
  13. if(visited[i]!=1){
  14.  
  15. cyc[ind] = '('; //start cycle with (
  16. cout << cyc[ind] << " ";
  17. ind++;
  18. while(visited[i]!=1){ //while the current element was not visited
  19. cyc[ind] =(char) (fx[i] + '0'); // make element cycle element (cast it to char first)
  20. cout << cyc[ind] << " ";
  21. visited[i] = 1; // check out the position of the element just visited
  22. ind++;
  23. i = findInd(array, colsCnt, fx[i]); //find position of next element of cycle
  24. }
  25. cyc[ind] = ')'; // close cycle with )
  26. cout << cyc[ind] << " ";
  27. ind++;
  28. }
  29. if(visited[i] == 1){
  30. continue;
  31. }
  32.  
  33. cyc[ind] = (char)(fx[i] + '0');
  34. cout << cyc[ind] << " ";
  35. ind++;
  36. arraylen = ind;
  37. }
  38.  
  39. }
  40.  
  41. int findInd(int **array, int colsCnt, int element){
  42. for(int i = 0; i<colsCnt; i++){
  43. if(array[0][i] == element){
  44. return i;
  45. }
  46. }
  47. return -1;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement