Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions assignment5.txt
Original file line number Diff line number Diff line change
@@ -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 */

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right! but make sure that the function is declared before the main function to avoid a warning.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but what is recursion? and how about memory usage? which is better?



3. A compiler reads the human-readable, high-level language and converts it to assembly, which is the lowest level language.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right!


30 changes: 30 additions & 0 deletions loopFibonacci.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

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);
}
}
56 changes: 56 additions & 0 deletions recurseFibonacci.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

/*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");*/
}