Question

In: Computer Science

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

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 multithreaded C++ program that generates the Fibonacci series using the pthread library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program will generate. The program will then create a separate thread that will generate the Fibonacci numbers placing the sequence in a data structure that is shared by the threads (a vector is probably the most convenient data structure). Note that the thread function should be iterative when calculating fibonacci (can be recursive, but maybe difficult to implement). When the thread finishes execution, the parent thread will output the sequence generated by the child thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the child thread finishes, this will require having the parent thread wait for the child thread to finish, using the techniques described in lectures pertaining to threads. Note that the vector should only have those many terms as the user wanted.

The name of this program must be fibonacci.cpp

Solutions

Expert Solution

#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std; 

//iterative with output
DWORD WINAPI fib3(LPVOID param){
double u = 0; 
double v = 1;
double t; 
int upper = *(int*)param;

for(int i = 2; i <= upper; i++){
    cout << v << " "; 
    t = u + v; 
    u = v; 
    v = t; 
    cout << "testing" << endl; 
}
    cout << v << " "; 
    return 0; 
}

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

bool done = true; 
int x = argv[0] ; 
DWORD ThreadId; 
HANDLE ThreadHandle; 

while(done){

    if(x == -1){
        cout << "\nExiting" << endl; 
        return 0; 
    }

    ThreadHandle = CreateThread(NULL, 0, fib3, &x, 0, &ThreadId); 

    if(ThreadHandle != NULL){
        WaitForSingleObject(ThreadHandle, INFINITE); 

        CloseHandle(ThreadHandle); 
    }

}

return 0; 
}

Related Solutions

The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13,...
The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 . . . See the pattern? Each element in the series is the sum of the preceding two elements. Here is a recursive formula for calculating the nth number of the sequence: Fib(N) = {N, if N = 0 or 1 Fib(N - 2) + Fib(N - 1), if N > 1 a) Write a recursive method fibonacci that returns...
The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 … begins with the...
The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 … begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. Write a non-recursive function Fibonacci (n) that calculates the nth Fibonacci number. Write a program to display a table of terms and the Fibonacci number in two columns for the first 15 terms, using the function you created.
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not 5. Your assignment...
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence,...
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 114, … etc. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. We define Fib(0)=0,...
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 +...
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 + Fn for all n ∈ N ∪ {0}. (1) Make and prove an (if and only if) conjecture about which Fibonacci numbers are multiples of 3. (2) Make a conjecture about which Fibonacci numbers are multiples of 2020. (You do not need to prove your conjecture.) How many base cases would a proof by induction of your conjecture require?
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1,...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1, 2, 3, 5, 8, … 2. Check if a number is an Armstrong number A positive integer is called an Armstrong number of order n if abcd... = a^n + b^n + c^n + d^n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5 3 0 2 5 4 1 6 9 5 3 1 2 10 6 1 1 2 1 19 6 6 6 7 0 4 1 1 1 0 1 9 2 2 2 1 16 10 10 5 2 3 1 4 4 4 3 6 2 8 5 2 7 1 6 4 0 3 1 1 1 Background: A group of...
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics...
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics and applications to arrangement of flower petals, population growth of rabbits, and genetics. For each natural number n ≥ 1, the nth Fibonacci number fn is defined inductively by f1 = 1, f2 = 2, and fn+2 = fn+1 + fn (a) Compute the first 8 Fibonacci numbers f1, · · · , f8. (b) Show that for all natural numbers n, if α...
Google the first 50 numbers of the Fibonacci sequence (starting with 1) to answer the following...
Google the first 50 numbers of the Fibonacci sequence (starting with 1) to answer the following questions:          (a) Test to see if the leading digits conform to Benford’s law. Do this both graphically and analytically.          (b) Using the first 10 odd numbers in the sequence as sample 1 and the first 10 even numbers in the sequence as sample 2, use Wilcoxon’s Rank-Sum to test the claim that the numbers come from different populations.          (c) Repeat (b)...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6 13 7 33 8 42 9 66 10 77 11 105 12 103 13 110 14 105 15 84 16 70 17 51 18 40 19 27 20 27 21 15 22 5 23 7 24 2 25 2 26 1 27 0 28 0 29 0 30 0 (7) On the Histogram worksheet, calculate all frequencies of the distribution using the table shown....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT