Iterative version of fibonacci sequence

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
saltire78
Date:
Sat Dec 05 12:55:13 2020 +0000
Parent:
0:8f9265fb1552
Commit message:
.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Jul 31 12:19:03 2020 +0000
+++ b/main.cpp	Sat Dec 05 12:55:13 2020 +0000
@@ -1,20 +1,24 @@
-#include"mbed.h"
+#include"mbed.h"                                                                //mbed header folder
 
-Serial pc(USBTX,USBRX);//tx,rx
-PwmOut led(LED1);
-float brightness=0.0;
+Serial pc(USBTX,USBRX);                                                         //tx,rx
+ 
+int main() {                                                                    //  start main program
+    int i, iter, fibSum;                                                        // define unknown integers
+    int fib1 = 0, fib2 = 1;                                                     // define known Fibonacci values
+    
+    pc.printf("Number of iterations: ");                                        // ask for number of iterations
+    scanf("%d", &iter);                                                         // input number of iterations
+    pc.printf("\n\rFibonacci Series(%d iterations): ",iter);                    //print statement
 
-int main(){
-    pc.printf("Press 'u' to turn LED1 brightness up, 'd' to turn it down\n");
-    while(1){
-        char c =pc.getc();
-        if((c=='u')&&(brightness<0.5)){
-            brightness+=0.01;
-            led=brightness;
-            pc.putc('^');}
-        if((c=='d')&&(brightness>0.0)){
-            brightness-=0.01;
-            led=brightness;
-            pc.putc('v');}
+    for (i = 1; i <= iter; ++i) {                                               // begin loop for selected number of iterations
+        pc.printf("%d  ", fib1);                                                // print the lowest value known at that time
+        fibSum = fib1 + fib2;                                                   // create the next known value for the sequence
+        fib1 = fib2;                                                            // shift the 2nd known Fibonacci value to lowest value
+        fib2 = fibSum;                                                          // shift the highest known Fibonacci value to 2nd known value
+        
     }
-}
+    
+    pc.printf("\n\n\r");                                                        // cosmetic - moves the carriage to new line so if reset for further values it will be clearly seperate
+
+    return 0;                                                                   // close out the program
+}
\ No newline at end of file