Variables Type help
I've wrote a function that takes two numbers and "Connects" them together. Example:
12345, 98766 >>>>> 1234598766.
Here it is:
Now, it works well, except when I try to do it for TWO long numbers....
unsigned long long concatenate(unsigned long long x, unsigned long long y) // Checked, works ok!!!
int counter=0, test, tens=1;
x= (x* tens ) + y;
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!
This function lives here:
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:
square= sum_squares(array[i], array[i+3], array[i+6]);
prime = largest_prime_factor(square);
full[i]= concatenate(square, prime);
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...
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.
Yeah I got that but what type is longer than unsigned long long?
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...
Originally Posted by Elkvis
Maybe I'll try it in an array or something the like...
you can try converting to strings or use double or a library for handling arbitrary precision numbers.
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?
can you give a code example to explain what you mean?
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.