Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenate Variables?

Status
Not open for further replies.

Daieseo

Technical User
Mar 19, 2006
3
GB
Hi, I have a loop that used a variable i. then I have several variables x1, x2, x3 etc

how can I have it so the loop concatenates the value of i to x ? - this is without using an array for the x variable btw :)

thanks!
Dan
 
That depends on what you mean by concatenating them. If you mean adding them, that's easy. If you mean putting them in an array of ints, well actually that's easy too (assuming you know how many x variables you have), but you don't need a loop for either one of those.

I have a feeling like there might be a better way of doing what you want to do though, so maybe if you explained why you need to concatenate those variables, and where the variables are coming from, we can find a better solution for you.
 
One more guess - may be there was meant concatenation of decimal digits of initial numbers, i.e., concatenated 123 and 456 should result to 123456. If so, then itoa() both numbers, strcat() them and atoi() again. Or more sophisticated and slow formula like this:
c = a * 10 ^ (lg(b) + 1) + b;

 
How did you come up with that formula?
I'm assuming lg means log.

When I try it with a=123, b=456, I get this:

log( 456 ) + 1 = 3.6589648426644349844725780631855

10^3.6589648426644349844725780631855 = 4560

123 * 4560 = 560880

560880 + 456 = 561336

561336 != 123456
 
The formula obviously seem to bee not in C syntax. lg() I meant is logarithm in base 10 (additionally fixed to int). Sorry about ^, I meant pow():
Code:
#include <stdio.h>
#include <math.h>

int lg(const int ii)
{
   return(log(ii)/log(10));   
}


int concatenate(const int a, const int b)
{
   return(a * pow(10, (lg(b) + 1)) + b);
}

int main(void)
{
   printf("%d\n", concatenate(123, 456));
   return(0);
}
 
(1) Why no array for x1, x2, x3 etc.? Limitations like that sound a bit homeworky to me.

(2) Assuming the problem was to concatenate (as strings) integer numbers in decimal (into a giant string): I'm a bit wary of turning integers into floating-point to do this. Although it looks quite elegant it's probably slower, and certainly less versatile.

(2a) Calculating logs is quite a work-intensive thing to do. If you're going to have to convert the number to a string eventually anyway, you might just as well convert it straight away and see how long the string is (since you don't really want a log, just the number of digits in the value).

(2b) If you go the log way, you can only handle concatenation of a set of numbers whose total length is no greater than the significant digits of your floating point type, which is probably 15 or 30 at most, depending what you use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top