Question

In: Computer Science

please do it in C++, will up vote!! please add snap shots of result and explanation....

please do it in C++, will up vote!! please add snap shots of result and explanation.

You are to create a recursive function to perform a linear search through an array.

How Program Works

Program has array size of 5000

Load values into the array, equal to its index value. Index 5 has value 5, index 123 has value 123.

Pass array, key, and size to the recursive function:

int recursiveLinearSearch(int array[],int key, const int size, bool & methodStatus)

User enters key to search for, recursive function calls itself until the key is found (methodStatus is true), print out the key and number of function calls when control is returned to the main

Handle situation of key not found (return number of function calls AND methodStatus of false) – print not found message and number of calls in the main

Function returns a count of how many recursive calls were made

Value returned is the number of calls made to that point, that is, when item is found the value returned is 1 with preceding functions adding 1 to the return value until the actual number of recursive function calls are counted).

Determine smallest key value that causes stack-overflow to occur, even if you need to make array larger than 5000.

Test cases need to include, biggest possible key value, “not found” message, and a stack overflow condition.

--------------------------------------

sry mixed 2 questions!

Solutions

Expert Solution

#include <iostream>
using namespace std;

int recursiveLinearSearch(int array[],int key, const int size, bool & methodStatus)
{
    int count=size;
    if(size<0)
    {
        methodStatus=false;
        return 5000;
    }
    if(size==key)
    {
        methodStatus=true;
        return (5000-size);
    }
    count--;
    recursiveLinearSearch(array,key,count,methodStatus);
}

int main()
{
    int arr[5000];
    for(int i=0;i<5000;i++)
    {
        arr[i]=i;
    }
    bool methodStatus;
    int key=20000;
    int size=5000;
    int count=recursiveLinearSearch(arr,key,size,methodStatus);
    cout<<" Total calls: "<<count<<" Key: "<<key<<" methodStatus: "<<methodStatus<<endl;
    key=4000;
    count=recursiveLinearSearch(arr,key,size,methodStatus);
    cout<<" Total calls: "<<count<<" Key: "<<key<<" methodStatus: "<<methodStatus;
}

The methodstatus 0 means not found and 1 means found. The recursive function works from size n to 0;

I hope it helps.


Related Solutions

please do it in C++, will up vote!! please add snap shots of result and explanation....
please do it in C++, will up vote!! please add snap shots of result and explanation. You are to create a recursive function to perform a linear search through an array. How Program Works Program has array size of 5000 Load values into the array, equal to its index value. Index 5 has value 5, index 123 has value 123. Pass array, key, and size to the recursive function: int recursiveLinearSearch(int array[],int key, const int size, bool & methodStatus) User...
Excel to VBA Please post with screen shots Add a few Check Box controls or Option...
Excel to VBA Please post with screen shots Add a few Check Box controls or Option Button controls to a worksheet and then use a Select/Case code structure in a sub procedure that outputs a message to the user telling him which box or option has been selected.
Please understand that a wrong answer from you will result in a down vote, negative feedback,...
Please understand that a wrong answer from you will result in a down vote, negative feedback, and a tweet to chegg support. I have less tolerance towards incompetency when my money is spent on your talent. Provide detail explanation and type your answer. 11) What are Bench Marking Recommendations are all about ? it is relating to bench marking concept
Please understand that a wrong answer from you will result in a down vote, negative feedback,...
Please understand that a wrong answer from you will result in a down vote, negative feedback, and a tweet to chegg support. I have less tolerance towards incompetency when my money is spent on your talent. Provide detail explanation and type your answer. 18) why products are sold below the cost price ?
Please understand that a wrong answer from you will result in a down vote, negative feedback,...
Please understand that a wrong answer from you will result in a down vote, negative feedback, and a tweet to chegg support. I have less tolerance towards incompetency when my money is spent on your talent. Provide detail explanation and type your answer. 17) Disadvantages of Cost + Pricing ?
Please add the explanation. 6. Killnum Corp. announces that the dividend for the next year will...
Please add the explanation. 6. Killnum Corp. announces that the dividend for the next year will be $2.50 per share rather than the originally expected $1.50 per share. From then on, it is expected that dividends will resume their historical constant growth rate of 5% per year. What would you expect to happen to the price of the stock? Ignore any tax effects. A) The price will likely double. B) The price will likely rise by less than 100%. C)...
please add the explanation 10. The government has been trying to decide whether or not to...
please add the explanation 10. The government has been trying to decide whether or not to purchase any of the new, advanced missiles it has developed. One of the arguments in favor of purchasing the missiles is that since so much money has been spent on their development it would be a waste of money not to buy them now. What is the major problem with this argument? A) It includes erosion costs in the decision-making process. B) It includes...
Please add the explanation. 9. Which of the following would be considered a use of funds?...
Please add the explanation. 9. Which of the following would be considered a use of funds? I. An increase in receivables II. An increase in payables III. An increase in inventory IV. An increase in sales A) I and III only B) I and IV only C) II and III only D) II and IV only E) I, III, and IV only
Write a C++ function template to add two inputs and return their result. Make exceptions for...
Write a C++ function template to add two inputs and return their result. Make exceptions for Characters (such that sum of two characters is a character associated with sum of their ASCII) and String (such that sum of two strings is their concatenation)
Please add the explanation. 3.A project costs $475 and has cash flows of $100 for the...
Please add the explanation. 3.A project costs $475 and has cash flows of $100 for the first three years and $75 in each of the project's last five years. If the discount rate is 10%, what is the discounted payback period? A) The project never pays back on a discounted basis B) 5 years C) 6 years D) 7 years E) 8 years
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT