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

The Fibonacci sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, ... Formal

ID: 3663853 • Letter: T

Question

The Fibonacci sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, ...

Formally it can be expressed as:

fib0 = 0

fib1 = 1

...

   fibn = fibn-1 + fibn-2

Write a multi-threaded program that generates the Fibonacci sequence. This program should work as follows:

On the command line, the user will enter the number of Fibonacci numbers that the program is to generate.

Hint: Use the argc and argv parameters passed to main() from the command line

The program will then create a worker thread that will generate the Fibonacci numbers.

The sequence should be placed in data structure that can be shared by threads.

When the worker thread is finished, the parent thread will output the sequence generated by the worker.

Since the parent thread cannot begin outputting the Fibonacci sequence until the worker thread finishes, the parent will need to wait for the worker to finish

Explanation / Answer

#include<stdio.h>
#include<pthread.h>


#define SIZE 25

int fib_arr[SIZE]; //this array is shared by threads
void *fibonachi(void *n); // dymanically aloocate memory

int main(int argc,char *argcv[] ){

int num;
int x;

pthread_attr_t attr; //declare pthread_t type variable 'tid'

if(argc !=2){
printf("Please insert one command line argument ");
return -1;
}
num=atoi(argcv[1]); //convert string value to integer and store to 'no'

if(num<0){
printf("To Countinue please enter non-negative number ");
}
else{
pthread_attr_init(&attr);
pthread_t tid; //thread Identifier
pthread_create(&tid,&attr,fibonachi,num); //create a thread
pthread_join(tid,NULL);

printf(" - Programs Out put - ");
printf(" ");
printf("Set of Fibonachi numbers......... ");

for(x=0; x<num; x++){

printf("%d ",fib_arr[x]);
}
printf(" ");

}
return 0;
}

void *fibonachi(void *n){

int x = 0;
int y = 1;
int fibn=0;
int i;

for(i=1; i<=n; i++){
fib_arr[i-1]=fibn;
fibn = x + y;
x = y;
y = fibn;
}

pthread_exit(0);
}

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