I need to create a program which calculates recursion (for certain sequence). When I use int and decleare a recursion, that calculates values without floating numbers (like fibonacci sequence, which returns only neutral numbers) it works. However, when trying to use sequences based on divison (with floating numbers) it displays an error as below:
error: cannot convert to a floating type pthread_exit((void*)(float)wynik;
How should I change the code (or actually a function *ciag, because problem is with that one), that it will accept floating numbers?
Function which works fine (with int)
int* fibo(int n){
int wynik;
int* n1;
if (n==0) wynik=0;
else if (n==1) wynik=1;
else wynik =(int)fibo((int)(n-1))+(int)fibo((int)(n-2));
return (int*)wynik;
pthread_exit((void*)wynik);
}
And the one I have problem with (with float, but same happens when I try to use double)
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#define COUNT 2
float *ciag(int n) {
float wynik;
if(n == 0)
wynik = -1;
else
wynik = ((float)ciag(n - 1)*(n + 1))/(float)ciag(n - 1)*(float)ciag(n - 1)*(float)ciag(n - 1);
return(float *)wynik;
pthread_exit((void *)wynik);
}
void *drugi_watek(void* wynik) {
int i = 1;
while(i == 0) {
printf("#");
fflush(stdout);
usleep(300000);
pthread_exit((void*)wynik);
}
}
int main() {
pthread_t watek_1, watek_2;
int n;
float wynik;
printf("Podaj numer ciagu: ");
scanf("%d", &n);
pthread_create(&watek_1, NULL,(void*)&ciag, n);
pthread_create(&watek_2, NULL, &drugi_watek, NULL);
if(!pthread_join(watek_1,(void**)&wynik))
{
pthread_cancel(watek_2);
}
printf("Element numer %f ciagu: %f\n", &n, &wynik);
return 0;
}