goldshakil / ElectricalProgrammingCourse2020

This repository is aimed towards sharing and discussing issues about Electrical and Electronics Programming Course
6 stars 1 forks source link

Practice Session 4 _#Q.3 #121

Open EEE2017Assignments opened 4 years ago

EEE2017Assignments commented 4 years ago

TA,

  1. Can I use 'printf' in fibonacci function?

I mean in main function I just write ' Fibonacci(n); ' then I can get the output with fibonacci numbers.

This is some part of my Fibonacci function. : void Fibonacci(int n) {

printf("%d ", f1);
if (n == 1)
    return;

and the end of the function I call next fibonacci function to recurrence. Is it okay to make my fibonacci function to calculate numbers and print the numbers by itself?

  1. Can I use global variables?

This is connected with first question. I declare and be initialization global variables for my fibonacci function. Is it okay?

I always appreciate your efforts!

goldshakil commented 4 years ago

No that is not okay. The whole goal of recursion is based on using the local variables so you can't use global variables.

Please check my video i gave a sample code.

You have to call the fibonacci function from the main and print the result in the main function

e.g. printf(fibonacci(3)); // this should print the 3rd term in the fibonacci sequence.

Moreover, you fibonacci function should return an integer (not void) which represents the nth term in the fibonacci sequence.

If you have more questions let me know.

EEE2017Assignments commented 4 years ago

Thank you, TA.

So I changed my code. This is a part of the code included function declaration and main function.

int Fibonacci(int n); int Factorial(int n);

int main() { int n, i;

printf("Enter a single integer: ");
scanf("%d",&n);

for (i = 1;i <= n;i++)
    printf("%d ", Fibonacci(i));

printf("\nFactorial of n: %d\n", Factorial(n));

return 0;

}

If there is no problem with that code, then my assignment is okay?

goldshakil commented 4 years ago

This is correct :)