help with program please

This is a discussion on help with program please within the C Programming forums, part of the General Programming Boards category; ok, i need some help with this: i need to write a program where there are "x" amount of men ...

  1. #1
    Unregistered
    Guest

    Arrow help with program please

    ok, i need some help with this:
    i need to write a program where there are "x" amount of men in a circle, beginning at a particular position, we count around the circle and execute every "y" men.for example when there are 1,2,3,4,5,6,7,8 guys and the execution is every 4 persons, the order of exec. is : 5,1,6,3,2,4,8

    i can't figure out how to code the order of execution, can i get some ideas?? so far, this is what i have:

    { int num[100],i,n,m,order,execute,count;

    printf("enter value for n: ");
    scanf("%d", &n);
    printf("\nthe total number of men are: %d",n);

    printf("\nNow enter the order that you would like:\n");
    for(i=0;i<n;i++)
    {scanf("%d",&order);
    num[i]=order;
    }
    printf("\nthe order of men are: ");
    for(i=0;i<n;i++)
    {
    printf("\n%d",num[i]);
    }

    printf("\nNow enter the execution order: ");
    scanf("%d",&execute);

    printf("\nThe order they were executed in is: ");

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    41
    This is just some brainstorming around, maybe you can get some ideas of it. Not sure if it works.

    Code:
    int remaining, position, exec_count, tmp_count, i;
    int exec_order[100];
    int new_num[100];
    
    remaining = n;
    position = 0;
    exec_count = 0;
    
    while (remaining > 0) {
      // move the gun to the next guy in the row
      position = position + execute;
      // wrap around
      while (position > remaining - 1)
        position = position - remaining;
    
      exec_order[exec_count] = num[position];
      ++exec_count;
    
      // create a new row without this guy
      tmp_count = 0;
      for (i = 0; i < remaining; ++i) {
        if (i == position)
          ++i;
        if (i >= remaining)
          break;
    
        new_num[tmp_count] = num[i];
        ++tmp_count;
      }
      --remaining;
      --position;
      if (position < 0)
        position = remaining - 1;
      for (i = 0; i < remaining; ++i) {
        num[i] = new_num[i];
      } 
    }
    Then you _could_ have the order in exec_order[]. Maybe not.
    Last edited by Spark; 05-30-2002 at 03:15 AM.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    i need to write a program where there are "x" amount of men in a circle, beginning at a particular position, we count around the circle and execute every "y" men.for example when there are 1,2,3,4,5,6,7,8 guys and the execution is every 4 persons, the order of exec. is : 5,1,6,3,2,4,8
    Is it me, or is your example wrong.

    You start with
    12345678
    And you position yourself on 1.
    Add 4, moves you to position 5.
    Add 4, moves you to position 1.
    Add 4, moves you to position 5.
    and now we're in a loop, going between 1 and 5.

    This doesn't match your answer of
    5,1,6,3,2,4,8

    Anyway, there's no need for complicated loops and arrays. Just ask the user how many people there are, and how often execution occurs. Then use modulos to determine the next person.

    For example:
    Code:
    Execution = 1   /* Person currently *working* */
    Start Loop
       Execution = ((Execution + OccursEvery - 1 ) Mod TotalPeople) + 1
       Print "Worker is " Execution
    End Loop
    I think the maths works OK.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    41
    Originally posted by Hammer


    Is it me, or is your example wrong.

    You start with
    12345678
    And you position yourself on 1.
    Add 4, moves you to position 5.
    Add 4, moves you to position 1.
    Add 4, moves you to position 5.
    and now we're in a loop, going between 1 and 5.

    This doesn't match your answer of
    5,1,6,3,2,4,8
    I thought the same first, but I think the reason is that every executed guy is out of the circle. This makes everything considerably more complicated. =)
    It's like this:
    You start with
    12345678
    And you position yourself on 1.
    Add 4, moves you to position 5.
    Execute this guy. Now you have:
    1234678 and the position is on 4.
    Add 4, moves you to 1
    Execute this guy. Now you have:
    234678 and the position is 8.
    Add 4, moves you to 6, etc

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    41
    Ok, so let's use Mod for finding the poor guy who gets killed and simplify this a little bit to a final program:

    Code:
    int guy[100], exec_order[100];
    int i, guys_num, order, execute_step;
    int remaining, tmp_count, position = 0;
    
    printf("Number of men: ");
    scanf(%d", &guys_num);
    printf("\nTotal number of men: %d", &guys_num);
    
    printf("\nNow enter the order that you would like:\n");
    for (i = 0; i < guys_num; i++) {
      scanf("%d", &order);
      guy[i] = order;
    }
    
    printf("\nThe order of men is: ");
    for (i = 0; i < guys_num; i++) 
      printf("\n%d", guy[i]);
    
    printf("\nNow enter the execution step: ");
    scanf("%d", &execute_step);
    
    remaining = guys_num;
    
    while (remaining > 0) {
      position = position + execute_step;
      if (position > remaining - 1)
        position = position Mod remaining;
    
      exec_order[guys_num - remaining] = guy[position];
    
      tmp_count = 0;
      for (i = 0; i < remaining; i++) {
        if (i == position)
          i++;
        if (i >= remaining)
          break;
    
        guy[tmp_count] = guy[i];
        tmp_count++;
      }
    
      remaining--;
      position--;
      if (position < 0)
        position = remaining - 1;
    }
    
    printf("\nThe order they were executed in is:\n");
    for (i = 0; i < guys_num; i++) 
      printf("\n%d", exec_order[i]);
    Tell me if this works. I did this because my server is currently down and I have all my code there so I currently cant' work on my stuff. Can you see I'm bored? I don't even have a compiler here so I don't expect the code above to be free of syntax errors.
    Last edited by Spark; 05-30-2002 at 04:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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