1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
natka813 [3]
3 years ago
14

In c the square root of a number N can be approximated by repeated calculation using the formula NG = 0.5(LG + N/LG) where NG st

ands for next guess and LG stands for last guess. Write a function that calculates the square root of a number using this method. The initial guess will be the starting value of LG. The program will com- pute a value for NG using the formula given. The difference between NG and LG is checked to see whether these two guesses are almost identical. If they are, NG is accepted as the square root; otherwise, the next guess (NG) becomes the last guess (LG) and the process is repeated (another value is computed for NG, the difference is checked, and so on). The loop should be repeated until the difference is less than 0. 005. Use an initial guess of 1. 0. Write a driver function and test your square root function for the numbers 4, 120. 5, 88, 36.01, 10,000, and 0. 25
PLEASE İN C PROGRAMMİNG
Engineering
1 answer:
DanielleElmas [232]3 years ago
8 0

Answer:

Following are the program to the given question:

#include <stdio.h>//header file

double square_root(double N, double initialGuess)//defining a method square_root that takes two variable in parameters

{

double NG, LG = initialGuess,diff;//defining double variable

while(1)//use loop to calculate square root value

{

NG = 0.5 * (LG + N / LG);//using given formula

diff = NG - LG;//calculating difference

if(diff < 0)//use if to check difference is less than 0

diff = -diff;//decreaing difference

if(diff < 0.005)//use if that check difference is less than 0.005

break;//using break keyword  

else//defining else block

{

LG = NG;//holding value

}

}

return NG;//return value

}

int main()//defining main method

{

double ans, n,initialguess = 1.0;//defining double variable

n = 4;//use n to hold value

ans = square_root(n, initialguess);//calculating the square root value and print its value

printf("square_root(%lf) = %lf \n", n, ans);//print calculated value with number

n = 120.5;//use n to hold value

ans = square_root(n, initialguess);//calculating the square root value and print its value

printf("square_root(%lf) = %lf \n", n, ans);//print calculated value with number

n = 36.01;//use n to hold value

ans = square_root(n, initialguess);//calculating the square root value and print its value

printf("square_root(%lf) = %lf \n", n, ans);//print calculated value with number

n = 0.25;//use n to hold value

ans = square_root(n, initialguess);//calculating the square root value and print its value

printf("square_root(%lf) = %lf \n", n, ans);//print calculated value with number

printf("\nEnter a number: ");//print message

scanf("%lf", &n);//input value

ans = square_root(n, initialguess);//calculating the square root value and print its value

printf("square_root(%lf) = %lf \n", n, ans);//print calculated value with number

}

Output:

Please find the attachment file.

Explanation:

  • In this code, a method "square_root" is declared that takes two variable "N, initialGuess" in its parameters, inside the method a three double variable is declared.
  • It uses the given formula and uses the diff variable to hold its value and uses two if to check its value is less than 0 and 0.005 and return its calculated value.
  • In the main method, three double variables are declared that use the "n" to hold value and "ans" to call the method that holds its value and print its value.

You might be interested in
Use the case structure in order to do the following tasks. a) Use a five layer case structure in order to do following for a two
Igoryamba

Answer:

The complete answer along with step by step explanation and output results is provided below.

Explanation:

Task a)

#include<iostream>

using namespace std;

int main()

{

  int op;

  double num1, num2;

   cout<<"Enter num1 and num2"<<endl;

   cin>>num1>>num2;  

  // To provide the option of required 5 cases  

  cout << "Select the operation:"

          "\n1 = Addition"

          "\n2 = Subtraction"

          "\n3 = Multiplication"

          "\n4 = Division"

          "\n5 = Negation\n";

  cin >> op;  // user input the desired operation

  switch(op)  // switch to the corresponding case according to user input

   {

       case 1:

           cout <<"The Addition of num1="<<num1<<" and num2="<<num2<<" is: "<<num1+num2;

           break;

       case 2:

           cout <<"The Subtraction of num1="<<num1<<" and num2="<<num2<<" is: "<<num1-num2;

           break;

       case 3:

           cout <<"The Multiplication of num1="<<num1<<" and num2="<<num2<<" is: "<<num1*num2;

           break;

       case 4:        

        while(num2 == 0) // to check if divisor is zero  

        {

           cout << "\nWrong divisor! Please select the correct divisor again: ";

           cin >> num2; // if divisor is zero then ask user to input num2 again

        }

           cout <<"The division of num1="<<num1<<" and num2="<<num2<<" is: "<<num1/num2;

           break;

       case 5:

           cout <<"The Negation of num1="<<num1<<" and num2="<<num2<<" is: "<<-1*num1<<" "<<-1*num2;

           break;

       default:

           // If the operation is other than listed above then error will be shown

           cout << "Error! The selected operatorion is not correct";

           break;

   }

  return 0;

}

Output:

Test 1:

Enter num1 and num2

2

9

Select the operation:

1 = Addition

2 = Subtraction

3 = Multiplication

4 = Division

5 = Negation

1

The Addition of num1=2 and num2=9 is: 11

Hence the output is correct and working as it was required

Test 2:

Enter num1 and num2

8

0

