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
16 changes: 16 additions & 0 deletions Assignment5
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Assignment 5 Sheqi Zhang
1. How would you call the following function? Give an example of what it would look like written inside of the main() function and explain your code in detail.

```
int fxn(int a, float b, int c)
```
Answer:
int call = fxn(a,b,c)

2. Explain the difference between recursion and iteration. When/why is one preferable over the other? Why is recursion worse in other cases?
Answer:
Recursion is the function that call itself. Iteration is a repitition of process. Recursion is short and simple, while iteration is faster, and also takes less memory.

3. How does a compiler work? Explain step by step.
A compiler translates the high-level programming language like c to excutable code, which can be read by the computer. It first break up the text into "tokens", then converse these tokens into parse tree. Next, it evaluate constant expressions, and finally, it translate the parse tree into machine instructions.

19 changes: 19 additions & 0 deletions loopFibonacci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>

int main(){
int n,c;
printf("Please type in a number:\n");
scanf("%d",&n);
int fibonacci[n];
for(c=0;c<n;c++){
if(c<=1)
fibonacci[c]=1;
else
fibonacci[c]=fibonacci[c-1]+fibonacci[c-2];
}
printf("%d",fibonacci[n]);
return 0;
}



22 changes: 22 additions & 0 deletions recursionFibonacci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>

long int fibonacci(int n){
if(n==0)
return 1;
else if(n==1)
return 1;
else
return fibonacci(n-1)+fibonacci(n-2);
}


int main(){
int n,i=0,a;
printf("Please type in a number:\n");
scanf("%d",&n);
for(a=0;a<n;a++){
printf("%ld",fibonacci(i));
i++;
}
return 0;
}