Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
for a given number 'n' we put numbers form 1 to n around a table and start deleting the numbers starting from 2 like this:
============EXAMPLE===========
n=6
1 2 3 4 5 6 --> we delete 2,4,6
now we have: 1 _ 3 _ 5 _
then if we imagine them around a table after 6 we gotta delete 2 but since it has been deleted we delete the number after 2 which is 3. since they're around a table after 3 we gotta delete 1 (the number after 5). at the end we get to the number 5 which is the answer

What I have tried:

i cant translate this process into code especially the part that at the end of the array we gotta start from the beginning of the array especially the recursive function
Posted
Updated 15-Nov-18 5:19am
Comments
CPallini 15-Nov-18 7:48am    
It looks a variant of the Josephus problem, see
https://en.wikipedia.org/wiki/Josephus_problem
Unless it is explicitley required, I wouldn't use recursion (at least it doesn't seee to me recursion is a natural approach in such a problem).
Patrice T 15-Nov-18 11:26am    
Show what you have done.

We do not do your homework for you. Exercises like homework are set to cement the knowledge from the course into your brain, to help you practice the new skills and to let your tutor know where you need extra help.

Give it a go. It is not as hard as you think. Then, if you are still stuck, come back and post your code, explain your problem and we will try to help
 
Share this answer
 
Comments
Aydin Homay 15-Nov-18 7:43am    
AGREE
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Get some paper and a pencil, and draw yourself a picture of the table. Add the numbers around it. Now "play the game" and follow the rules. When you get it right, do it again, but this time, draw the table as a string line, and go from the end of the table back to the start. When that works, do it again, but this time write down the rules you are following: what you did when and why. When you have then written down, do it again, but this time, follow just the rules you just wrote down. Does it work? When it does, those rules are basically your code, all you have to do is translate to to actual code. Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
You need to visit some Learn C tutorial to learn the basics. You must learn about arrays, malloc and free.

You must implement some recursive function which does the job. It could be with the prototype
C++
int* shortenArray(const int *array, int cnt);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900