Question

DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General...

DO NOT use for loop and while loop

Goal:

Learn how to use recursive functions.

General requirement:

Use cout to fully demonstrate your works for all tasks. The output must clearly show how each of these functions work. If no output is displayed for a task below, 0 point will be given to the task. If the output is not clear, some points may be deducted.   

Task 1 – Reverse array (3 pts)

Given an integer array like:

const int SIZE =9;

int arrInt[SIZE]={11,22,33,44,55,66,77,88,99};

Use a recursive function to reverse the values in the array. That is, after calling the function the array should be like:

     arrInt[SIZE]={99,88,77,66,55,44,33,22,11};

Task 2 – Array sum: Linear recursive version (3 pts)

   Use a linear recursive function to add the integers of the array in Task 1 and display the sum.

Task 3 – Array sum: Binary recursive version (4 pts)

   Use a binary recursive function to add the integers of the array in Task 1 and display the sum.

Task 4 – Linear search: Recursive version (5 pts)

   Use a linear recursive function to search an integer in an array and display the index of the target.

Task 5 – Binary search: Recursive version (5 pts)

   Use a binary recursive function to search an integer in an array and display the index of the target.

#include <iostream>

using namespace std;

int linearSearch(int [], int, int, int);

int binarySearch(int [], int, int, int);

int main()

{

    const int SIZE =9;

    int arrInt[SIZE]={11,22,33,44,55,66,77,88,99};

    /// Enter your testing code for Task 1, 2 and 3 here

    cout << endl << "Task 4" << endl;

    int target = 99;

    int pos = linearSearch(arrInt, 0, SIZE, target);

    cout << "Linear search: The position of the target " << target << " is " << pos << endl;

    target = 66;

    pos = linearSearch(arrInt, 0, SIZE, target);

    cout << "Linear search: The position of the target " << target << " is " << pos << endl;

    cout << endl << "Task 5" << endl;

    target = 99;

    pos = binarySearch(arrInt, 0, SIZE - 1, target);

    cout << "Binary search: The position of the target " << target << " is " << pos << endl;

    target = 66;

    pos = binarySearch(arrInt, 0, SIZE - 1, target);

    cout << "Binary search: The position of the target " << target << " is " << pos << endl;

    return 0;

}

int linearSearch(int arrInt[], int pos, int sz, int target)

{

/// TO DO: ADD YOUR CODE HERE

}

int binarySearch(int arrInt[], int begIndex, int endIndex, int target)

