Array structure problem..

This is a discussion on Array structure problem.. within the C Programming forums, part of the General Programming Boards category; President gets advice from his pool of advisers every time he needs to make a decision. He has 13 top ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    61

    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;
    } advisor[13];
    
    
    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++)
      {
        advisor[lp1].num = lp1;
        advisor[lp1].status = 1;
      }
      while(ch != 1)
      {
        if(advisor[lp2].status == 1)
        {
            lp3++;
            if(advisor[lp2].num == interval)
            {
                advisor[lp2].status = 0;
                chosen[lp1] = advisor[lp2].num;
                lp1++;
            }
    
    
            if(lp3 == interval)
            {
                chosen[lp1] = advisor[lp2].num;
                advisor[lp2].status = 0;
                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();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    But what's your question?

    Don't just say "here's my code..." and hope we're going to all rush off to download and test it for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    I' m sorry.
    My program is in Infinite loop and I can't fix it.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    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. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    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. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by programmerc View Post
    It works!
    Good good!

    Quote 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. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    There's is no 0th person..

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by programmerc View Post
    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++)
      {
        advisor[lp1].num = lp1;
    You still can't do this though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-21-2012, 02:14 AM
  2. Replies: 10
    Last Post: 05-30-2011, 09:26 AM
  3. dynamic array and structure problem
    By bluetxxth in forum C Programming
    Replies: 3
    Last Post: 04-13-2010, 07:56 AM
  4. Structure Array Looping Problem
    By Vitamin_C in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2005, 03:22 PM
  5. Structure array problem
    By Birdhaus in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2005, 09:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21