need help to get this program done please. thank you ... Write a program that pe
ID: 3620499 • Letter: N
Question
need help to get this program done please. thank you ...Write a program that performs following functionalities in recursion.
Your program should contain following functions:
1. fibonacci: a recursive function that computes the Fibonacci series, which is defined as follows
fibonacci (n) = fibonacci(n-2) + fibonacci(n-1)
fibonacci(0) = 0
fibonacci(1) = 1
So the Fibonacci series looks like: 0 1 1 2 3 5 8 13 21 34 …. Therefore, fibonacci(4) = 3,
fibonacci(5) = 5, fibonacci(7)=13
2. power: a recursive function that takes two integer parameters and returns xy. Assuming x is
nonzero,
power(x,y) = 1 if y = 0
x *power( x, y-1) if y > 0
1/ x-y if y < 0
3. greatestCommonDivisor: a recursive function that computes the greatest common divisor of
two positive integers.
The greatest common divisor of two non-zero integers is the largest positive integer that divides
both numbers without remainder.
gcd(x,y) = x if y = 0
gcd( y, x mod y) if x>=y and y > 0
For example, gcd(5, 0) is 5, gcd(18, 12) is 6 and gcd(56, 42) is 14.
4. Find and erase eagle: Write a program that examines and counts the number of objects (eagles)
in a photograph. The data is in a two-dimensional grid of cells, each of which may be empty
(value 0) or filled (value 1 to 9). Maximum grid size is 50 x 50. The filled cells that are
connected form an object (eagle). Two cells are connected if they are vertically, or horizontally.
The following figure shows 3 x 4 grids with 3 eagles.
0 0 1 2
1 0 0 0
1 0 3 1
• Your program should include one recursive functions : Find2EraseEagle.
- Find2EraseEagle function takes as parameters the 2-D array and the x-y coordinates of a cell
that is a part of an eagle (non-zero value) and erases (change to 0) the image of an eagle. The
function Find2EraseEagle should return an integer value that counts how many cell has been
erased (this means the size of the erased eagle).
• There should be other functions to complete the program.
- Your program will read input for erase eagle from the data file “data.txt”.
- Print out the picture of the photograph, and number of eagles found in each picture.
- As the program discovers each eagle, it should also print the size of the eagle just found.
- Process all the pictures in the file.
• Sample input: The following sample data has two pictures, the first one is 3 x 4, and the second one is 5
x 5 grids. Note that your program should be able to handle any number of pictures that are contained in
the data file.
3 4
0 0 1 2
1 0 0 0
1 0 5 1
5 5
0 0 0 1 1
0 1 8 1 9
0 0 0 0 0
1 0 0 2 1
1 3 1 9 1
Sample output:
0 0 1 2
1 0 0 0
1 0 5 1
An eagle size 2 is found and erased.
An eagle size 2 is found and erased.
An eagle size 2 is found and erased.
3 eagle(s) found in the picture.
0 0 0 1 1
0 1 8 1 9
0 0 0 0 0
1 0 0 2 1
1 3 1 9 1
An eagle size 6 is found and erased.
An eagle size 8 is found and erased.
2 eagle(s) found in the picture.
Requirements:
• Your program should provide a menu as following:
Which function would you like to use? Type a number.
1. Fibonacci series
2. Power
3. Greatest Common Divisor
4. Find and Erase Eagle
5. Exit
• When the user selects a choice, your program should prompt for input if necessary and perform the
appropriate function or terminate the program. The result should be printed with an appropriate
heading.
• Assume that user always input valid values for each task but invalid choice should be handled.
• No global variables are allowed
Explanation / Answer
please rate - thanks message me if any problems #include <iostream>#include <fstream>
using namespace std;
int power(int,int);
int greatestCommonDivisor(int,int);
int fibonacci (int);
int menu();
int Find2EraseEagle(int[][10],int,int,int,int);
void getandprint(ifstream&, int[][10],int&,int&);
int main()
{int base,exponent;
int n,a,array[10][10],x,y,b,choice,i,j,k,count;
ifstream in;
in.open("data.txt"); //open file
if(in.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
choice=menu();
while(choice!=5)
{if(choice==1)
{ cout<<"Enter the number of terms of the Fibonacci sequence: ";
cin>>n;
for( i=0;i<n;i++)
cout<<fibonacci (i)<<" ";
cout<<endl;
}
else if(choice==2)
{ cout<<"Enter a base ";
cin>>base;
cout<<"Enter an exponent ";
cin>>exponent;
cout<<base<<"^"<<exponent<<" = "<<power(base,exponent)<<endl;
}
else if(choice==3)
{cout<<"Enter 1st number: ";
cin>>a;
cout<<"Enter 2nd number: ";
cin>>b;
cout<<"Their gcd is "<<greatestCommonDivisor(a,b)<<endl;
}
else if(choice==4)
{count=0;
getandprint(in,array,x,y);
for(i=0;i<x;i++)
for(j=0;j<y;j++)
{k=Find2EraseEagle(array,x,y,i,j);
if(k>0)
{count++;
cout<<"An eagle size "<<k<<" is found and erased ";
}
}
cout<<count<<" eagle(s) found in the picture. ";
}
cout<<endl<<endl;
choice=menu();
}
in.close();
return 0;
}
int Find2EraseEagle(int a[][10],int rows,int columns,int r,int c)
{if(r<0||r>=rows||c<0||c>=columns)
return 0;
if(a[r][c]==0)
return 0;
int size=1;
a[r][c]=0;
size+=Find2EraseEagle(a,rows,columns,r-1,c);
size+=Find2EraseEagle(a,rows,columns,r+1,c);
size+=Find2EraseEagle(a,rows,columns,r,c-1);
size+=Find2EraseEagle(a,rows,columns,r,c+1);
return size;
}
void getandprint(ifstream& in, int a[][10],int& x,int& y)
{int i,j;
cout<<endl;
in>>x>>y;
for(i=0;i<x;i++)
for(j=0;j<y;j++)
in>>a[i][j];
for(i=0;i<x;i++)
{for(j=0;j<y;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
int menu()
{int choice;
do{
cout<<"1. Fibonacci series ";
cout<<"2. Power ";
cout<<"3. Greatest Common Divisor ";
cout<<"4. Find and Erase Eagle ";
cout<<"5. Exit ";
cin>>choice;
if(choice<1||choice>5)
cout<<"Invalid choice ";
}while(choice<1||choice>5);
return choice;
}
int greatestCommonDivisor(int a,int b)
{if (a % b == 0)
return b;
else
return
greatestCommonDivisor(b, a % b);
}
int fibonacci (int n)
{if(n<2)
return n;
else return fibonacci (n-1)+fibonacci (n-2);
}
int power( int num1, int num2)
{
if(num2==0)
return 1;
else if (num2==1)
return num1;
else
return num1 * power(num1, num2-1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.