{

    /// TO DO: ADD YOUR CODE HERE

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code

============================================================================================

#include <iostream>

using namespace std;

int linearSearch(int [], int, int, int);

int binarySearch(int [], int, int, int);


void reverseArray(int [],int,int);

int linearSum(int [],int);

int binarySum(int[],int,int);

void display(int arr[],int n)
{
   int i;
   for(i=0;i<n;i++)
   cout<<arr[i]<<" ";
   cout<<"\n";
}

int main()

{

const int SIZE =9;

int arrInt[SIZE]={11,22,33,44,55,66,77,88,99};

/// Enter your testing code for Task 1, 2 and 3 here
  
//Task 1
cout << endl << "Task 1" << endl;
cout<<"Array before Reverse: ";
display(arrInt,SIZE);
reverseArray(arrInt,0,SIZE-1);
cout<<"Array After Reverse: ";
display(arrInt,SIZE);

  
//Task2
cout << endl << "Task 2" << endl;
cout<<"Array Sum using Linear Recusrion: "<<linearSum(arrInt,SIZE);
cout<<endl;
  
//Task3
cout << endl << "Task 3" << endl;
cout<<"Array Sum using Binary Recusrion: "<<binarySum(arrInt,0,SIZE-1);
cout<<endl;

cout << endl << "Task 4" << endl;
reverseArray(arrInt,0,SIZE-1);
int target = 99;

int pos = linearSearch(arrInt, 0, SIZE, target);

cout << "Linear search: The position of the target " << target << " is " << pos << endl;

target = 66;

pos = linearSearch(arrInt, 0, SIZE, target);

cout << "Linear search: The position of the target " << target << " is " << pos << endl;

cout << endl << "Task 5" << endl;

target = 99;

pos = binarySearch(arrInt, 0, SIZE - 1, target);

cout << "Binary search: The position of the target " << target << " is " << pos << endl;

target = 66;

pos = binarySearch(arrInt, 0, SIZE - 1, target);

cout << "Binary search: The position of the target " << target << " is " << pos << endl;

return 0;

}

void reverseArray(int arrInt[], int begIndex, int endIndex)
{
   if(begIndex>=endIndex)
       return;
   int temp=arrInt[begIndex];
   arrInt[begIndex]=arrInt[endIndex];
   arrInt[endIndex]=temp;
  
   reverseArray(arrInt,begIndex+1,endIndex-1);
  
}

int linearSum(int arrInt[],int n)
{
   if(n==1)
   {
       return arrInt[0];
   }
   else if(n>0)
   {
       return linearSum(arrInt,n-1)+arrInt[n-1];
   }
   else
   {
       return -1;
   }
}


int binarySum(int arrInt[], int begIndex, int endIndex)
{
  
   if(begIndex>endIndex)
   return 0;
   else if(begIndex==endIndex)
   return arrInt[begIndex];
   else
   {
       int mid=(begIndex+endIndex)/2;
       return binarySum(arrInt,begIndex,mid)+binarySum(arrInt,mid+1,endIndex);
   }
}

int linearSearch(int arrInt[], int pos, int sz, int target)

{
       if(sz<0) { // Base case - not found
return -1;
}
if(arrInt[sz]==target) { // Base case - found
return sz;
}
// Recursive case
return linearSearch(arrInt,0, sz-1, target);

}

int binarySearch(int arrInt[], int begIndex, int endIndex, int target)

{
   if(begIndex>endIndex)
   return -1;
   int mid=(begIndex+endIndex)/2;
   if(arrInt[mid]==target)
       {
           return mid;
       }
   else if(arrInt[mid]>target)
   {
           return binarySearch(arrInt,begIndex,mid-1,target);
   }
   else
   {
           return binarySearch(arrInt,mid+1,endIndex,target);
   }
  

}

============================================================================================

Output

Add a comment
Know the answer?
Add Answer to:
DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Note: None of these functions should use cout. It is OK to use cout while debugging...

    Note: None of these functions should use cout. It is OK to use cout while debugging to check how your function is working, but your final submission should not contain cout in any function but main. Head ==== Name the source file for this section head.cpp. Write a function named "head" which takes an array of integers as an argument, and returns the first element in the array. Write a function named main which outputs the result of testing your...

  • /* * Program5 for Arrays * * This program illustrates how to use a sequential search...

    /* * Program5 for Arrays * * This program illustrates how to use a sequential search to * find the position of the first apparance of a number in an array * * TODO#6: change the name to your name and date to the current date * * Created by Li Ma, April 17 2019 */ #include <iostream> using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() {...

  • * This program illustrates how to use a sequential search to find the position of the...

    * This program illustrates how to use a sequential search to find the position of the first apparance of a number in an array TODO#6: change the name to your name and date to the current date * * Created by John Doe, April 17 2019 */ #include using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() { //TODO#1: declare an integer array named intList with size of...

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • You will be reading in 3 files in the program. One will contain a list of...

    You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...

  • You are making a .h file. Implement a recursive binary search function. bSearch passes in an...

    You are making a .h file. Implement a recursive binary search function. bSearch passes in an array of integers, the size of the array, and the value the function is searching for. The function should return the index where found or -1 if not found. You will want to implement a recursive helper function that passes in the array, the value to be located, and the beginning and ending of the range of values to search within. Use the provided...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array...

    Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT