Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ question: Write a program that will do the following steps: - generate 2 ran

ID: 3566314 • Letter: C

Question

C++ question:

Write a program that will do the following steps: - generate 2 random integer numbers between 0 and 20 - calculate the square root of each number - calculate the average of the 2 numbers - print the 2 random numbers - print the square root of each random number with 3 digits after the decimal point - print the average of the 2 numbers with 1 digit after the decimal point

Example program output:
The two random numbers are 2 and 3

The average is 2.5

The square root of 2 is 1.214

The square root of 3 is 1.372

Explanation / Answer

#include <iostream>
#include <ctime> // Needed for the true randomization
#include <cstdlib>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
int range = 20;
float average;
float sqrt1;
float sqrt2;

srand( time(0)); // This will ensure a really randomized number by help of time.

   int rand1 = rand()%range+1;
int rand2 = rand()%range+1;
   cout << "The 2 random numbers are:" << endl << rand1 << endl << rand2 << endl;
   average = (rand1 + rand2) /2;
   cout << "The average is " << fixed << setprecision(1) << average << endl;
   sqrt1 = sqrt(rand1);
   cout << "The square root of " << rand1 << " is " << fixed << setprecision(3) << sqrt1 << endl;
   sqrt2 = sqrt(rand2);
   cout << "The square root of " << rand2 << " is " << fixed << setprecision(3) << sqrt2 << endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote