In: Computer Science
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!!
#include
#include
#include
#include
#include
int shared_data[10000];
void *fibonacci_thread(void* params);
void parent(int* numbers);
int main() {
int numbers = 0; //user input.
pthread_t child; // create thread
pthread_attr_t attr;
pthread_attr_init(&attr);
parent(&numbers); // get user input then start
separate thread.
pthread_create(&child, &attr,
fibonacci_thread, (void*) &numbers); //starts fibonacci
thread
pthread_join(child, NULL); //waits for thread to
finish
//output to command prompt after thread
finishes.
for(int i = 0; i <= shared_data[i]; i++) {
printf("%d",shared_data[i]);
}
return 0;
}
void *fibonacci_thread(void* params) {
int fib0 = 0, fib1 = 1, next = 0;
int *pointer;
pointer = (int*) params;
int total = *pointer;
for (int i = 0 ; i < total; i++ ) {
if ( i <= 1 )
next = i;
else {
next = fib0 + fib1;
fib0 = fib1;
fib1 = next;
}
next = shared_data[i]; //store to shared_data
array
}
//pthread_exit(0);
return NULL;
}
void parent(int* numbers) {
std::cout<<"Enter in a number to generate the
Fibonacci sequence: ";
std::cin>>*numbers;
while(isdigit(*numbers) != true) {
std::cout<<"Invalid
character, please enter in a number: ";
std::cin>>*numbers;
}
return;
}
here is the error below:
student@tuffix-vm:~$ cd CPSC-351-Project2-Fall2020
student@tuffix-vm:~/CPSC-351-Project2-Fall2020$ g++ -pthread
fibonacci.cpp -o fibonacci
student@tuffix-vm:~/CPSC-351-Project2-Fall2020$ ./fibonacci
Enter in a number to generate the Fibonacci sequence: 2
Invalid character, please enter in a number: 3
Invalid character, please enter in a number: 4
Invalid character, please enter in a number: 5
Invalid character, please enter in a number: 6
Invalid character, please enter in a number: 7
Invalid character, please enter in a number: 8
Invalid character, please enter in a number: 9
Invalid character, please enter in a number: 55
student@tuffix-vm:~/CPSC-351-Project2-Fall2020$
Please look at my code and in case of indentation issues check the screenshots.
--------------main.cpp---------------
#include <pthread.h>
#include <iostream>
#include <stdlib.h>
#include <string>
int shared_data[10000];
void *fibonacci_thread(void *params);
void parent(int *numbers);
bool isNumber(std::string str);
int main()
{
int numbers = 0; //user input.
pthread_t child; // create thread
pthread_attr_t attr;
pthread_attr_init(&attr);
parent(&numbers); // get user input
then start separate thread.
pthread_create(&child, &attr,
fibonacci_thread, (void*) &numbers); //starts
fibonacci thread
pthread_join(child, NULL); //waits for
thread to finish
//output to command prompt after thread
finishes.
for (int i = 0; i < numbers; i++)
{
printf("%d ",
shared_data[i]); //prints the array written by the
thread
}
std::cout << "\n";
return 0;
}
void *fibonacci_thread(void *params)
{
int fib0 = 0, fib1 = 1, next = 0;
int *pointer;
pointer = (int*) params;
int total = *pointer;
for (int i = 0; i < total; i++)
//loop to generate next fibonacci number
{
if (i <= 1)
next = i;
else
{
next = fib0 +
fib1;
fib0 =
fib1;
fib1 =
next;
}
shared_data[i] = next;
//store to shared_data array
}
//pthread_exit(0);
return NULL;
}
void parent(int *numbers)
{
std::string input;
std::cout << "Enter in a number to generate the
Fibonacci sequence: ";
std::cin >> input;
//read the input as a string
while (isNumber(input) != true) //check
if input is a number
{
std::cout << "Invalid input,
please enter in a number: ";
std::cin >>
input; //if not a
number ask for another input
}
*numbers = atoi(input.c_str()); //if input
was a number, convert it from string to integer
return;
}
bool isNumber(std::string str) //this function takes
a string and checks if it is a positive number
{
for (int i = 0; i < str.length(); i++)
{
if (!isdigit(str[i]))
//is digit is used to check if one character is a digit
return
false; //when even one character is not a digit, return
false
}
return true; //if all characters are
digits return true
}
----------Screenshots--------------
----------Output--------------
----------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou