Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>

int fib;

void* fibonacci(void *n)
{
    int a=0,b=1,c,i;
    if(atoi(n)==1)
    {
        printf("The Fibbonaci sequence for the entered number is\n");
        printf("%d\n",a);
        exit(0);
    }

    else if(atoi(n)==2)
    {
        printf("The Fibbonaci sequence for the entered number is\n");
        printf("%d\t%d",a,b);
        exit(0);
    }

    else
    {
        printf("The Fibbonaci sequence for the entered number is\n");
        printf("%d\t%d",a,b);
        for(i=0;i<atoi(n)-2;i++)
        {
            c=a+b;
            printf("\t%d",c);
            a=b;
            b=c;
        }

        pthread_exit(0);
    }


int main(int argc,char* argv[])
{
    pthread_t thrd;

    if(argc!=2)
    {
        fprintf(stderr,"Syntax: ./a.out <integer value>");
        return -1;
    }

    if(atoi(argv[1])<0)
    {
        fprintf(stderr,"Argument %d must be positive value\n",atoi(argv[1]));
        return -1;
    }

    pthread_create(&thrd,NULL,fibbonaci,(void*)argv[1]);

    pthread_join(thrd,NULL);

    exit(0);
}




The code on compilation gives the following errors:

VB
In function ‘main’:
FibnThread.c:56:27: error: ‘fibbonaci’ undeclared (first use in this function)
FibnThread.c:56:27: note: each undeclared identifier is reported only once for each function it appears in
FibnThread.c: In function ‘fibonacci’:
FibnThread.c:61:1: error: expected declaration or statement at end of input
Posted
Updated 25-Aug-13 9:36am
v2
Comments
pasztorpisti 25-Aug-13 15:30pm    
You shouldn't call exit(0) on your thread. Call pthread_exit(0) or simply return with "return NULL;" from the thread function.

You (correctly) implemented the function as fibonacci (Leonardo Fibonacci[^]) but invoked it as fibbonacci. Remove the spurious b in the function call.

[update]
You actually forgot a closing brace ('}'). Here you are the fixed code:
C
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>

int fib;

void* fibonacci(void *n)
{
  int a=0,b=1,c,i;
  if(atoi(n)==1)
  {
    printf("The Fibonacci sequence for the entered number is\n");
    printf("%d\n",a);
    exit(0);
  }
  else if(atoi(n)==2)
  {
    printf("The Fibonacci sequence for the entered number is\n");
    printf("%d\t%d",a,b);
    exit(0);
  }
  else
  {
    printf("The Fibonacci sequence for the entered number is\n");
    printf("%d\t%d",a,b);
    for(i=0;i<atoi(n)-2;i++)
    {
      c=a+b;
      printf("\t%d",c);
      a=b;
      b=c;
    }
    pthread_exit(0);
  }
}

int main(int argc,char* argv[])
{
  pthread_t thrd;

  if(argc!=2)
  {
    fprintf(stderr,"Syntax: ./a.out <integer value>");
    return -1;
  }
  if(atoi(argv[1])<0)
  {
    fprintf(stderr,"Argument %d must be positive value\n",atoi(argv[1]));
    return -1;
  }

  pthread_create(&thrd,NULL,fibonacci,(void*)argv[1]);

  pthread_join(thrd,NULL);

  exit(0);
}

[/update]
 
Share this answer
 
v4
Comments
Brady Bar 25-Aug-13 14:46pm    
I later on realised that and removed that but the last error is still there.
CPallini
nv3 25-Aug-13 18:04pm    
You have sharp eyes. 5.
CPallini 26-Aug-13 2:14am    
Actually I am myopic :-)
Thank you.
just look at this link.same problem

here
 
Share this answer
 
A brace was missing for the last else condition in the fibonacci function.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Aug-13 21:56pm    
This is not an answer, should not be here.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900