0

I'm a beginner in programming and trying to learn C++ by the book Programming principles and practice using C++. In some parts of the book there are little exercises that you can try to do, one of this exercises is about calculating the square of a number, here is what my book says :

Implement square() without using the multiply operator, that is, do the x * x by repetead addition (start a variable result to 0 and add x to it x times).

I've already found a solution for this program but my first tentative was something like this :

#include <iostream>

int main()
{
    int a = 0;
    std::cout << "Enter an integer value : ";
    std::cin >> a;

    while (a < a * a)
    {

        a += a;
        std::cout << a << "\n";

    }
}

I know this code is wrong but I can't understand the output of the progam, if I enter 5 the program prints 10 20 30 40 50 until 8000, why the for loop doesn't stop when a is greater than its square ? I'm just curious to undersant why

piero borrelli
  • 737
  • 2
  • 9
  • 20
  • 1
    I'm not sure what you're doing here, but `a < a * a` seems nonsensical. Is `5 < 5 * 5`? – tadman Nov 26 '14 at 19:27
  • 2
    `while (a < a * a)` is always true. – Maroun Nov 26 '14 at 19:27
  • `a` is a variable; its value changes during its lifetime. The condition `a < a * a` is reevaluated using the current value of `a` every time through the loop. The reason the loop can stop at all is that `a * a`, when `a` is large enough, can overflow the largest positive value that `int` can contain and give a negative result. – Cameron Nov 26 '14 at 19:28
  • 1
    There is no for loop in this code. – MrEricSir Nov 26 '14 at 19:28
  • @Cameron so the problem in my code is that when the value of a changes it happens for a * a too right ? – piero borrelli Nov 27 '14 at 17:01
  • 1
    @piero: Yes. C++ is an imperative language; that means that the program is not a *description* of how things should be, but quite literally a *series of steps* to execute in order. Control flow enters at the start of main, executing all the statements until the end, in order, and always with the *current state* (i.e. `a` changes value each time through the loop, which affects the condition each time it's evaluated). – Cameron Nov 27 '14 at 18:10

2 Answers2

7

Using multiplication when trying to avoid multiplication seems broken. What about this:

int r = 0;

for (int n = 0; n < a; ++n) {
  r += a;
}
tadman
  • 208,517
  • 23
  • 234
  • 262
  • @pm100 That was left as an exercise for the reader. Yeah, that's it. (Thanks.) – tadman Nov 26 '14 at 19:32
  • @tadman thank you for the answer, I encountered some problems in writing this program because I just learned the repetition and then it takes me a while to understand it, is this normal? – piero borrelli Nov 27 '14 at 18:33
  • If you're new to programming it'll take a while to learn how the fundamental constructs like `if`, `for` and `while` are used and how they can be applied. C++ isn't as forgiving as other languages, so don't feel so bad about it. So long as you learned something, you're already better than before. – tadman Nov 27 '14 at 18:50
1

why the for loop doesn't stop when a is greater than its square ?

Because it never is. If you compare the graph of y=x^2 against the graph of y=x, you will see that the only time y=x is above, is when 0 < x < 1. That's never the case for integers1. Now, since we're talking about computers with limited storage here, there is a thing called overflow, which will cause a very large number to become a very small number. However, signed integer overflow is undefined behavior in C++. So once your loop gets to the point where overflow would happen, you cannot rely on the results.

1. Note that your loop is not set to stop just when a is greater than its square, but when it is greater than or equal to its square. So, your loop will actually stop if a is 0 or 1.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274