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]
2 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]2 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
A. Calculate the concentration of a toxic (Cout) that is added to a lake with an input concentration of 100 μg/l. The flow rate
Mazyrski [523]

Answer:

Calt =42.129 mg/eit

Explanation:

See attached image

5 0
3 years ago
Student A says hazardous waste can take the form of solid, liquid, or gas. Student B says hazardous waste can only take the form
lina2011 [118]

Answer:

Student A

Explanation:

hope this helps have a great day

4 0
3 years ago
Programming Assignment 2 Decimal and IEEE-754 ConversionsObjective: To write a C program (not C++) that converts numbers between
kondaur [170]

Answer:

// Program is written in C Programming Language

// Comments are used for explanatory purpose

// Program starts here

#include<stdio.h>

#include<math.h>

//Function to Convert to float

void To float(int num, int I)

{

//Create a kount variable

int kount;

// Start an iteration

for(kount=i-1; kount>0; kount--)

{

if((num>>kount) && 1) {printf("1");}

else { printf("0"); }

}

}

// Create a user defined variable

typedef union {// Definition

float Number;

struct

{

// Mantissa

unsigned int mant : 23;

// Exponent

unsigned int exp : 8;

// Sign

unsigned int sign: 1;

} raw;

} myfloat; // Variable name

// Create print segment

void printsegment(myfloat var)

{

printf("%d |", var.raw.sign);// Sign

To float(var.raw.exp,8); // Exponent

printf("|");

To float(var.raw.mant,8); // Mantissa

printf("\n");

}

// Function to Convert to Real

unsigned int ToReal(int* dig[], int l, int h)

{

unsigned int f = 0, I;

Start an iteration

for(I = h; I>=l;I--)

{

// Calculate individual value

f = f + dig[I] * pow(2,h-1);

}

return f;

}

// Main method start here

int main()

{

printf("Floating Point Conversion\n");

printf("Select any of the following options\n");

printf("1. Decimal to IEEE754 Conversion\n");

printf("2. IEEE754 to Decimal Conversion\n");

printf("3. Quit");

// Declare integer variable for option

int opt;

// Prompt to select option

printf("Select an option; Option 1 to 3: ");

scanf("%d", $opt);

if(opt == 1)

{

printf("You have selected option 1");

// Declare a user defined variable and a system defined variable

myfloat var; float number;

// Accept input

scanf("%d", number);

// Check for special cases

if(isnan(number/0.0))// Not a number

{

printf("Not a Number");

}

else

{

var.f = number;

// Print Sign

printf("%d | ", var.raw.sign);

// Print Exponent

ToFloat(var.raw.exp,8);

printf(" | ");

// Print Mantissa

ToFloat(var.raw.mant,23);

}

}// End of option 1;

// Beginning of option 2

else if(opt == 2)

{

printf("You have selected option 2");

// Declare an array and two integer variables

unsigned int number[32];

int ctrlno, I = 0;

// Accept input by through an iteration

for(int k = 0; k < 32; k++)

{

// Create a label

label: scanf("%d", ctrlno);

// Check for special cases

if(isnan(ctrlno/0.0))// Not a number

{

printf("Not a Number"); I++;

break;

}

else if(ctrlno>1 || ctrlno < 0)

{

printf("Invalid Number\n Please enter a valid digit");

goto label;

}

else {

// Assign number to array

number[k] = ctrlno;

}

// Check validity of number

if(I != 0)

{

printf(" Invalid Number Representation");

}

else

{

// Declare user defined variable

myfloat var;

// Get sign

var.raw.sign = number[0];

// Get mantissa; From to 31

unsigned f = ToReal(number,9,31);

var.raw.mant = f;

// Get exponent; 1 to 8

f = ToReal(number,1,8);

var.raw.exp = f;

// Print Output

printf("The converted digit is ");

printf("%f", var.f);

}

else

{

// Quit Application

break;

}

return 0;

}

3 0
3 years ago
What is the<br> engineering
Oliga [24]

<em>Engineering</em><em> </em><em>is</em><em> </em><em>the</em><em> </em><em>profession</em><em> </em><em>which</em><em> </em><em>deals </em><em>with</em><em> </em><em>building</em><em> </em><em>the</em><em> </em><em>structures</em><em> </em><em>like</em><em> </em><em>bridge</em><em>,</em><em>house</em><em>,</em><em>roads</em><em> </em><em>etc</em><em>.</em>

<em>Hope</em><em> </em><em>it</em><em> </em><em>helps</em><em>.</em><em>.</em><em>.</em>

8 0
3 years ago
Read 2 more answers
In a voltage-divider biased pnp transistor, there is no base current, but the base voltage is approximately correct. The most li
N76 [4]

Answer:

the base-emitter junction is open and the emitter resistor is open

Explanation:

Because here there will be no base current only when the the base emitter junction is kept open and. Also when emitter resistor is kept open or with thus there will be no voltage drop across the Resistor meaning the base voltage Will be equal to that in the voltage divider circuitry

4 0
3 years ago
Other questions:
  • In order to test the feasibility of drying a certain foodstuff, drying data were obtained in a tray dryer with air flow over the
    14·1 answer
  • Estimate the theoretical fracture strength (in MPa) of a brittle material if it is known that fracture occurs by the propagation
    8·1 answer
  • A hollow aluminum sphere, with an electrical heater in the center, is used in tests to determine the thermal conductivity of ins
    6·1 answer
  • A long bone is subjected to a torsion test. Assume that the inner diameter is 0.375 in. and the outer diameter is 1.25 in., both
    14·1 answer
  • A cylinder with a piston restrained by a linear spring contains 2 kg of carbon dioxide at 500 kPa and 400°C. It is cooled to 40°
    7·1 answer
  • What friction rate should be used to size a duct for a static pressure drop of 0.1 in wc if the duct has a total equivalent leng
    9·1 answer
  • For laminar flow of air over a flat plate that has a uniform surface temperature, the curve that most closely describes the vari
    15·1 answer
  • The purpose of pasteurizing milk is to
    13·2 answers
  • When was solar power envold ​
    8·2 answers
  • Technician A says that a radio may be able to receive AM signals, but not FM signals if the antenna is defective. Technician B s
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!