O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Please use only ONE recursive-thank you- Please use c language- thank.docx

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Fibonacci surce code
Fibonacci surce code
Carregando em…3
×

Confira estes a seguir

1 de 2 Anúncio

Please use only ONE recursive-thank you- Please use c language- thank.docx

Baixar para ler offline

Please use only ONE recursive?thank you?
Please use c language? thank you?
1. (10 minutes) Write a program called fib.c that calculates the nth fibanocci number. The equation for the Fibonacci numbers is defined as follows: Fib(0)-0 Fib(1)=1 Fib(N)= Fib(N-1)+ Fib(N-2) 2. Name your executable fib.out 3. Your program should accept N as a command line argument 4. You MUST solve this program RECURSIVELY 5. Unlike the example in class, you may only have ONE RECURSIVE CALL in your function 1. Hint pointers help make this possible. 6. Here are the first 100 numbers in the Fibonacci sequence 7. 7. Examples 1../fib.out 0 2../fib.out 1 3../fib.out 10 The 0th fibanocci number is 0. The 1th fibanocci number is 1. The 10th fibanocci number is 55.
Solution
//All libraries needed for this program. #include #include #include #include //Function declarations. void fib(int fibNum1, int fibNum2, int count,int num); //This function takes in the input and recursively calls itself until it hits the //base cases, where it takes in the input and returns the nth fibonacci number. void fib(int fibNum1, int fibNum2, int count,int num) { //Base case where it stops when it is either one or 0 and prints out the //result. if (count <= 1) { // Base case: ran for user specified // number of steps, do nothing. //This is when the user inputs a 1, it will print out. if(num == 2 && fibNum1 == 0 && count == 0){ printf(\"The 1th fibanocci number is 1.\"); } //This is when the user inputs a 0, it will print out. else if(num == 2 && fibNum1 == 0){ printf(\"The 0th fibanocci number is 0.\"); } //Else, this is the classic base case, where the program will print out the //nth Fibonacci number once it is reduced down to 1. else{ printf(\"The %dth fibanocci number is %d.\ \",num, fibNum1 + fibNum2); } } else { // Recursive case: compute next value num++; fib(fibNum2, fibNum1 + fibNum2, count - 1,num); } return; } //Main function that will run int main(int argc,char**argv) { //Variable declarations. int runFor = 0; runFor = atoi(argv[1]); // User specified number of values computed int num = 2; //Function call to the function fib. fib(0, 1, runFor-1,num); return 0; }
.

Please use only ONE recursive?thank you?
Please use c language? thank you?
1. (10 minutes) Write a program called fib.c that calculates the nth fibanocci number. The equation for the Fibonacci numbers is defined as follows: Fib(0)-0 Fib(1)=1 Fib(N)= Fib(N-1)+ Fib(N-2) 2. Name your executable fib.out 3. Your program should accept N as a command line argument 4. You MUST solve this program RECURSIVELY 5. Unlike the example in class, you may only have ONE RECURSIVE CALL in your function 1. Hint pointers help make this possible. 6. Here are the first 100 numbers in the Fibonacci sequence 7. 7. Examples 1../fib.out 0 2../fib.out 1 3../fib.out 10 The 0th fibanocci number is 0. The 1th fibanocci number is 1. The 10th fibanocci number is 55.
Solution
//All libraries needed for this program. #include #include #include #include //Function declarations. void fib(int fibNum1, int fibNum2, int count,int num); //This function takes in the input and recursively calls itself until it hits the //base cases, where it takes in the input and returns the nth fibonacci number. void fib(int fibNum1, int fibNum2, int count,int num) { //Base case where it stops when it is either one or 0 and prints out the //result. if (count <= 1) { // Base case: ran for user specified // number of steps, do nothing. //This is when the user inputs a 1, it will print out. if(num == 2 && fibNum1 == 0 && count == 0){ printf(\"The 1th fibanocci number is 1.\"); } //This is when the user inputs a 0, it will print out. else if(num == 2 && fibNum1 == 0){ printf(\"The 0th fibanocci number is 0.\"); } //Else, this is the classic base case, where the program will print out the //nth Fibonacci number once it is reduced down to 1. else{ printf(\"The %dth fibanocci number is %d.\ \",num, fibNum1 + fibNum2); } } else { // Recursive case: compute next value num++; fib(fibNum2, fibNum1 + fibNum2, count - 1,num); } return; } //Main function that will run int main(int argc,char**argv) { //Variable declarations. int runFor = 0; runFor = atoi(argv[1]); // User specified number of values computed int num = 2; //Function call to the function fib. fib(0, 1, runFor-1,num); return 0; }
.

Anúncio
Anúncio

Mais Conteúdo rRelacionado

Semelhante a Please use only ONE recursive-thank you- Please use c language- thank.docx (20)

Mais de rtodd884 (20)

Anúncio

Mais recentes (20)

Please use only ONE recursive-thank you- Please use c language- thank.docx

  1. 1. Please use only ONE recursive?thank you? Please use c language? thank you? 1. (10 minutes) Write a program called fib.c that calculates the nth fibanocci number. The equation for the Fibonacci numbers is defined as follows: Fib(0)-0 Fib(1)=1 Fib(N)= Fib(N-1)+ Fib(N-2) 2. Name your executable fib.out 3. Your program should accept N as a command line argument 4. You MUST solve this program RECURSIVELY 5. Unlike the example in class, you may only have ONE RECURSIVE CALL in your function 1. Hint pointers help make this possible. 6. Here are the first 100 numbers in the Fibonacci sequence 7. 7. Examples 1../fib.out 0 2../fib.out 1 3../fib.out 10 The 0th fibanocci number is 0. The 1th fibanocci number is 1. The 10th fibanocci number is 55. Solution //All libraries needed for this program. #include #include #include #include //Function declarations. void fib(int fibNum1, int fibNum2, int count,int num); //This function takes in the input and recursively calls itself until it hits the //base cases, where it takes in the input and returns the nth fibonacci number. void fib(int fibNum1, int fibNum2, int count,int num) { //Base case where it stops when it is either one or 0 and prints out the //result. if (count <= 1) { // Base case: ran for user specified // number of steps, do nothing. //This is when the user inputs a 1, it will print out. if(num == 2 && fibNum1 == 0 && count == 0){ printf("The 1th fibanocci number is 1."); } //This is when the user inputs a 0, it will print out. else if(num == 2 && fibNum1 == 0){ printf("The 0th fibanocci number is 0."); } //Else, this is the classic base case, where the program will print out the //nth Fibonacci number once it is reduced down to 1. else{ printf("The %dth fibanocci number is %d. ",num, fibNum1 + fibNum2); } } else { // Recursive case: compute next value num++; fib(fibNum2, fibNum1 + fibNum2, count - 1,num); } return; } //Main function that will run int main(int argc,char**argv) { //Variable declarations. int runFor = 0; runFor = atoi(argv[1]); // User specified number of values computed int num = 2; //Function call to the function fib. fib(0, 1, runFor-1,num); return 0; }

×