From 82d3b4ccae251d298d2f64c298cebc64fa781ec9 Mon Sep 17 00:00:00 2001 From: Richard Zak Date: Thu, 28 Jul 2022 12:30:00 -0400 Subject: [PATCH] fixup: CLI arguments for C, CPP, Go, Ruby Signed-off-by: Richard Zak --- .gitignore | 2 ++ C++/fibonacci/fibonacci.cpp | 25 +++++++++++++++++++------ C/fibonacci/fibonacci.c | 25 ++++++++++++++++++++----- Go/fibonacci/go.mod | 3 +++ Go/fibonacci/main.go | 34 +++++++++++++++++++++++++--------- Ruby/fibonacci/fibonacci.rb | 22 +++++++++++++++++++--- 6 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 Go/fibonacci/go.mod diff --git a/.gitignore b/.gitignore index f0f19a63..0a016c7b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ Zig/*/zig-out/ .idea/ .vscode/ result +*.wasm +a.out diff --git a/C++/fibonacci/fibonacci.cpp b/C++/fibonacci/fibonacci.cpp index 07295a73..0f05d38a 100644 --- a/C++/fibonacci/fibonacci.cpp +++ b/C++/fibonacci/fibonacci.cpp @@ -1,16 +1,29 @@ -// Simple Program to calculate Fibonacci Sequence of an integer input #include +#include using namespace std; + +#define DEFAULT_N 10 + int FibonacciSequence(int num) { if(num <= 1) { return num ; } return FibonacciSequence(num-1) + FibonacciSequence(num-2); } -int main(){ - cout << "Enter the Number" << endl; - int n ; - cin >> n ; - cout << "Fibonacci Sequence term at " << n << " " << "is " << FibonacciSequence(n) << endl; +int main(int argc, char* argv[]){ + int n = DEFAULT_N; + if (argc > 1) { + n = atoi(argv[1]); + } else { + cout << "Which Fibonacci index to find? "; + cout.flush(); + cin >> n; + } + + if (n < -1) { + n = DEFAULT_N; + } + + cout << "Fibonacci sequence number at index " << n << " is " << FibonacciSequence(n) << endl; } diff --git a/C/fibonacci/fibonacci.c b/C/fibonacci/fibonacci.c index 18f96e1b..3e7ec781 100644 --- a/C/fibonacci/fibonacci.c +++ b/C/fibonacci/fibonacci.c @@ -1,4 +1,7 @@ #include +#include + +#define DEFAULT_N 10 int FibonacciSequence(int num) { if(num <= 1) { @@ -6,10 +9,22 @@ int FibonacciSequence(int num) { } return FibonacciSequence(num-1) + FibonacciSequence(num-2); } -int main(){ - printf("Enter the Number\n"); - int n ; - scanf("%d",&n); - printf("Fibonacci Sequence term at %d is %d " , n , FibonacciSequence(n)); +int main(int argc, char* argv[]){ + int n = DEFAULT_N; + if (argc > 1) { + n = atoi(argv[1]); + } else { + printf("Which Fibonacci index to find? "); + int matched = scanf("%d", &n); + if (matched < 1) { + n = DEFAULT_N; + } + } + + if (n < -1) { + n = DEFAULT_N; + } + + printf("Fibonacci sequence number at index %d is %d" , n , FibonacciSequence(n)); } diff --git a/Go/fibonacci/go.mod b/Go/fibonacci/go.mod new file mode 100644 index 00000000..e510be72 --- /dev/null +++ b/Go/fibonacci/go.mod @@ -0,0 +1,3 @@ +module github.com/enarx/codex/Go/fibonacci + +go 1.17 diff --git a/Go/fibonacci/main.go b/Go/fibonacci/main.go index c9f2dfa9..be47628d 100644 --- a/Go/fibonacci/main.go +++ b/Go/fibonacci/main.go @@ -1,19 +1,35 @@ -// Simple Program to calculate fibonacci of input - package main -import "fmt" -func FibonacciRecursion(n int) int { +import ( + "fmt" + "strconv" + "os" +) + +const DEFAULT_N int = 10 + +func FibonacciSequence(n int) int { if n <= 1 { return n } - return FibonacciRecursion(n-1) + FibonacciRecursion(n-2) + return FibonacciSequence(n-1) + FibonacciSequence(n-2) } func main(){ - fmt.Print("Enter number : ") - var n int - fmt.Scanln(&n) + n := DEFAULT_N + + if len(os.Args) > 1 { + n, _ = strconv.Atoi(os.Args[1]) + } else { + var input string + fmt.Print("Which Fibonacci index to find? ") + fmt.Scanln(&input) + n, _ = strconv.Atoi(input) + } + + if n < -1 { + n = DEFAULT_N + } - fmt.Println("Fibonacci of", n , "is", FibonacciRecursion(n)); + fmt.Printf("Fibonacci sequence number at index %d is %d" , n , FibonacciSequence(n)); } diff --git a/Ruby/fibonacci/fibonacci.rb b/Ruby/fibonacci/fibonacci.rb index 150c4162..29a377e3 100644 --- a/Ruby/fibonacci/fibonacci.rb +++ b/Ruby/fibonacci/fibonacci.rb @@ -1,5 +1,21 @@ -def fibonacci( n ) +def FibonacciSequence( n ) return n if ( 0..1 ).include? n - ( fibonacci( n - 1 ) + fibonacci( n - 2 ) ) + ( FibonacciSequence( n - 1 ) + FibonacciSequence( n - 2 ) ) end -puts fibonacci( 5 ) + +DEFAULT = 10 + +n = DEFAULT + +if ARGV.length > 0 + n = ARGV[0].to_i +else + puts "Which Fibonacci index to find? " + n = ARGF.gets.to_i +end + +if n < -1 + n = DEFAULT +end + +puts "Fibonacci sequence number at index #{n} is #{FibonacciSequence(n)}"