(1) Short Recursion Practice a) A prime number is an integer that cannot be divi
ID: 3559183 • Letter: #
Question
(1) Short Recursion Practice
a) A prime number is an integer that cannot be divided by anyinteger other than one and itself. For
example, 7 is prime because its only divisors are 1 and 7. Theinteger 8 is not a prime number because it
is divisible by 1, 2, 4, and 8. In a class called RecursionTester,write a recursive static method called
isPrime(int x) that returns a boolean with the value true if andonly if the given integer x is a prime
number. Assume that x is positive. The method should have no loopsof any kind. You may use
indirect recursion ... the following definitions should help giveyou some ideas:
isPrime(1) = true
isPrime(N) = isPrime(N, N-1)
isPrime(N, 1) = true
isPrime(N, X) = false if X divides N evenly, otherwise isPrime(N,X-1)
b) Write a method in the RecursionTester class called sort(int[] v)which takes an integer array and
sorts the array in increasing order. Note that the method shouldsort the array given, not create any new
arrays. Also, you MUST write the method recursively to get anymarks. You will want to use a helper
method. (Hint: Move left to right through the array recursively,swapping a pair of elements if the one on the left is larger
than the one on the right. Use a parameter to keep track of theposition of the element pair that you are examining. If you
reach the end of the array and no items have been swapped, then thearray is sorted now and you can stop. Otherwise, you
will need to restart the swapping process from the beginning of thearray. You'll need to keep track of whether or not you
made a swap...use another parameter). It may be helpful to write amethod that will display your array, so that
you can use this for debugging. Here is an example of how to testit from a main method, assuming that
you have a method called displayArray to display the array:
int[] v = {3, 6, 9, 2, 7, 1, 5, 8, 4, 2, 2, 1};
sort(v);
displayArray(v);
Make sure to hand in your testing for both of these methods. Tryvarious test cases.
(2) The MAZE GUI
Create a class called Maze that represents a maze in which a robotcan move up, down, left or right in
(i.e., a grid that allows only horizontal and vertical movements).A maze can have walls or open
areas. A robot cannot pass through the walls but may move freelythrough the open areas. Each grid
location is identified by a row and a column number (starting at0,0) and may store "something" to
indicate whether a wall is in that location. Here is an example ofa 10x10 maze:
Get the 4 text files labeled maze1.txt, maze2.txt, maze3.txt andmaze4.txt from the course website that
represent the following mazes:
Create a GUI in a class called MazeSolver that displays the mazes.Here are the GUI expectations:
Explanation / Answer
program to find prime number using recursion
void prime(int x, int y)
{ int waa;
if ( x == 1 )
x++;
if(x <= y)
{
waa = isPrime(x,2); // second input parameter added if(waa != 0)
{ printf("%5d",x);
} prime(x+1,y);
} } int isPrime(int n, int i)
{ if(n%i==0 && n!=2 && n!=i)
{ return(0);
}
else
{
if (i < sqrt(n))
{
return( isPrime(n,i+1) );
} else return 1;
} }
int main()
{ int num[2];
printf("Input 2 numbers: ");
scanf("%d %d", &num[0], &num[1]); prime(num[0], num[1]); return 0;
}
______________________________________________________________________________________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.