John Curran
/
Lab2_functionsandrecursivefunctions_JohnCurran_T00214119
Lab Question 2 Functions & recursive functions
Diff: main.cpp
- Revision:
- 1:25616f0791d2
- Parent:
- 0:c87e6041f68f
--- a/main.cpp Sat Apr 17 12:14:10 2021 +0000 +++ b/main.cpp Sat Apr 17 12:16:56 2021 +0000 @@ -1,20 +1,26 @@ // Lab 2: Functions and recursive actions // John Curran T00214119 - +// we want to print using simple loops and recursive function #include "mbed.h" Serial pc(USBTX,USBRX);// serial communications int j=1, k=0, fib =0; -int N = 10 ; +int N ; -int main() { - for (int count = 0; count < N; count ++ ) { - pc.printf (" %d, ",fib); - fib = k+j; - k=j; - j=fib; +int fi(int inp) { + if (inp == 0) + return 0; + else if (inp == 1) + return 1; + else + return (fi(inp - 1) + fi (inp - 2)); + } + + int main () { + for (int i=0; i<10; i++) + pc.printf ("%d, ",fi(i)); + pc.printf("\r\n"); } - } \ No newline at end of file