Question

In: Computer Science

Language C++ Thank You! -Write a function getMeASentence() that takes zero arguments, and its return type...

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.

Solutions

Expert Solution

Answered On : 15 - Oct - 2020

PLEASE NOTE : FEEL FREE TO ASK ANY DOUBTS by COMMENTING

  • If you want me to add/change anything, Please let me know by commenting.
  • I used comments for better understandin. Comments starts with '//'
  • Please refer to the screenshot of the code to understand the indentation of the code

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 :-)


Related Solutions

C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array using a template. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a general example of interrupts in C language with comments. Thank you
Write a general example of interrupts in C language with comments. Thank you
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT