Thread: Th power function

  1. #1
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48

    Th power function

    I want to use the power function to make the digits 1234567 to the sum of squared digits. Eg, 1^2+2^2+3^2+4^2+5^2+6^2+7^2=140

    I have done:
    #include <cmath>

    int b;

    b = pow((id/1000000),2) + pow(((id%1000000)/100000),2) + pow(((id%100000)/10000),2) + pow(((id%10000)/1000),2) + pow(((id%1000)/100),2) + pow(((id%100)/10),2) + pow((id%10),2);


    I get the answer of 2 where it suppose to be 140. What went wrong? Can someone please help me?

  2. #2
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Your whole algorithm needs rework.

    May I suggest using a loop and a running total instead?? here's some C++ psuedo to get you going?

    Code:
    int number = 1234567;
    int total = 0;
    
    while(number > 0)
    {
       int remainder = number % 10;
       total += pow(remainder, 2);
       number = number / 10;
    }
    
    cout << total;
    This code hasn't been tested or compiled, but should give you the idea of the structure you're looking for.

    Hope that helps.

  3. #3
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    I followed your structure by I get an error say that:

    total += pow(remainder, 2); has an "call of overloaded `pow(int&, int)' is ambiguous"

    What does this mean?

  4. #4
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    It means that pow() has more than one overload, and that it doesn't know which of the overloads to call.

    IMHO, using pow() in this case is pointless anyway. Why not just times the variable together??

    Code:
    total += remainder * remainder;

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by wiz23
    I followed your structure by I get an error say that:

    total += pow(remainder, 2); has an "call of overloaded `pow(int&, int)' is ambiguous"

    What does this mean?
    Try casting to double:

    pow (static_cast<double>remainder, 2.0)
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  6. #6
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    Let me explain what I am doing, I needed to enter 7 digits as 1234567 and get the sum of squared digits so the answer should be 140. I have declare the 7 typed digits as int id; I also included
    Code:
    #include <cmath> I have tried this code int number = id; int total = 0; while(number > 0) { int remainder = number % 10; total += remainder * remainder; } cout << total;
    but when typed in 1234567, there is nothing to appear. I am stuck.

  7. #7
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    you missed a line in what i gave you:

    Code:
    number = number / 10;
    You'll get an infinite loop otherwise.

    Cheers

    PS. Many thanks to the person who "disapproved" of my post stating that "it wasn't psuedocode" That's hilarious Please would be you be so kind as to point me at the psuedocode specification/standard?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM