Ducks and Alligators

I am having a problem with this program.

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++.

_________________________________________________________________________

//Author: Michael Valentine
//Date: January 20, 2014
//Purpose: Ducks and Alligators




#include <iostream.h>
#include <iomanip.h>
#include <conio.h>




int main()

{

//State Variables

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: ";

for(i=1;i<=many;i=i+1)

{

if(ducks[i]==1)

{

cout << i;

}

}

getch();

}
___________________________________________________________________________
Last edited on
[code] "Please use code tags" [/code]

Array index goes from 0 to size-1
1
2
3
4
for(i=1;i<=11;i=i+1)
{
   ducks[i] = 1; //out of bounds
}



1
2
//Going through all of the ducks
for(i=fate;i<=many-1;i=i+bate)
Wrong. Just try to count how many times does that loop execute.


1
2
3
4
5
6
7
8
9
10
		if(ii>many)
		{
			ii = ii-(many+1);
			temp = (ii+bate)-(many+1);
			ii = ii + temp;
			if(ii<=0)
			{
				ii = 1;
			}
		}
¿what are you trying to do there?


> I am using Borland C++
http://www.cplusplus.com/forum/articles/36896/#msg199830
How do I count how many times the loop executes?

and the

1
2
3
4
5
6
7
8
9
10
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.
Last edited on
Topic archived. No new replies allowed.