What is false

I can't find what is wrong in my program. Is somebody who knows ?.
#include <iostream>
#include <conio.h>
#include <time.h>
#include <cstdlib>
#include <iomanip>
using namespace std;
long double fibo [100]; int n;
double czas;
clock_t start,stop;
int main ();

int f( int n)
{
int main();
start=clock();
if (n==0) return 3;
else return f(n-1)+2;
stop=clock();
cout<<"Czas zapisu:"<<czas<<"s "<<endl;
return (0);
}
Siema! Your program doesn't make much sense. The main function is only declared, but never defined. You never assign anything to your czas variable, but attempt to print it. All the code after your if-else return statements is inaccessible.

Your program really makes no sense. This is nowhere near a proper fibonacci sequence, if that's what you're going for.

Perhaps something like this is what you wanted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
//#include <conio.h>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;

// long double fibo [100]; // not used

int f(int n) // still not the fibonacci sequence, but at least it compiles
{
    if (n == 0)
        return 3;
    else
        return f(n-1) + 2;
}

int main()
{
    clock_t start, stop;
    
    start = clock(); // not actually used in a meaningful way
    
    int czas = f(3); // I decided to call your function with n = 3
    cout << "Czas zapisu: " << czas << "s "<< endl;
    
    stop = clock(); // not actually used in a meaningful way
    
    return 0;
}


Please search for tutorials online, or read this one:
http://www.cplusplus.com/doc/tutorial/

Also note the units of clock() are in CLOCKS_PER_SEC, not seconds.
http://www.cplusplus.com/reference/ctime/clock/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <ctime>

long long int fib(long long int n) {
	if (n <= 1)
		return n;
	return fib(n - 1) + fib(n - 2);
}

int main() {

	clock_t time;
	time = clock();
	std::cout << "Dwudziesta liczba fibonacci: " << fib(20);
	time = clock() - time;
	std::cout << "\nCzas zapisu: " << (double)time / CLOCKS_PER_SEC << "s\n";

	std::cin.get();
	return 0;
}
Topic archived. No new replies allowed.