Thread: Josephous Problem

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    Josephous Problem

    Can anyone tell me what is wrong with my code?
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    typedef struct Josephous
    {
        int pos;
        struct Josephous *next;
    }node;
    node *create(node*,int);
    int Calculate(node*,int ,int);
    int main()
    {
        int n,m,res;
        node *head;
        printf("Enter no. of players = ");
        scanf("%d",&n);
        printf("Enter position to eliminate = ");
        scanf("%d",&m);
        head=(node*)malloc(sizeof(node));
        head->pos=1;
        head->next=head;
        head=create(head,n);
        res=calculate(head,n,m);
        printf("The winner is = %d",res);
        return 0;
    }
    node *create(node *head,int n)
    {
        node *q,*temp;
        int i;
        q=head;
        for(i=2;i<=n;i++)
        {
            temp=(node*)malloc(sizeof(node));
            temp->pos=i;
            temp->next=q;
            q->next=temp;
            q=temp;
        }
        return head;
    }
    int calculate(node *head,int n,int m)
    {
    
        node *q,*temp;
        int i;
        while(head->next!=head)
        { 
            q=head;
            temp=q;
            for(i=1;i<m;i++)
            {
                temp=q;
                q=q->next;
            }
            temp->next=q->next;
            head=temp->next;
            q->next=NULL;
            free(q);
            q=NULL;
        }
        return (head->pos);
    }
    Last edited by daggerhunt; 11-21-2012 at 04:32 AM.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    gcc -std=c89 -pedantic -Wall -Werror 152488.c
    152488.c:2:18: fatal error: conio.h: No such file or directory

    Ok, so I commented line 2 out
    gcc -std=c89 -pedantic -Wall -Werror 152488.c
    152488.c:23:5: error: implicit declaration of function `calculate` [-Werror=implicit-function-declaration]
    <line 23 is where you call the calculate() function>
    <line 10 is where you declare a Calculation() function. I assume you meant calculation>

    so I changed the initial uppercase letter in line 10 to lowercase
    gcc -std=c89 -pedantic -Wall -Werror 152488.c
    <COMPILATION SUCCESSFUL>

    No more errors with your code.
    Last edited by qny; 11-21-2012 at 05:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  3. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 10:40 PM