1. ## Array structure problem..

President gets advice from his pool of advisers every time he needs to make a decision. He has 13 top advisers that he really relies on. But if all of them are involved in any decision, they always end up in an argument where more often than not, the final decision is not achieved. To resolve this, a scheme was devised where only 5 of the 13 will be randomly chosen to decide on a certain matter.

They achieved this by having the 13 advisers arrange themselves in a circle. The President can start counting from a certain adviser with an appropriate counting interval. For example, he can start counting from adviser 1 with a counting interval of 4. Then the first adviser counter out will be adviser 4, followed by advisers 8, 12, 3 and 9. Note that those advisers who are already counted out will not be counted again.

Write a program that would ask which adviser to start from and ask the counting interval. Then, it should display which advisers were selected.

Here's my code...

Code:
```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct bla
{
int num;
int status;

int check(int x[5])
{
int lp1 = 0, lp2 = 0;
while(lp1 != 5)
{
if(x[lp1] > 0)
{
lp2++;
}
lp1++;
}
if(lp2 == 5)
{
return 1;
}
else
{
return 0;
}
}

void main()
{
int lp1 = 0, lp2 = 0, lp3 = 0;
int start = 0, interval = 0, ch = 0;
int chosen[5] = {0, 0, 0, 0, 0};
clrscr();
printf("Start: ");
scanf("%d", &start);
if(start == 0)
{
exit(1);
}
printf("Interval: ");
scanf("%d", &interval);
lp2 = start - 1;
for(lp1 = 1; lp1 <= 13; lp1++)
{
}
while(ch != 1)
{
{
lp3++;
{
lp1++;
}

if(lp3 == interval)
{
lp1++;
lp3 = 0;
}
}
lp2++;
if(lp2 == 13)
{
lp2 = 0;
}
ch = check(chosen);
printf("%d   ", ch);
}
for(lp1 = 0; lp1 < 5; lp1++)
{
printf("\nChosen Adviser %d: %d", lp1+1, chosen[lp1]);
}
getch();
}```

Don't just say "here's my code..." and hope we're going to all rush off to download and test it for you.

3. I' m sorry.
My program is in Infinite loop and I can't fix it.

4. The thing that's making your code go badly wrong is that you're not resetting lp1 to 0 after the init loop on ln 52. lp1 is 13 when leaving that loop! So the loop will never terminate, because it's impossible for the first 3 elements of chosen[] to have been written when we started at element 13.

That won't give you the right answer but it shouldn't behave more sensibly now.
I have a few pointers for fixing the rest of it:

You should rename your variables to things that describe that the variable is for. lp1, lp2 and lp3 don't do this, and it's quite difficult to read as a result.

Code:
`if(advisor[lp2].num == interval)`
This isn't right. In the example you gave, interval is 4. So this will be true when you get to dude 4, but not 8, 12, 3, 9....
You probably want to check to see if the count divides by the increment and leaves no remainder (have a look at Modulus Operator in C and C++ - Programming Tutorials - Cprogramming.com).

Code:
```} advisor[13];
...
lp2 = start - 1;
...
for(lp1 = 1; lp1 <= 13; lp1++)```
You've been inconsistent in adjusting for the arrays being numbered from 0. You handle it fine with the chosen[] array but not with advisor[]. Advisor[13] doesn't exist! Although you could declare yourself an array of 14 elements and ignore the first one, it's not good practice to do so. Lots of things are numbered from 1 onwards, so it's good to get used to adapting programs around that.
I'd tend to keep the numbers from 0-12 throughout the program and just add 1 at output printing time. You'll need to adjust your check() function if 0 is a valid position though.

5. It works!
What do you mean with this sentence? "You'll need to adjust your check() function if 0 is a valid position though."

6. Originally Posted by programmerc
It works!
Good good!

Originally Posted by programmerc
What do you mean with this sentence? "You'll need to adjust your check() function if 0 is a valid position though."
Your check() functions checks to see if every value in chosen[] has been written. It does this by checking that the 5 values are > 0.
If you count from 1 in your arrays, that's ok.
If you count from 0, which would be easier, then 0 could be a valid entry in chosen[], because there is a 0th person. E.g. "start at 10, interval 2" would get mapped to "start at 9" and give 9, 11, 0, 2, 4, 6 (output as 10, 12, 1, 3, 5, 7).

So you can't check that the array is complete by checking that there aren't any 0s.

A fairly standard thing to do would be to initialise the chosen[] array with -1 s. They can never be valid here.

Even easier would be to exit the loop when the counter you're using for filling in chosen[] gets to 5.

7. There's is no 0th person..

8. Originally Posted by programmerc
There's is no 0th person..
Ahh - you're right, I'm sorry. I didn't read your code closely enough. I would have used each advisors array index to identify them, rather than storying a number in each entry.

Ignore what I said -- there is indeed no 0th person, as in no number 0 stored in advisor.num.

Code:
```  for(lp1 = 1; lp1 <= 13; lp1++)
{