Multiplication Through Addition Function

I am trying to create a function called square() that will multiply a given integer through addition. This is an exercise in my book. Help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "std_lib_facilities.h"

int square(int x)
{
    // Multiplication through addition
}

int main()
{
    cout << square() << endl;

    return 0;
}

This code is bare bones, but nothing I've tried works properly.
Last edited on
Did you try using a for loop?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "std_lib_facilities.h"

using namespace std;

int square(int x)
{
    int output = 0;
    for (int i = 0; i < x; i++)
    {
        output += x;
    }
     return output;
}

int main()
{
    cout << "Enter a number to be squared.\n";
    int input;
    cin >> input;
    cout << square(input) << endl;

    return 0;
}


Are you using Bjarne Stroustrup's book? That is the library he gives on his website and from the book so I was just wondering.
Last edited on
Yes, I'm using Programming, Principles and Practice Using C++ Second Edition by Bjarne Stroustrup.

Thank you, that code works very well.

I am only now starting to learn functions. Being unfamiliar with them, I would like to know if it is possible to make it so no input is required from the user to square a number. The number would be inserted inside the parenthesis, before the program is run, like square(2), for example. Is this possible, without using the multiplication operator within the function?
Last edited on
You could give input a value in the code without having to ask for user input.

1
2
    int input = 2;
    cin >> input;



If you wanted more of a range of values, you could do something like this and just give max whatever value you want.

1
2
3
const int max = 12;
for (int i = 1; i <= max; ++i) 
    cout << i << " squared is " << square(i) << endl;
Last edited on
You could set a variable equal to two and put it inside the parentheses .
I must be dense, but I can't figure this out. Are functions meant to be this difficult to grasp?

The first thing I don't understand is why (int x) is seemingly not given a value, yet is still used in the conditional branch of the for loop within the function.

The next thing I don't understand is why int output within the function is returned, when in main(), the variable int input is the one which gets squared. Surely output is the value which should be spat back out to the user?

The final thing I would like to know is why there needs to be any extra input to square a variable within main() at all? Surely the whole point of using a function for something like this is to make it so you only need to use one word, in this case being "square()", with whatever given value you decide to use within the parenthesis.

As I said, I'm new to functions, so I'm probably missing something glaringly obvious. Still, it's bloody frustrating.
int square(int x)

This first line of the function gives information about what information the function expects to receive and what information it expects to return to the function that called it.

int square(int x)
This is the data type of the info that the function will return to the place it was called. The function returns an integer which is the value calculated and stored in the local function variable output. So when square(2) is output with the cout statement in the main function it outputs the value the function returned.

int square(int x)
This info in the parenthesis is the information the function expects to receive and how that information can be referred to locally within that function. So if you call the function with square(2), this function parameter x takes on the value 2.

There's a pow function in the <cmath> header, but to square a value x, it might be easier just to multiply x*x and not call a function at all.
http://www.cplusplus.com/reference/cmath/pow/
Last edited on
Are functions meant to be this difficult to grasp?

Functions are generally easy to grasp. What can be difficult to grasp for a beginner is the concept of arguments.

The first thing I don't understand is why (int x) is seemingly not given a value

x is not given a value at the time you write the function. x is an argument that will be supplied when your function is called (line 20). Let's say the user enters 10 (input). This is the value passed to square() as x. At the time we write square(), we don't know the value the user will enter, but we use x to represent that value within square().

The next thing I don't understand is why int output within the function is returned

We need a local variable to accumulate our answer. output is that local variable within square().

Surely output is the value which should be spat back out to the user?

Yes, but indirectly. square() returns the accumulated value. It doesn't directly output that value to the user. At line 20, square() returns a value (what was in the local variable output). This returned value is then written to cout. If we change main() as follows, it might be a little clearer:
1
2
3
4
5
6
7
8
int main()
{  cout << "Enter a number to be squared.\n";
    int input, result;
    cin >> input;
    result = square(input);
    cout << result << endl;
    return 0;
}

We really don't need the result variable since cout knows how to display the return value from square() (an int).

The final thing I would like to know is why there needs to be any extra input to square a variable within main() at all?

I don't understand your question. square() needs an argument (the number you want to square). What "extra input" are you referring to?

Keep in mind that square() doesn't know where it will be called from and does not have access to main's variables. If we want to pass a value to square(), it has to be through the argument list.


Last edited on

We need a local variable to accumulate our answer. output is that local variable within square().

So, the variable that is returned is what the call function becomes? As in, output, which would be equal to 4 if the call function was square(2), is what square(2) becomes once returned?


I don't understand your question. square() needs an argument (the number you want to square). What "extra input" are you referring to?

What I meant was whether or not extra lines of code, other than the call function, would be required to return a value. Having played around with the code a little bit, I now realise that you do not need an input value by the user to return a value to the call function at run time.

This is the code I have now. This should give you a clearer view of what I was trying to achieve.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "std_lib_facilities.h"

int square(int x)
{
    int output = 0;
    for (int i = 0; i < x; i++)
    {
        output = output + x;
    }
    return output;
}

int main()
{
    cout << square(/*0, 1, 2, 3...*/) << endl;

    return 0;
}


That for loop is very clever. At first I didn't understand how it was squaring the call function value, but I think I have it now.


Summary

int x, which is the equivalent to the value the call function square() receives, is implemented into the conditional branch of the for loop, thereby becoming the determining factor as to how many times the loop iterates. If the value is two, the loop iterates twice, if it's four, four times, etc.

The actual variable, in this case output, within the loop statement receives an accumulative value depending on how many iterations of the loop there are. This is because the value of output increases with each iteration, due to x being added to it, and continues to increase until i is no longer less than x.

Once i is no longer less than x, the loop ends and output has its final value, which is then returned to the call function square(). This is the value we see when we output the call function.

Please let me know if my summation of the function process is in any way inaccurate. I'm really trying to nail this one.
Your summary is exactly right.
Alright, I feel I have a better understanding of basic functions now. Thanks guys.
Last edited on
No problem
Topic archived. No new replies allowed.