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

The <cmath> function sqrt(x) is used to calculate the exact square-root value of

ID: 3809657 • Letter: T

Question

The <cmath> function sqrt(x) is used to calculate the exact square-root value of the number x such that x is a non-negative real number. The square root value of x can also be estimated using the following mathematical equation: sqrt X = pow(exp, ( 0.5 * ln * X ) Where e = 2.718282, and ln is the natural logarithm; called log() in <cmath>.

Write a C++ program that calculates and writes to an output file the exact and estimated square roots of a list of numbers. The list of numbers is either read from the keyboard, generated randomly or read from an input file. The program displays to the user the following menu and performs the corresponding tasks based on the user’s selection. The program should keep running until the user enters ‘Q’ or ‘q’.
K: Read the list of numbers from the keyboard R: Generate the list of numbers randomly F: Read the list of numbers from an input file Q: Quit the program

If the user enters ‘K’ or ‘k’, the program should read a list of numbers terminated by Ctrl+z from the keyboard. If the user enters ‘R’ or ‘r’, the program should ask the user to specify how many numbers to generate randomly and then generates that many random values. The generated numbers MUST range between 5.000 and 20.000. For this purpose use the rand() function to return values between 5000 and 20000 and divide them by 1000. If the user enters ‘F’ or ‘f’, the program should read the list of numbers from an input file which could be empty or contain any number of values given individually on separate lines. The name of the input file is provided by the user at run-time. For each of the 3 menu options K, R and F, the program should calculate the exact and estimated square roots of the obtained list of numbers and write the results in an output file called ‘square_roots.txt’. The program should ignore (skip) any negative values in the list of numbers and do the calculations and display the results only for the non-negative values. The output should be written to the output file in tabular format including, for each non-negative number x in the list, the number x itself, its exact root, its estimated root, and the absolute value of the difference between the two roots. Format the x’s with 3 digits after the decimal point and format the roots and the difference between them with 6 digits after the decimal point. The <cmath> function sqrt(x) is used to calculate the exact square-root value of the number x such that x is a non-negative real number. The square root value of x can also be estimated using the following mathematical equation: sqrt X = pow(exp, ( 0.5 * ln * X ) Where e = 2.718282, and ln is the natural logarithm; called log() in <cmath>.

Write a C++ program that calculates and writes to an output file the exact and estimated square roots of a list of numbers. The list of numbers is either read from the keyboard, generated randomly or read from an input file. The program displays to the user the following menu and performs the corresponding tasks based on the user’s selection. The program should keep running until the user enters ‘Q’ or ‘q’.
K: Read the list of numbers from the keyboard R: Generate the list of numbers randomly F: Read the list of numbers from an input file Q: Quit the program

If the user enters ‘K’ or ‘k’, the program should read a list of numbers terminated by Ctrl+z from the keyboard. If the user enters ‘R’ or ‘r’, the program should ask the user to specify how many numbers to generate randomly and then generates that many random values. The generated numbers MUST range between 5.000 and 20.000. For this purpose use the rand() function to return values between 5000 and 20000 and divide them by 1000. If the user enters ‘F’ or ‘f’, the program should read the list of numbers from an input file which could be empty or contain any number of values given individually on separate lines. The name of the input file is provided by the user at run-time. For each of the 3 menu options K, R and F, the program should calculate the exact and estimated square roots of the obtained list of numbers and write the results in an output file called ‘square_roots.txt’. The program should ignore (skip) any negative values in the list of numbers and do the calculations and display the results only for the non-negative values. The output should be written to the output file in tabular format including, for each non-negative number x in the list, the number x itself, its exact root, its estimated root, and the absolute value of the difference between the two roots. Format the x’s with 3 digits after the decimal point and format the roots and the difference between them with 6 digits after the decimal point.

Explanation / Answer

Program:-

#include<iostream>
#include<fstream>   //for open files
#include<cstdlib>
#include<cmath>       //for sqrt()
#include<stdlib.h>   //for exit(0)
#define exp 2.718282
using namespace std;
int main()
{
   fstream file1("square_roots.txt",ios_base::out);   //open output file
   int i=0,n;
   float x;
   char ch;
   float estsquareroot,squareroot;
   do
   {
       cout<<"K: Read the list of numbers from the keyboard R: Generate the list of numbers randomly F: Read the list of numbers from an input file Q: Quit the program"<<endl;
       cout<<"Enter your choice :";
       cin>>ch;
       fstream file("Num.txt");   //open input file
       switch(ch)
       {
           case 'K':
           case 'k':while (1)
                   {
                       cout<<"enter a number"<<endl;
                       cin>>x;       //read number from key board
                       squareroot=sqrt(x);       //calculate square root
                       estsquareroot=pow(exp, ( 0.5 * log(x)));       //calculate estimated square root
                       file1<<x <<" "<<squareroot<<" "<<estsquareroot<<endl;   //printing number , Square root , Estimated Square root
                   }
               break;
           case 'R':
           case 'r':cout<<"Enter how many numbers to generate :"<<endl;
                   cin>>n;
                   while (i<n)
                   {
                       x=5000+rand()%20000;   //generating random numbers
                       x=x/1000;
                       i++;
                       squareroot=sqrt(x);       //calculate square root
                       estsquareroot=pow(exp, ( 0.5 * log(x)));       //calculate estimated square root
                       file1<<x <<" "<<squareroot<<" "<<estsquareroot<<endl;   //printing number , Square root , Estimated Square root
                   }
               break;
           case 'F':
           case 'f':
                   while(file>>x)
                   {
                       if(x>0)
                       {
                           squareroot=sqrt(x);       //calculate square root
                           estsquareroot=pow(exp, ( 0.5 * log(x)));       //calculate estimated square root
                           file1<<x <<" "<<squareroot<<" "<<estsquareroot<<endl; //printing number , Square root , Estimated Square root
                       }
                   }
                   break;
           case 'Q':
           case 'q':exit(0);
       }
       file.close();
       file1.close();
   }while(ch!='Q'||ch!='q');
}


input:-

K: Read the list of numbers from the keyboard
R: Generate the list of numbers randomly
F: Read the list of numbers from an input file
Q: Quit the program
Enter your choice :k
enter a number
1
enter a number
5
enter a number
25
enter a number
9
enter a number
7
enter a number
6
enter a number
4
enter a number
55
enter a number
ctrl+pause/break

output:-

1   1   1
5   2.23607   2.23607
25   5   5
9   3   3
7   2.64575   2.64575
6   2.44949   2.44949
4   2   2
55   7.4162   7.4162
55   7.4162   7.4162

input:-

K: Read the list of numbers from the keyboard
R: Generate the list of numbers randomly
F: Read the list of numbers from an input file
Q: Quit the program
Enter your choice :r
Enter how many numbers to generate :
10
K: Read the list of numbers from the keyboard
R: Generate the list of numbers randomly
F: Read the list of numbers from an input file
Q: Quit the program
Enter your choice :

Output:-

5.041   2.24522   2.24522
23.467   4.84427   4.84427
11.334   3.3666   3.3666
11.5   3.39117   3.39117
24.169   4.9162   4.9162
20.724   4.55236   4.55236
16.478   4.05931   4.05931
14.358   3.7892   3.7892
11.962   3.45861   3.45861
9.464   3.07636   3.07636

input:-

K: Read the list of numbers from the keyboard
R: Generate the list of numbers randomly
F: Read the list of numbers from an input file
Q: Quit the program
Enter your choice :F
K: Read the list of numbers from the keyboard
R: Generate the list of numbers randomly
F: Read the list of numbers from an input file
Q: Quit the program
Enter your choice :

num.txt:-
4
16
25
3
64
5
7
9

Output:-

4   2   2
16   4   4
25   5   5
3   1.73205   1.73205
64   8   8
5   2.23607   2.23607
7   2.64575   2.64575
9   3   3


*note:-All outputs are in file square_roots.txt