Select the operation:

1 = Addition

2 = Subtraction

3 = Multiplication

4 = Division

5 = Negation

4

Wrong divisor! Please select the correct divisor again: 2

The Division of num1=8 and num2=2 is: 4

Hence the output is correct and working as it was required

Test 3:

Enter num1 and num2

-2

4

Select the operation:

1 = Addition

2 = Subtraction

3 = Multiplication

4 = Division

5 = Negation

5

The Negation of num1=-2 and num2=4 is: 2 -4

Hence the output is correct and working as it was required

Task b)

#include<iostream>

#include<cmath>   // required to calculate square root

using namespace std;

int main()

{

  int op;

  double num;

   cout<<"Enter a real number > 0"<<endl;

   cin>>num;

  cout << "Press 1 for square root:";

  cin >> op;

  switch(op)  // switch to the corresponding case according to user input

   {

       case 1:

          if (num <= 0) // to check if number is less or equal to zero

        {

           cout << "\nError! number is not valid";

           break;   // if number is not valid then terminate program

        }

           cout <<"The square root of num="<<num<<" is: "<<sqrt(num);

// if number is valid then square root will be calculated

           break;

       default:

           // If the operation is other than listed above then error will be shown

           cout << "Error! The selected operation is not correct";

           break;

   }

  return 0;

}

Output:

Test 1:

Enter a real number > 0

6

Press 1 for square root: 1

The square root of num=6 is: 2.44949

Hence the output is correct and working as it was required

Test 2:

Enter a real number > 0

-4

Press 1 for square root: 1

Error! number is not valid

Hence the output is correct and working as it was required

8 0
3 years ago
An engine indicator is used to determine the following: (a) speed (d) m.e.p, and IHP (e) BHP (b) temperature (c) volume of cylin
Greeley [361]

Answer:

Option (d) MEP and IHP

Explanation:

MEP stands for Mean Effective Pressure and IHP stands for Indicated Horse Power

In engines (Internal Combustion), engine indicator is generally to indicate the indicate the changes in pressure inside the cylinder of an Internal Combustion Engine or IC engines. Once, Mean Effective Pressure of the engine is calculated it further helps to calculate the Horse power and both these quantities, i.e., MEP and IHP are displayed on the engine indicator.

8 0
3 years ago
Which of the following have the capacity to display formatted data?
sukhopar [10]

Answer:

D.  A and B

Explanation:

1. The method Console.Write() is an overloaded method in a language like C#.

One of its variations could be as follows;

<em>Console.Write(String format, Object a, Object b).</em>

This contains three parameters and will write the text representation of the specified objects to the standard output stream using the information specified by the format specifier. Parameter 1 is <em>format</em> which is a composite format string representing the format specifier. Parameter 2 is <em>a</em>, which is the first object to be written using <em>format. </em>Parameter 3 is b, which is the second object to be written using <em>format</em>.

2. The method Console.WriteLine() has the same characteristics as Console.Write() above, except that it writes the text representation of the specified objects, followed by current line terminator then to the standard output stream using the information specified by the format specifier.

3. Console.WriteFormat() does not exist, at least not in C# or .NET

Therefore, Console.Write() and Console.WriteLine() have the capacity to display formatted data.

<em>Hope this helps!</em>

6 0
3 years ago
Why does my man bun not have its own erodynamics
Aloiza [94]

Answer:

umm okay for starters I have no clue lol.

7 0
3 years ago
Read 2 more answers
Often an attacker crafts e-mail attacks containing malware designed to take advantage of the curiosity or even greed of the reci
Mademuasel [1]

Answer:

1) The ethical act Amy should display is to open the Email since it came in through her personal inbox. However, further action should not be taken since ordinarily opening the mail will not disclose or corrupt her system either locally of her system on the server and web space.

2)  If such email came in, the first action i will take is to open the mail then take a pause to read through the instruction because most scam or malware comes with an array of further instructions.

After carefully reading through, i will call up Davey to confirm if he sent such mail which answer will obviously be a No and in the case where i am unable to get a call through to Davey, i will immediately delete the mail from my inbox to avoid the mistake of clicking any embedded link within the mail.

5 0
3 years ago
Other questions:
  • Consider a thermal energy reservoir at 1500 K that can supply heat at a rate of 150,000 kJ/h. Determine the exergy of this suppl
    15·1 answer
  • What are the functions of the peripheral nervous system
    6·2 answers
  • 1. Two aluminium strips and a steel strip are to be bonded together to form a composite bar. The modulus of elasticity of steel
    11·1 answer
  • Determine if the fluid is satisfied​
    10·1 answer
  • it creates parts from thin plastic sheets as opposed to plastic pellets. is it 1. Pickling 2. Thermoforming 3. Extrusion​
    13·2 answers
  • I have a question.What does DIY mean?
    6·2 answers
  • Periodic lubrication and oil changes according to manufacturer’s recommendations extend the life of your vehicle, and allow you
    15·1 answer
  • What is shown in the above figure
    11·1 answer
  • Which step in the engineering design process does not come before building a<br> prototype?
    12·1 answer
  • Aqueous cleaners are ________ parts cleaning agents.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!