Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Josephus Problem In programming C The Josephus Problem, People are standing

ID: 3586748 • Letter: T

Question

The Josephus Problem In programming C

The Josephus Problem, People are standing in a circle waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is executed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains, and is freed. The problem - given the number of people, starting point, direction, and number to be skipped - is to choose the position in the initial circle to avoid execution.

Instead of using bit-manipulating operations, implement your program by using an array to represent the circle of soldiers and loop and conditional constructs to "simulate" the counting-out game.

The out put should look like:

How many people are in the circle?: 41

The spot 19 is safe.

Explanation / Answer

Code:

#include <stdio.h>

#include <stdlib.h>

     
    struct node
    {

        int num;

        struct node *next;

    };
     
    void create(struct node **);

    void display(struct node *);

    int survivor(struct node **, int);
     
    int main()
    {

        struct node *head = NULL;

        int survive, skip;
     
        create(&head);

        printf("The persons in circular list are: ");

        display(head);

        printf("Enter the number of persons to be skipped: ");

        scanf("%d", &skip);

        survive = survivor(&head, skip);

        printf("The person to survive is : %d ", survive);

        free(head);
     
        return 0;

    }
     
    int survivor(struct node **head, int k)
    {

        struct node *p, *q;

        int i;
     
        q = p = *head;

        while (p->next != p)

        {

            for (i = 0; i < k - 1; i++)

            {

                q = p;

                p = p->next;

            }

            q->next = p->next;

            printf("%d has been killed. ", p->num);

            free(p);

            p = q->next;

        }

        *head = p;
     
        return (p->num);

    }
     
    void create (struct node **head)
    {

        struct node *temp, *rear;

        int a, ch;
     
        do
        {

            printf("Enter a number: ");

            scanf("%d", &a);

            temp = (struct node *)malloc(sizeof(struct node));

            temp->num = a;

            temp->next = NULL;

            if (*head == NULL)

            {

                *head = temp;

            }

            else

            {

                rear->next = temp;

            }

            rear = temp;

            printf("Do you want to add a number [1/0]? ");

            scanf("%d", &ch);

        } while (ch != 0);

        rear->next = *head;

    }
     
    void display(struct node *head)
    {

        struct node *temp;
     
        temp = head;

        printf("%d   ", temp->num);

        temp = temp->next;

        while (head != temp)

        {

            printf("%d   ", temp->num);

            temp = temp->next;

        }

        printf(" ");

    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote