There are N ducks swimming in a circle on a pond. An alligator is eating every Rth duck as it swims by. The ducks are numbered 1 to N. The maximum value for N will be 10. The program must allow the user to specify the number of ducks on the pond., tell the alligator the number of the first duck to be eaten and then what the Rth duck to eat at each pass (2nd, 3rd, 4th, etc.). The program should then print out the number of the last duck to be eaten. The program must use arrays
My problem is that the loop is saying that the not only the third, but the first second, and fourth are the last one too. This is my code, anybody have any suggestions? If it helps, I am using Borland C++.
int i, ii;
int many, fate, bate, temp;
int ducks[11];
//Set lists to one, so that they can be set to zero later.
for(i=1;i<=11;i=i+1)
{
ducks[i] = 1;
}
cout << "How many ducks are there? <1-10>\n";
cin >> many;
clrscr();
cout << "Which is the first duck eaten? <1-10>\n";
cin >> fate;
ducks[fate] = 0;
clrscr();
cout << "How many pass before another duck is eaten? <1-10>\n";
cin >> bate;
clrscr();
//Going through all of the ducks
for(i=fate;i<=many-1;i=i+bate)
{
ii = i;
while(ducks[ii]==0)
{
ii = ii+1;
}
if(ii>many)
{
ii = ii-(many+1);
temp = (ii+bate)-(many+1);
ii = ii + temp;
if(ii<=0)
{
ii = 1;
}
}
ducks[ii] = 0;
i = ii;
}
//Looking for and printing out the last duck
cout << "Ducks starting on the pond: " << many << "\n\n";
cout << "First duck to be eaten: " << fate << "\n\n";
cout << "Number of ducks passed before next one was eaten: " << bate << "\n\n";
cout << "The one that thought he survived: ";
if(ii>many)
{
ii = ii-(many+1);
temp = (ii+bate)-(many+1);
ii = ii + temp;
if(ii<=0)
{
ii = 1;
}
}
This is where, if the number is larger than the array exists, I try to rationalize it by turning it into an algorithm that makes it start the number sequence over and count on. Also, if the number ends up being zero, or less, I set it to one so that it is always within the bounds of 1 and the number of total ducks. I can't think of a way to loop numbers though.
And I can only use borland. This is due to the fact that my computer programming class hasn't been used in years, and it is all they have.