Thread: Variables Type help

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    23

    Variables Type help

    Hello guys!

    I've wrote a function that takes two numbers and "Connects" them together. Example:

    12345, 98766 >>>>> 1234598766.

    Here it is:

    Code:
    unsigned long long concatenate(unsigned long long x, unsigned long long y) // Checked, works ok!!!
    {
        int counter=0, test, tens=1;
    
    
        test=y;
    
    
        while (test>0)
        {
            counter++;
            test/=10;
        }
    
    
        while (counter>0)
        {
            tens*=10;
            counter--;
        }
    
    
        x= (x* tens ) + y;
    
    
        return x;
    }
    Now, it works well, except when I try to do it for TWO long numbers....

    Example:

    if I try to connect:

    6611 and 93311267 I get 406333314979.

    It's weird because the function itself works, and this new numbers does have 12 digits as it should have, but they're different....

    I'd like to know if it's something with the variables perhaps?


    Thanks a bunch!

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    PS

    This function lives here:
    Code:
    while (i<3)
        {
            square= sum_squares(array[i], array[i+3], array[i+6]);
            prime = largest_prime_factor(square);
            full[i]= concatenate(square, prime);
            printf("%ul\n", full[i]);
            i++;
        }
    I won't say what each function does, but ths printf prints each part of the array, which is unsigned long long... and something weird prints out:

    Variables Type help-untitled-jpg

    I don't know what those 1-like symbols are...

    123456789 <<< this is the input.... Then it calculates something, and Outputs the "Square". Then it calculates the highest Prime number which devides the Square, then I use the function to connect them together.

    Then I just put in printf to debug it and that's what I saw...
    Last edited by Gal Abir; 12-21-2012 at 09:11 AM.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    my guess is that int isn't big enough to handle the result. try using long (or even long long if your compiler supports C++11) to store your result.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Yeah I got that but what type is longer than unsigned long long?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    unsigned long long is the largest integral type offered by the standard. the trouble you're having is that your "tens" variable is of type int, and int on most platforms these days is just too small to hold more than 10 decimal digits.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Quote Originally Posted by Elkvis View Post
    unsigned long long is the largest integral type offered by the standard. the trouble you're having is that your "tens" variable is of type int, and int on most platforms these days is just too small to hold more than 10 decimal digits.
    Not that... I changed my tens to unsigned long long and it still didn't work... I need to find a different way of saving a 12 digits long number...

    Maybe I'll try it in an array or something the like...

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you can try converting to strings or use double or a library for handling arbitrary precision numbers.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    I see... well I think I can solve it but I have a question (I really suck at pointers).

    If I have a function that gets unsigned long long, can I feed it a point to an array?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    can you give a code example to explain what you mean?

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You declared test as an int, so when you do test = y; you are truncating the value to the size of an int. You would need to make test an unsigned long long as well. tens would also need to be unsigned long long, but counter can stay as an int.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do Nullable<T> Type variables work?
    By y99q in forum C# Programming
    Replies: 7
    Last Post: 12-31-2011, 03:59 PM
  2. <type> far variables
    By ThLstN in forum C Programming
    Replies: 4
    Last Post: 03-27-2011, 01:28 PM
  3. How to find type of variables?
    By Niels_M in forum C Programming
    Replies: 1
    Last Post: 08-06-2010, 06:41 AM
  4. user input type variables
    By pastitprogram in forum C++ Programming
    Replies: 1
    Last Post: 09-05-2008, 07:21 AM
  5. Replies: 3
    Last Post: 04-05-2008, 07:44 AM