1 Write a program that prompts you to input a positive float

1. Write a program that prompts you to input a positive floating point value. If the value is negative, print an error message. otherwise, find the square root of the value using a brute force iterative loop.

[INTI] Initialize the accuracy variable to 0.01

If the entered value is greater than one,

initialize the guess variable to the entered value

Otherwise,

initialize the guess variable to 1.0

[COND CHECK] If the square of the guess is greater than the entered value,

perform the following:

[LOOP ACTION] None

[ACTION @ END] Reduce the guess by the accuracy variable and repeat the loop.

After the loop, print out the calculated square root (the Guess Variable).

The above loop description suggests that this algorithm is easily implemented using a for ( ) or while ( ) loop.

use these variables:

float Accuracy;

float EnteredValue;

float Guess;

2- add a second loop with Accuracy set to 0.001 ( be sure to reinitialize Guess to the entered value ) and print out the more accurate calculated square root.

I start with this but I do not know what is th next step:

#include <stdio.h>

void
main(void)

{
//declare variables
float Accuracy;
float EnteredValue;
float Guess;

printf(\"Enter a positive number: \");
scanf(\"%f\", &EnteredValue);
//printf(\"Entered: %f\ \", EnteredValue);

if (EnteredValue < 0)
{
   printf(\"***ERROR-number cannot be negative ***\ \");
}
else if(EnteredValue < 1.0)
{
   Guess = 1.0;
}
else
{
   Guess = EnteredValue;
}
printf(\"Guess: %f\ \",Guess);

}

Solution

SOURCE CODE:

#include <stdio.h>

int main()
{
//declare variables
float Accuracy;
float EnteredValue;
float Guess;
float Guess_prev;

printf(\"Enter a positive number: \");
scanf(\"%f\", &EnteredValue);

if (EnteredValue < 0)
{
printf(\"***ERROR-number cannot be negative ***\ \");
}
else if(EnteredValue < 1.0)
{
Guess = 1.0;
}
else
{
Guess = EnteredValue;
Accuracy = 0.01;
Guess_prev = 0.0;
while(Guess!=Guess_prev)
{
   Guess_prev = Guess;
   if((Guess*Guess)>EnteredValue)
       Guess = Guess - Accuracy;
}

printf(\"Guess: %f\ \",Guess);
}
if (EnteredValue < 0)
{
printf(\"***ERROR-number cannot be negative ***\ \");
}
else if(EnteredValue < 1.0)
{
Guess = 1.0;
}
else
{
Guess = EnteredValue;
Accuracy = 0.001;
Guess_prev = 0.0;
while(Guess!=Guess_prev)
{
   Guess_prev = Guess;
   if((Guess*Guess)>EnteredValue)
       Guess = Guess - Accuracy;
}

printf(\"More Accurate Guess: %f\ \",Guess);
}
return 1;
}

OUTPUT:

Enter a positive number: 3.56
Guess: 1.880002
More Accurate Guess: 1.886108

--------------------------------

Enter a positive number: 0.478
Guess: 0.690000
More Accurate Guess: 0.691004

--------------------------------

Enter a positive number: -9.78
***ERROR-number cannot be negative ***
***ERROR-number cannot be negative ***

--------------------------------

1. Write a program that prompts you to input a positive floating point value. If the value is negative, print an error message. otherwise, find the square root
1. Write a program that prompts you to input a positive floating point value. If the value is negative, print an error message. otherwise, find the square root
1. Write a program that prompts you to input a positive floating point value. If the value is negative, print an error message. otherwise, find the square root

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site