From 693ac4770f873c56a416abf79e3813ada4acf376 Mon Sep 17 00:00:00 2001 From: yael kelmer Date: Wed, 6 Jul 2016 00:48:18 -0400 Subject: [PATCH] completed assignment 5 --- assignment5.txt | 18 +++++++++++++++ loopFibonacci.c | 30 +++++++++++++++++++++++++ recurseFibonacci.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 assignment5.txt create mode 100644 loopFibonacci.c create mode 100644 recurseFibonacci.c diff --git a/assignment5.txt b/assignment5.txt new file mode 100644 index 0000000..6bcbbfc --- /dev/null +++ b/assignment5.txt @@ -0,0 +1,18 @@ +Yael Kelmer. + +1. This is how you would call the function inside of the main() function: + +int main () { + + int a = 1; /* this initializes a value for a */ + float b = 2.5; /* this initializes a value for b */ + int c = 4; /* this initializes a value for c */ + + int fxn (a, b, c); /* this calls the function fxn and uses the arguments that were initialized */ +} + +2. The difference between recursion and iteration is that recursion is recursive and iteration goes step by step. In terms of runtime, iteration is preferable, because it will take much less time than a recursion. In terms of lines of code, recursion will take less lines of code, so in that sense it would be preferable to use recursion. However, recursion is so slow that the extra lines of code are worth it. + + +3. A compiler reads the human-readable, high-level language and converts it to assembly, which is the lowest level language. + diff --git a/loopFibonacci.c b/loopFibonacci.c new file mode 100644 index 0000000..d6102af --- /dev/null +++ b/loopFibonacci.c @@ -0,0 +1,30 @@ +/* Yael Kelmer. + This code prompts the user for the amount of elements of the Fibonacci series that they would like to see and the code uses a loop to give those numbers. */ + +#include + +int main () { + + printf ("Please type the amount of numbers of the Fibonacci series that you would like to see:\n"); + int numberOfElements; + scanf ("%d", &numberOfElements); + + int i; + int n; + int n1 = 1; + int n2 = 1; + + printf ("This is the first %d elements in the Fibonacci series\n", numberOfElements); + if (numberOfElements == 1) { + printf ("%d\n", n2); + } + else { + printf ("%d\n%d\n", n2, n1); + } + for (i = 0; i < numberOfElements - 2; i++) { + n = n1 + n2; + n2 = n1; + n1 = n; + printf ("%d\n", n); + } +} diff --git a/recurseFibonacci.c b/recurseFibonacci.c new file mode 100644 index 0000000..127616e --- /dev/null +++ b/recurseFibonacci.c @@ -0,0 +1,56 @@ +/* Yael Kelmer. + This code prompts the user for the amount of elements of the Fibonacci series that they would like to see and the code uses a recursion to give those numbers. */ + +#include + + /*This was my second attempt at fixing the code, but this printed all of the duplicate values that get calculated in the recursive calls. */ + /*int x; + int numberInSeries; + int fib (int x) { + if (x == 1) { + printf ("1\n"); + return 1; + } + if (x == 2) { + printf ("1\n"); + return 1; + } + else { + numberInSeries = fib (x-1) + fib (x-2); + printf ("%d\n", numberInSeries); + return numberInSeries; + } + }*/ + +/* this is my final code and it works! */ +int sum; +int fib(int x, int y, int z, int i) { + if (i >= z) { + return; + } + sum = x + y; + printf ("%d\n", sum); + return fib (y, x+y, z, i+1); + +} + +int main () { + int z; + printf ("Please type the amount of numbers in the Fibonacci series that you would like to see\n"); + scanf ("%d", &z); + + printf ("This is the first %d elements in the Fibonacci series\n", z); + printf ("1\n1\n"); + fib (1, 1, z, 2); + + /* This was my first attempt at printing all of the values for the Fibonacci series, but it was inefficient, so I tried another code. */ + /*int i; + int numberInSeries; + printf ("This is the Fibonacci series:"); + for (i = 1; i <= x; i++) { + numberInSeries = fib (i); + printf ("%d ", numberInSeries); + } + printf ("\n");*/ +} +