This is C++ Design a function template called randomly Select that receives two
ID: 3676614 • Letter: T
Question
This is C++
Design a function template called randomly Select that receives two arguments: the array of data values and its size. Then randomly returns one of the component in the array. For example, suppose that we have the following declarations: Then the function should return 1, 3, 4, 10, 7, or 9. You may use the function defined in cstdlib, to return a random value between 0 and RAND-MAX. For more details on how to use this function, you may visit: www.cplusplus.com/reference/clibrary/cstdlib/rand.html. Write a C++ program to test the function randomlySelect and name it testRandoml ySelect.cpp. Your program should use the function to randomly select a component in the list of different data type. Additionally, it should print the results of the random selections on the screen. Write the main function of your program. Note that you will need to include the header file in your program.Explanation / Answer
/**C++ program that test the method randomlySelect that takes a generic
array and size of the array . Then print the random value from the generic
array*/
//testRandomlySelect.cpp
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
//generic array function template
template<typename T>
T randomlySelect(T arr[], int size)
{
//Seed a random value
srand(time(NULL));
//Create a random value
int r=rand()%size;
//store the result in the type T
T result=arr[r];
//returns result
return result;
}
int main()
{
//Create a integer list
int listInt[]={1,3,4,10,7,9};
//Create a listDouble list
double listDouble[]={4.26, 89.59, 88, 67.85, 76.24,69.46};
//Create a listChar list
char listChar[]={'a','d','6','8','t','e','s'};
//Create a listString list
string listString[]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
//print the random value in listInt
cout<<"Randomly selected value from listInt : "
<<randomlySelect(listInt,(sizeof(listInt)/sizeof(*listInt)))<<endl;
//print the random value in listDouble
cout<<"Randomly selected value from listDouble : "
<<randomlySelect(listDouble,(sizeof(listDouble)/sizeof(*listDouble)))<<endl;
//print the random value in listChar
cout<<"Randomly selected value from listChar : "
<<randomlySelect(listChar,(sizeof(listChar)/sizeof(*listChar)))<<endl;
//print the random value in listString
cout<<"Randomly selected value from listString : "
<<randomlySelect(listString,(sizeof(listString)/sizeof(*listString)))<<endl;
//pause the program output on console
system("pause");
return 0;
}
--------------------------------------- --------------------------------------- ---------------------------------------
Sample output:
Run1:
Randomly selected value from listInt : 7
Randomly selected value from listDouble : 76.24
Randomly selected value from listChar : e
Randomly selected value from listString : Saturday
Run2:
Randomly selected value from listInt : 7
Randomly selected value from listDouble : 76.24
Randomly selected value from listChar : s
Randomly selected value from listString : Sunday
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.