Question

In: Computer Science

(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest...

(C++ )

·In “recursive.cpp”, write a recursive function minDoub() which:

·returns the address of the smallest value in the array. If the array is empty, return the “end” pointer

·and takes as parameters:

(1)   a pointer to double. The pointer is the address of the start of an array,

(2)   the “end” pointer to the address after the array

(3)   and the address of the smallest value seen so far

·Write main() to test this function – try a case where the array is not empty and a case where the array is “empty” (the first two parameters have the same value)

·Also in “recursive.cpp”, write a recursive function replaceFirst() which should replace the first instance of a target string with a new value. If the target is not in the array, nothing needs to be done. The function:

·Does not return anything

·and takes as parameters:

(1)   an array of string,

(2)   the number of strings in the array

(3)   a target string to replace

(4)   the new string to replace the target

(5)   the current index to check

·Add tests to main() for the replace function:

·Show the list before you replace anything

·Then after each replace

·Here is some sample final output:

Recursion practice

smallest of {3.0, -1.3, -2.4, 5.7} = -2.4

List is empty

Plants array before any replacing: rose sequoia tulip tulip

After 1th replace: rose sequoia taro tulip

After 2th replace: rose sequoia taro taro

After 3th replace: rose sequoia taro taro

·In a different file “templateEx.cpp”, write a single function, a generic function sum to add up the values in a vector parameter and return the sum.

·To get this to work more generally, there is a 2nd parameter init which is the starting value for the sum. For numbers, this is 0, but for strings, this is the empty string.

·The type parameter for this function will be the type of the elements of the vector.

·The only requirements for the type is that it support + and =

(1)   Something to think about is why you might not want to support non-numerical element types

·The attached file provides the main(). The expected output for the final program is:

Template example

Sum of ints should be 10, = 10

Sum of reals should be -0.5, = -0.5

Sum of words should be namespace, = namespace

templateEx.cpp provided code

#include <iostream>
#include <string>
#include <vector>

using namespace std;


int main() {
    cout << "Template example" << endl;
    vector<int> ints{1,2,3,4};
    vector<float> reals{-2.0f, 1.0f, 0.5f};
    vector<string> words{"name", "space"};
    cout << "Sum of ints should be 10, = "
         << sum(ints, 0) << endl;
    cout << "Sum of reals should be -0.5, = "
         << sum(reals, 0.0f) << endl;
    cout << "Sum of words should be namespace, = "
         << sum(words, string("")) << endl;
    return 0;
}

Solutions

Expert Solution

Here is the solution to above problem in C++. Please read the code comments for more information

GIVE A THUMBS UP!!!

C++ Code

#include<iostream>
using namespace std;

double * minDoub(double *array,double *min,double *end)
{
   //if array is null
   if(array==end)
       return min;
  
   //check if current element is smaller
   //than min, and update the min
   if(*array < *min)
   {
       min=array;
   }
   //return the minimum value address
   return minDoub(array+1,min,end);;
}


int main()
{
   double * a = new double[10];
   a[0]=3.0;
   a[1]=-1.3;
   a[2]=-2.4;
   a[3]=5.7;
   a[4]=22;
   double *temp = new double;
   double *end= &a[5];
   double *min = minDoub(a,temp,end);
   cout<<*min;
  
}

Screenshot of output


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.
Write a modification of the recursive binary search algorithm that always returns the smallest index whose...
Write a modification of the recursive binary search algorithm that always returns the smallest index whose element matches the search element. Your algorithm should still guarantee logarithmic runtime. Give a brief discussion of the best- and worst-case runtimes for this new algorithm as they compare to the original. NOTE: You do not have to re-write the entire algorithm. You just need to indicate any changes you would make and show the pseudocode for any portions that are changed. Example: Given...
Write a function that takes a valid stringlist and returns the index of the smallest element...
Write a function that takes a valid stringlist and returns the index of the smallest element in the list represented by the stringlist. You may not use split(). Examples: >>> stringlist min index('[123,53,1,8]') # 1 is smallest2 >>> stringlist min index('[1,2,345,0]') # 0 is smallest3 >>> stringlist min index('[5] ') # 5 is smallest0
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to...
scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to L except that every occurrence of x has been replaced with y. The following example illustrates the use of this function: > (subst 'c 'k '(c o c o n u t)) (k o k o n u t) Write a recursive Scheme function (all-different? L), which determines whether all elements of list L are distinct (that is, not equal?). The following example illustrates...
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
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) }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT