### Array elements to Integer

Hi guys, i am looking for a function to fuse the elements of an array into a single "int" variable, to make it simple i am looking for the opposite of this function, couldn`t really figure it out
 ``12345`` `````` for(i = 9 ; i >= 0 ; i--) { array[i] = N%10; N = N / 10; }``````
The opposite of division is multiplication.
I could not figure out what to do with the modulus operator ...
In this case the reverse operation would be addition.
 ``12345`` ``````for(i = 0 ; i<=9 ; i++) { a *= 10; tab[i] = a+10; }``````

always a=0 ...
0 * 10 is 0? How very shocking.
look pal, if you don`t wanna help, don`t do it, ok ?!

and i wonder where did you got that 0*10 from ?
If you don't want to use your brain, then why on earth are you trying to learn programming?
When you have the digits 1, 2 and 3, how do you get to 123? This is first grade maths.

 and i wonder where did you got that 0*10 from ?

What value does a have? What happens when you multiply it by 10? Or even better, if a is 0 after you multiplied the previous value by 10, what was the previous value?
closed account (D80DSL3A)
I say stop trying to figure out how to "invert" that function and just write the calculation. If int `digits[5];` holds the digits of a 5 digit number then the N you are looking for is:
`int N = digits[0] + 10*digits[1] + 100*digits[2] + ...`
Just put that in a loop.

@Athar: Hope you don't mind me stepping in here.
Question for you. The meaning of big-endian vs. little-endian in a number representation.
I have chosen big-endian above because I am storing the most significant digit last, correct?
 I have chosen big-endian above because I am storing the most significant digit last, correct?

That's little-endian, actually. How you store them is up to you, but with OP's code it would be big-endian.
closed account (D80DSL3A)
Thanks, so I was wrong.

The endianness is named for what is in element 0 then, not the last element?
I store the least significant digit (the little one) in digits[0], so it is little-endian?
I see, by refering to OP's OP (original post), that he is indeed assigning the ones digit to array[9] on the 1st iteration, so it would be big-endian.
I was confused because I thought the "end" in "endian" referred the "end" of the array, not the beginning.

The term should be beginianness then, I say. LOL
I am sorry Arthar, my bad ...

 always a=0 ...

i wanted to point at the fact that "&a" is the address in which the 12345 number has to be stored, but no matter how hard i tried it kept outputing "0"
Last edited on
And about using my brain, yes, i used my brain doing this, so far it worked ok, but i can only insert 5 digits, no more, no less

 ``123456789101112131415161718192021222324252627`` ``````#include #include int i; int tab[5]; int tab1[5]={10000, 1000, 100, 10, 1}; int tab2[5]; void main() { printf("\n-----------------------------------------------------"); printf("\nInsert 5 digits!!!"); printf("\n-----------------------------------------------------"); printf("\n"); for(i=0 ; i<5 ; i++) { printf("Digit [%d] = ", i+1); scanf("%d", &tab[i]); } printf("\n-----------------------------------------------------\n"); for(i=0 ; i<5 ; i++) tab2[i]=tab1[i]*tab[i]; int a=tab2[0]+tab2[1]+tab2[2]+tab2[3]+tab2[4]; printf("A = %d", a); printf("\n-----------------------------------------------------"); getch(); }``````
Last edited on
Topic archived. No new replies allowed.