find cycle in array

if i have an array of 8 integers every element in any index point to the next index like if we have 2 in index 0 so it point to index 2
cycle defined here to start at index and finish in it
for example: index 3 contain number 5 --> index 5 contain number 3
this is a cycle
output should be the indexes : 3 , 5
how i can built this code using only arrays and loops (no functions only cstdlib and iostream)
thank you
You have defined a cycle, but you haven't actually said what "this code" is supposed to do.
Find any cycle?
Find all cycles?
Find the shortest cycle?

Also, give sample input and output.

One way of detecting cycles is using a std::set (the attempted insertion of an element already there would indicate a cycle). Are you allowed to use std::set?

Basically, you haven't given enough information.
i have to find the first cycle i meet in the array
and im only allowed to use std::cout , cin, endl
no any other
also only with loops (for, while...) and the array.
From a starting position, follow the chain until you either come back to the start (chain) or have visited all the elements or re-visit a non-start position.
Topic archived. No new replies allowed.