C++ Thinking recursively - No loops , i solved the first one i need help with the others
int factR(int n)
{ // Find the factorial of a number
if (n == 0) return 1;
return n * factR(n - 1);
return 0;
}
2) // Linear Search
int searchR(const int data[], int first, int last, int
lookFor)
{
return 0;
}
3)
int binarySearchR(const int data[], int first, int last, int
lookFor)
{
return 0;
}
int raiseToPowerR(int base, int exponent)
{
// Raise base to the power exponent
return 0;
}
int productR(const int data[], int cellsUsed)
{
// Find the product of integers in an array
return 0;
}
int sumR(const int data[], int cellsUsed)
{
// Find the sum of integers in an array
return 0;
}
Solved Function:
int searchR(const int data[], int first, int last, int
lookFor)
{
if(last<first)
return -1
if(data[first]==lookFor)
return first;
if(data[last]==lookFor)
return last;
return searchR(data,first+1,last-1,lookFor);
}
int binarySearchR(const int data[], int first, int last, int
lookFor)
{
if(last<first)
return -1;
else{
int midElement=first+(last-1)/2;
if(data[midElement]==lookFor)
return mid;
if(data[midElement]>x)
return binarySearchR(data,1,midElement-1,lookFor);
return binarySearchR(data,midElement+1,last,lookFor);
}
}
int raiseToPowerR(int base, int exponent)
{
if(exponent!=0)
return (base*raiseToPowerR(base,exponent-1));
else
return 1;
}
int productR(const int data[], int cellsUsed)
{
if(cellsUsed<=0)
return 1;
else
return (data[cellsUsed-1]*productR(data,cellsUsed-1));
}
int sumR(const int data[], int cellsUsed)
{
if(cellsUsed<=0)
return 0;
return (sumR(data,cellsUsed-1+data[cellsused-1]));
}
if you like the answer please provide a thumbs up.
C++ Thinking recursively - No loops , i solved the first one i need help with...
PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE TEST CODE FOR IT. 5) Design a recursive function to find the immediate successor of a target integer in an array o:f sorted integers. Here the immediate successor of a target is defined as the smallest number that is no smaller than the target in this sorted array int binarySuccessor(const int anArrayl, const int first, const int last, int target); In the above function...
I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. * * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...
I need help with this program. C++ Given a string, compute recursively (no loops) a new string where all appearances of "pi" have been replaced by "3.14". It should enter a string like: (xpix)--> and print out " x3.14x" (pi)--> and print out " 3.14p" (pipi)--> and print out " 3.143.14" This is what I have so far... #include<iostream> #include<string> using namespace std; string changePi(string str) { string left; if(str.length() < 2) return str; if(str.substr(0, 1).compare("pi")) return "3.14" + changePi(str.substr(2));...
I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...
Can I get some help with this? please and thanks Cost vouw 1. A simple linear Search algorithm on an unsorted array (30 points) Statements 1 int linearSearch( int [ ]a, int n, int target ) { 2 int i = 0; 3 while (i<n) { if (target == array[i]) return i; i++; 7 } 8 return -1; 9 } 2. The number of statements executed for calculating the average value of a two-dimensional array with n*n elements (30 points)...
PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...
C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...
may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...
Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...
I need help with this assignment. My answers are in bold but I am not getting the correct output which is also below. Can you please take a look at it. #include <stdlib.h> #include <stdio.h> #include <float.h> #include <math.h> // PURPOSE: To define a nickname for type 'unsigned int'. typedef unsigned int uInt; //-- Sign related constants --// // PURPOSE: To tell how many bits to shift the sign field from the // least...