user4017662
user4017662

Reputation:

Making a square() function without x*x in C++

I am self-studying C++ and the book "Programming-Principles and Practices Using C++" by Bjarne Stroustrup. One of the "Try This" asks this:

Implement square() without using the multiplication operator; that is, do the x*x by repeated addition (start a variable result at 0 and add x to it x times). Then run some version of “the first program” using that square().

Basically, I need to make a square(int x) function that will return the square of it without using the multiplication operator. I so far have this:

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

return i;
}

But I was wondering if there was a better way to do this. The above function works, but I am highly sure it is not the best way to do it. Any help?

Upvotes: 10

Views: 12897

Answers (7)

Josef.L
Josef.L

Reputation: 1

  //Josef.L
  //Without using multiplication operators.

int square (int a){
     int b = 0; int c =0;
    //I don't need to input value for a, because as a function it already did it for me.
    /*while(b != a){
            b ++;
        c = c + a;}*/
    for(int b = 0; b != a; b++){  //reduce the workload.
        c = c +a;
        //Interesting, for every time b is not equal to a, it will add one to its value:
        //In the same time, when it add one new c = old c + input value will repeat again.
        //Hence when be is equal to a, c which intially is 0 already add to a for a time.
        //Therefore, it is same thing as saying a * a.
    }
    return c;
}

int main(void){
    int a;
    cin >>a;
    cout <<"Square of: "<<a<< " is "<<square(a)<<endl;
return 0;
}

//intricate.

Upvotes: 0

icer
icer

Reputation: 11

int square(int x) {
    int result = { 0 };
    int *ptr = &result;
    for (int i = 0; i < x; i++) {
        *ptr = *ptr + x;
    }
    return *ptr;
}

I am reading that book atm. Here is my solution.

Upvotes: 1

yotke
yotke

Reputation: 1208

 int square(int n) 
 { 
        // handle negative input 
        if (n<0) n = -n; 

         // Initialize result 
         int res = n; 

          // Add n to res n-1 times 
          for (int i=1; i<n; i++) 
          res += n; 

      return res; 
 } 

Upvotes: 0

John Smith
John Smith

Reputation: 1

int square(int x)
{
    int result = 0;
    for (int counter = 0; counter < x; ++counter) result += x;
    return result;
}

Upvotes: 0

JedaiCoder
JedaiCoder

Reputation: 666

You can include <math.h> or <cmath> and use its sqrt() function:

#include <iostream>
#include <math.h>
int square(int);

int main()
{
   int no;
   std::cin >> no;

   std::cout << square(no);
   return 0;
}

int square(int no)
{
   return pow(no, 2);
}

Upvotes: -1

Christopher Oicles
Christopher Oicles

Reputation: 3107

Mats Petersson stole the idea out of my head even before I thought to think it.

#include <iostream>

template <typename T>
T square(T x) {
    if(x < 0) x = T(0)-x;
    T sum{0}, s{x};
    while(s) {
        if(s & 1) sum += x;
        x <<= 1;
        s >>= 1;
    }
    return sum;
}

int main() {
    auto sq = square(80);
    std::cout << sq << "\n";
}

Upvotes: 5

gehan
gehan

Reputation: 19

In term of the running time complexity,your implementation is clear and simply enough,its running time is T(n)=Θ(n) for input n elements.Of course you also can use Divide-and-Conquer method,assuming split n elements to n/2:n/2,and finally recursive compute it then sum up two parts,that running time will be like T(n)=2T(n/2)+Θ(n)=Θ(nlgn),we can find its running time complexity become worse than your implementation.

Upvotes: -1

Related Questions