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

Write a program that will do the following steps: - generate 2 random integer nu

ID: 660924 • Letter: W

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

Hi, since the langue of the program is not mention I have presented the answer assuming that it is in CPP

First, for the first part of the program. We require a code that will generate two random numbers between 1 and 20. C++ comes witha built inb pseudo random number generator. It is generateda as two separate function under the header : #include<cstdlib>

The block of code for the first part of the program to display two random numbers is :

#include <iostream>
#include<cstdlib>
#include<time.h>
using namespace std;


int main()
{
const int number = 2;
const int total = 20;
int value[number];

srand(time(NULL));


for (int i=0;i<number;i++)
{
bool check;
int n;
do
{
n=rand()%total;

check=true;
for (int j=0;j<i;j++)
if (n == value[j])
{
check=false;
break;
}
} while (!check);
value[i]=n;
cout << value << endl;
}
}

Now that we have the two numbers we need. We are to perform the operations listed. So we might want to declare two variables say num1,num2 early in the program. So as we get the to random variables we can fetch the variables using cin into the declared variables (num1 and num2). To carry out the operatonms mentioned above we can include the #include<cmath> and also #include<iomanip> for setting precision. Of course you can also use printf.

so the complete code would be :

#include <iostream>
#include<cstdlib>
#include<time.h>
#include<iomanip>
#include<cmath>

using namespace std;


int main()
{
const int number = 2;
const int total = 20;
int value[number];
int num1,num2;
double average;

srand(time(NULL));


for (int i=0;i<number;i++)
{
bool check;
int n;
do
{
n=rand()%total;

check=true;
for (int j=0;j<i;j++)
if (n == value[j])
{
check=false;
break;
}
} while (!check);
value[i]=n;
cout << value << endl;
cin >> num1;
cin >> num2;
average = (num1 + num2)/2 ;
cout << "The average is : " << average << endl;
cout << " The square root of " << num1 << "is " << sqrt(num1) << endl;
cout << " The square root of " << num2 << "is " << sqrt(num2) << endl;
}
}

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