In: Computer Science
C++ homework
URGENT
Select ONE of the following scenarios and write an appropriate function to produce the required result.
Please write down which scenarios you have selected (a, b or c).
a) Write a function that will accept a sentence and a letter and the function return will be the number of times the sent letter appears in the sent sentence. Example; if the sentence is "hello world" and the letter is “o” the return value should be 2.
b) OR Write a function that will receive a minimum and maximum value and returns a random number between them. Example; if the minimum value is 5 and and maximum value is 10, the function should return a random number between [5 and 10].
c) OR Write a function that will receive a string value and return a single character response based on user input. The function must validate the user’s input before it returns the user’s response.
Answer b.
rand() function is used in C++ to generate random integers.
rand()%a + b; //here a random number is generated between a and b where a>b.
The function :
int func(int min,int max){
int temp = rand()%max+min;
return(temp);
}
Entire Code :
#include <iostream>
using namespace std;
int func(int min,int max){
int temp = rand()%max+min;
return(temp);
}
int main()
{
int min,max;
cout<<"Enter the smaller number : ";
cin>>min;
cout<<"Enter the larger number : ";
cin>>max;
cout<<func(min,max);
return 0;
}