In: Computer Science
(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; }
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