Question

In: Computer Science

For C++ Function 1: Write a recursive function to perform a sequential search on a set...

For C++

  • Function 1:
    • Write a recursive function to perform a sequential search on a set of integers
    • The function will require an array parameter and the number to look for.
      • These are the minimal parameter requirements
      • The function should take an array of any size
  • Function 2:
    • Write a recursive function that will convert an integer (base 10) to binary
    • The function should only have an integer parameter
    • Have the function write the binary number to the console
    • You do not need to create a string nor put the characters into an array
      • Print out the binary number one digit at a time
  • Main Function:
    • ask the user for a number
    • search an array for the number
    • if found, display the binary representation of the number
    • if not found, display an appropriate message
  • You may create the array and fill it in with any numbers as you see fit. The size of the array should be 15 elements

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

#include <iostream>
using namespace std;
int search(int arr[],int key,int size)
{
size=size-1;
if(size <0)
{
return -1;
}
else if(arr[size]==key)
{
return size;
}
else
{
return search(arr,key,size);
}
}

void printToBinary(int num)
{
if (num>0)
{
printToBinary(num/2);
cout<<num%2 << " ";
}
}
int main()
{
int arr[]={1,12,33,4,10,-3,6,100,11,45,-5,-9,0,245,90};
int size=15;
int num;
   cout<<"Enter a number: ";
   cin>> num;
  
   int pos=search(arr,num,size);
   if(pos==-1)
   cout<<"The number is not found in the array."<<endl;
   else
   {
   cout<<"The number is found at "<<pos<<" position."<<endl;
  
   cout<<"The Binary representation of number is: ";
   printToBinary(num);
   }
   return 0;
}

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


Related Solutions

C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that computes the sum of the digits in an integer. Use the following function header: int sumDigits(int n) For example, sumDigits(234) returns 2 + 3 + 4 = 9. Write a test program that prompts the user to enter an integer and displays its sum.
In c++ Write a recursive driver function that will replace each of the odd values in...
In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
In the recursive version of binary search each function call reduces the search size by half....
In the recursive version of binary search each function call reduces the search size by half. Thus in the worst case for a sorted list of length n. The algorithm makes log n calls. Is it possible to make fewer than log n calls? Group of answer choices 1) Yes, depends on when it finds the element it is looking for 2) No, it will always make log n calls.
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT