1. In main(), declare a variable N , input an integer less than 10 from standard
ID: 3617938 • Letter: 1
Question
1. In main(), declare a variableN, input an integer less than 10 from standardinput (keyboard). Then, define a function Fibonacci tocalculate the Fibonacci number, which is: 0, 1, 1, 2, 3, 5,8, 13, 21...
E.g. if you enter a 10, yourprogram will report 34
Note:N will be used as an argument of the function. Youare required to use a recursive function tocalculate the result. Finally, return the result to main()function, and output the result in main().
2. In main(), define a two dimensional integer arrayN[5][5] which has 5 rows and 5 columns, a total of25 integer elements. Initialize the array by generating randomnumbers between 70 and 100 usingfunction rand() and assigning them to the arrayintegers. Then define two arrays: S[5] andA[5]; and use loops to calculate the sum andaverage of the numbers in each row of N[5][5](i.e. numbers that have the same row index), assign the results toS[5] and A[5], respectively.Output S[5] and A[5] results.
To generate the random numberbetween 70 and 100, you can use the following code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
srand((unsigned)time(0));
int random_integer;
for(int index=0; index<20; index++){
random_integer =(rand()%30)+70;
cout <<random_integer << endl;
}
}
Explanation / Answer
#include<iostream> using namespace std; int fib(int,int,int,int); int main() { int N; int first=0,second=1,sum=0; cout<<"Enter a number for fabnocie: "; cin>>N; int result=fib(first,second,sum,N); cout<<"Fibnocie number at this position is :"<<result; system("pause"); return 0; }
int fib(int f, int s, int sum, int n) { if(n==1) return sum; else { f=s; s=sum; sum=s+f; n--; fib(f,s,sum,n); } }
#include<iostream> using namespace std; int fib(int,int,int,int); int main() { int N; int first=0,second=1,sum=0; cout<<"Enter a number for fabnocie: "; cin>>N; int result=fib(first,second,sum,N); cout<<"Fibnocie number at this position is :"<<result; system("pause"); return 0; }
int fib(int f, int s, int sum, int n) { if(n==1) return sum; else { f=s; s=sum; sum=s+f; n--; fib(f,s,sum,n); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.