In: Computer Science
Language C++
Thank You!
-Write a function getMeASentence() that takes zero arguments, and its return type is string. This function will return a randomly generated sentence that abides by certain rules.
-The rules of this sentence are that there are 5 randomly generated lowercase letters, a space, a randomly generated inequality symbol, a space, and a randomly generated 1 digit number.
-In this context, an inequality symbol is understood to mean the "<", ">", or "=" symbol.
-Write a function isItCloseEnough(double, double, double) that takes three arguments of type double, num1, num2, and cutoff. Its return type is Boolean. It will return true if num1 and num2 are within a distance of cutoff, false otherwise. It is up to you to decide what value to return if the distance is exact.
-In your main function, write code that calls the getMeASentence() function 2 separate times, with appropriate labels, to test that it is working.
-In your main function, also write code to test the IsItCloseEnough(double, double, double) function by prompting the user to enter three numbers, and then call the function and output the results with appropriate labels. You should do this THREE times to test it properly.
Sample Output (remember that random values may vary)
Sentence 1: bksde > 6
Sentence 2: zloxr = 9
Enter 3 numbers: 2.1 2.3 0.3
Yes the numbers are within the cutoff.
Enter 3 numbers: 2.3 2.1 0.3
Yes the numbers are within the cutoff.
Enter 3 numbers: 2.3 2.1 0.1
No the numbers are not within the cutoff.
*Explanation*: In all cases, the distance between the two entered numbers is 0.2. If the cutoff is 0.3 units, then yes, the numbers are within the cutoff distance, but if the cutoff is 0.1 like that last case, then that is considered to be no.
Answered On : 15 - Oct - 2020
PLEASE NOTE : FEEL FREE TO ASK ANY DOUBTS by COMMENTING
Language : C++ Programming
IDE : Dev C++
::::::::::::::::::::::::::::::::::::::::::::::::: CODE :::::::::::::::::::::::::::::::::::::::::::::
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
string getMeASentence(){
string s = "";
char ascii;
// generate random letters for 5 times
for(int i=0; i<5; i++){
ascii = 'a'+rand()%26;
s = s+ascii;
}
// add space and comparator symbol
s = s+" ";
ascii = '<'+rand()%3;
// add space and digit
s = s+ascii+" ";
ascii = '0'+rand()%10;
s = s+ascii;
// return string
return s;
}
bool isItCloseEnough(double num1,double num2,double
cutoff){
// calculate difference
double diff = num1-num2;
// if difference is negative make it
positive
if(diff < 0){
diff = -diff;
}
// if difference within cutoff, return true,else
false
if(diff <= cutoff){
return true;
}
return false;
}
int main(){
srand(time(NULL));
// print random sentence for 2 times
cout << "Sentence 1: " << getMeASentence()
<< endl;
cout << "Sentence 2: " << getMeASentence()
<< endl;
double num1,num2,cutoff;
for(int i=0; i<3; i++){
cout << "Enter 3
numbers: ";
cin >> num1 >>
num2 >> cutoff;
if(isItCloseEnough(num1,num2,cutoff)){
cout <<
"Yes the numbers are within the cutoff.\n";
}
else{
cout<<"No
the numbers are not within the cutoff.\n";
}
}
return 0;
}
::::::::::::::::::::::::::::::::::::::::::::::: OUTPUT :::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::: CODE in EDITOR :::::::::::::::::::::::::::::::::::::
_________________________________________________________________
Dear Friend, Feel Free to Ask Any Doubts by Commenting. ASAP i'll respond when i'm available.
I'm on a critical Situation. Please Do Not Forget To Give A Thumbs UP +1. It will Helps me A Lot.
Thank YOU :-)