Question: Please complete using C++, using only arrays, for and while loops. Please explain the code if possible, since I have already written a version of

Please complete using C++, using only arrays, for and while loops. Please explain the code if possible, since I have already written a version of the solution but want to understand how this can be done more efficiently. Answer with a good explanation will get a thumbs up!

Please complete using C++, using only arrays, for and while loops. Please

Program 2 - Cycles Imagine you had a travel scheme where by for every city you visited you had a mapping of what city you would travel to next. You are curious what is the longest chain (or cycle) of cities you could visit before returming to where you started. For example if your mapping was: Then, if you tound yourself in LA then you would go to NY, then Setlehn back to LA where you started. You visited 3 citles before returning to the starting point. However, if you found your selt in Chicago you would go to Denver and then back again to Chicago. You visited only 2 cities before returning. We are going to write a progran that perfomis similar computations. Description & Requirements In this program, the user will enter some permutation of the integersto n-1, inclusive, (each integer in that ranoe will appear exactly once) followed by -1 to indicate the end of the input. The user will never input more than 100 numbers. Your job is to store all the numbers in an aray in the order they are input. For example if the user inputs 3 021-then you should store the 3 at index 0 in an array, incex 1, 2 and index and at index 3 Once you have stored the data in your array your task is to find the maximum length cycle in the array. To answer the question, What does cycle mean?", interpret the value at index i as the next index in the array that we should visit. So n the example above, at index is the value 3. This indicates that we should go visit index 3 of the array next. Once we go to index3 we find the value. This tells us to go vist index 1 next. Once we go to index 1 we tind the value 0. This tell us to go visit index 0 next. But we've already visited index0 it's where we started). If and when you get back to your starting location (.e. we started at location and then got back to that location because the value at index 1 told us to o to 0 we stop and define those indices to be a cycle. So in this case the cycle s created ron index 0 3 1 and then back to 0). Th 3 cycle has a length of 3 3 nce t tok s us 3 steps to aet back to the starting ocation. Your job is to detect the longest (max number of steps to get back to the starting point) cycle in the array. Notice, once we start at 0 and find a cycle of length 3, we could then repeat the process starting at index 1. As we start at ndex 1 we would visit 0, from 0 we would go ta 3, and from 3 back to1The same cycle is detected and thus its lenath is the same. We have not found anything longer. From there we could look for a cycle starting at location 2. At index 2 we find the value 2 which tells us to simply stay put (or go back to itself. Thus, we have a cycle of length 1 since 2 only requires one step betore we get back to the starting point. By doing this cycle-walking process starting at each point in the array we can determine the longest cycle Note: You may realize there is some inefficiency in what we've described. We would find the cycle of 0 3 1 three times as we start our search fromhen from 1. and finally from3. You can potentially use another array to avoid this duplicated work, but you are not required to do so. It is fine to use an 'ineficient' approach to solve this problem. Your program should only output a single number on the last line of output which should be the length of the longest cycle present in the array Example 1 If the user entered: 3 0 2 1-1 our program shauld output exactly the lines Example2 If the user entered 1 2340 -1 Your program should output exactly the lines: Example 3 If the user entered: 01 234 -1 Your program should output exactly the lines: Example 3 If the user entered 03421 Your program should output exactly the lines. Program 2 - Cycles Imagine you had a travel scheme where by for every city you visited you had a mapping of what city you would travel to next. You are curious what is the longest chain (or cycle) of cities you could visit before returming to where you started. For example if your mapping was: Then, if you tound yourself in LA then you would go to NY, then Setlehn back to LA where you started. You visited 3 citles before returning to the starting point. However, if you found your selt in Chicago you would go to Denver and then back again to Chicago. You visited only 2 cities before returning. We are going to write a progran that perfomis similar computations. Description & Requirements In this program, the user will enter some permutation of the integersto n-1, inclusive, (each integer in that ranoe will appear exactly once) followed by -1 to indicate the end of the input. The user will never input more than 100 numbers. Your job is to store all the numbers in an aray in the order they are input. For example if the user inputs 3 021-then you should store the 3 at index 0 in an array, incex 1, 2 and index and at index 3 Once you have stored the data in your array your task is to find the maximum length cycle in the array. To answer the question, What does cycle mean?", interpret the value at index i as the next index in the array that we should visit. So n the example above, at index is the value 3. This indicates that we should go visit index 3 of the array next. Once we go to index3 we find the value. This tells us to go vist index 1 next. Once we go to index 1 we tind the value 0. This tell us to go visit index 0 next. But we've already visited index0 it's where we started). If and when you get back to your starting location (.e. we started at location and then got back to that location because the value at index 1 told us to o to 0 we stop and define those indices to be a cycle. So in this case the cycle s created ron index 0 3 1 and then back to 0). Th 3 cycle has a length of 3 3 nce t tok s us 3 steps to aet back to the starting ocation. Your job is to detect the longest (max number of steps to get back to the starting point) cycle in the array. Notice, once we start at 0 and find a cycle of length 3, we could then repeat the process starting at index 1. As we start at ndex 1 we would visit 0, from 0 we would go ta 3, and from 3 back to1The same cycle is detected and thus its lenath is the same. We have not found anything longer. From there we could look for a cycle starting at location 2. At index 2 we find the value 2 which tells us to simply stay put (or go back to itself. Thus, we have a cycle of length 1 since 2 only requires one step betore we get back to the starting point. By doing this cycle-walking process starting at each point in the array we can determine the longest cycle Note: You may realize there is some inefficiency in what we've described. We would find the cycle of 0 3 1 three times as we start our search fromhen from 1. and finally from3. You can potentially use another array to avoid this duplicated work, but you are not required to do so. It is fine to use an 'ineficient' approach to solve this problem. Your program should only output a single number on the last line of output which should be the length of the longest cycle present in the array Example 1 If the user entered: 3 0 2 1-1 our program shauld output exactly the lines Example2 If the user entered 1 2340 -1 Your program should output exactly the lines: Example 3 If the user entered: 01 234 -1 Your program should output exactly the lines: Example 3 If the user entered 03421 Your program should output exactly the lines

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!