Home > Enterprise >  Why did we use this temp variable?
Why did we use this temp variable?

Time:11-20

why did we use the temp variable ? what does it mean in the for loop ?

#include <iostream>

using namespace std;

int main()
{
    float a, x, sqrt;
    double temp;

    cout << "type a number: " << endl;
    cin >> a;
    x = 1.5;
    sqrt = 0.5 * (x   (a / x));
    cout << sqrt << endl;
    for (int i = 0; i < a; i  ) {
        temp = sqrt;
        sqrt = 0.5 * (sqrt   (a / sqrt));
        if (temp == sqrt) {
            return 0;
        }
        cout << sqrt << endl;
    }

    return 0;
}

CodePudding user response:

Temp is normally just a variable name used for something that will be deleted soon or will soon become out of scope.

In this code, it is just used to check if sqrt had changed, if sqrt had not changed it can return 0. So it is temporarily storing what sqrt used to be, so then we can compare what it is now with what it used to be later.

CodePudding user response:

It seems that you are using the temp variable to store the initial value of the "sqrt" variable at the start of each loop iteration, before calculating a new value for "sqrt" and comparing it against its initial value (stored in temp).

Based on your program, it will run loop until the loop condition no longer applies, or until the formula "0.5 * (sqrt (a / sqrt));" outputs the same as its input, meaning sqrt is no longer changing.

CodePudding user response:

To some extend this question is about maths, not coding.

The coding part:

The loop stops when sqrt does not change after applying

sqrt = 0.5 * (sqrt   (a / sqrt));

temp stores the value of sqrt before applying that formula and compared to sqrt after updating it.

The maths part can be investigated with the help of eg Wolfram Alpha. Merely typing the above equation (= -> == and sqrt -> s to not be confused with the square root function) will tell us some information, among it also that sqrt = - sqrt(a) is an integer solution (ie a fixpoint of iteratively applying the update again and again.

TL;DR: The code calculates the square root of a by applying the same update to sqrt until sqrt does not change. To detect if sqrt changes, the value before (temp) and after is compared.

CodePudding user response:

It seems the program tries to find an approximation of the square root of a.

sqrt will become an ever better approximation.

It starts with an estimate of 1.5 and then iteratively uses the formula

             sqrt   a/sqrt
next sqrt =  -------------
                   2

When sqrt is larger/smaller than the real square root of a, a / sqrt is smaller/larger the the real square root. So taking the average gives a better approximation.

It sets a raw boundary of steps to a itself.

Every approximation is printed.

The old and new sqrt value need to be compared. If they are equal one should stop too. For that temp stores a copy of the (old) sqrt.

When there are no changes to sqrt (it is a/sqrt, close to the real square root) it would continue emitting the same value, when not exiting the program with return 0;.


You see that x, the initial guess of 1.5, is not really need as variable, especially with such no-name moniker. Also the approximation formula applied redundantly on x. Of course you do not want to start every approximation sequence by outputing 1.5, but it could have been written neater.

  •  Tags:  
  • c
  • Related