# Thread: printing number from array question..

1. ## printing number from array question..

i got this 50 cell integer array
i use only a few cells from 0(which represents the highest coefficient of 10^(col-1) )
to cell col-1 (which coefficient is 10^0 =1)
i got an input in this array
i could have the number 123 in cell col-1
so i need to add in cell col-1 the number 3
int cell col-2 (tens) i need to add 2
and in cell col-3 (the hundreds) i need to add 1

the problem is that in some cell there could be such a big number
that i would need to change the variable col.
and i couldnt do this simple process that i described in the beginning

i tried to build this :
Code:
```
kounter=0;
for (index=0;index<cols;index++){
sumc[index]=0;
}
for(index=cols-1;index>=cols;index--)
{
copy=sumc[index+kounter];
while (copy!=0){

sumc[index+kounter]=sumc[index+kounter]+copy%10;
copy=copy/10;
kounter--;
}
}

for (index = 0; index < rows; index++)
{
printf("%d",sumc[kndex]);

}```

2. so if i got this array

|0|0|0|8|19|10|

the array should look
|0|0|1|0|0|0|
it needs to print
1000

i cant calculate the sum because it could go over the integer bounders
it we are told to print the number as it should be and use integers only

??

3. Well you could store the number going from the ones place to the biggest place that you want to support, that would make calculations easier.
Code:
```int input = 123;
int odometer[4] = {0}; /** largest number supported: 9999 **/
int i;
for( i = 0; i < 4 && input > 0; i++) {
odometer[i] = input % 10;
input /= 10;
}```
And you would just reverse the array when you want to print.

4. when i entered this array
|0|0|0|8|19|10|

the array should look
|0|0|1|0|0|0|

i tried to fit your code
its not working
Code:
```               for (kndex=0;kndex<cols;kndex++){
input=sumc[kndex];
for( index = 0; index < cols && input > 0; index++) {
sumc[index] = input % 10;
input /= 10;
}
}```

5. You have been told time and time again, and I am pretty sure that I have given you at least one example: indent your code properly!

Originally Posted by transgalactic2
i tried to fit your code
its not working
How does it not work? The next time you merely say "its not working", I'm going to say "too bad" and laugh at your incompetence

If you want to avoid getting laughed at, read the article I linked to in my signature concerning how to ask smart questions.

Often, it would also help if you posted the smallest and simplest (compilable) program that demonstrates the problem. (Sounds familiar?)

6. this code is intended
and basicly what the code needs to do is
when i entered this array
|0|0|0|8|19|10|

the array should look
|0|0|1|0|0|0|

Code:
```kounter=0;
for (index=0;index<cols;index++){
sumc[index]=0;
}
for(index=cols-1;index>=cols;index--)
{
copy=sumc[index+kounter];
while (copy!=0){

sumc[index+kounter]=sumc[index+kounter]+copy%10;
copy=copy/10;
kounter--;
}
}

for (index = 0; index < rows; index++)
{
printf("%d",sumc[kndex]);

}```

7. Originally Posted by transgalactic2
this code is intended
Yes, but not properly (e.g., consistently). This code is indented properly:
Code:
```kounter = 0;
for (index = 0; index < cols; index++)
{
sumc[index] = 0;
}

for (index = cols - 1; index >= cols; index--)
{
copy = sumc[index + kounter];

while (copy != 0)
{
sumc[index + kounter] = sumc[index + kounter] + copy % 10;
copy = copy / 10;
kounter--;
}
}

for (index = 0; index < rows; index++)
{
printf("%d", sumc[kndex]);
}```
Originally Posted by transgalactic2
and basicly what the code needs to do is
Yes, you have explained that. Basically, you are trying to do some kind of bignum-style addition where each element of the array is a digit. What you have not explained is how does your current code not work.

It looks like you are missing a variable to carry over the addition.

Oh, and whiteflags suggested a little-endian format. Are you going to do it that way, or stick to a big-endian format?

8. i am going to try and fix my code
it work b taking a member
taking its remainder by 10
and adding this number to the first
then deviding it by 10
because its an int
it will keep only the whole part
and adding it remainder by 10 on the next cell
etc..

9. Originally Posted by transgalactic2
it work b taking a member
taking its remainder by 10
and adding this number to the first
then deviding it by 10
That is not quite right. I would expect the algorithm to be something like this:
Code:
```carry = 0
loop from the least significant "digit" to the most significant "digit"
number = digit + carry
digit = number % 10
carry = number / 10 (integer division)```

10. in what place i put values in the array?

11. Originally Posted by transgalactic2
in what place i put values in the array?
What do you mean? Do you understand my pseudo-code?

12. i cant understand this scheme
its all about changing the array
and you didnt mention it in your "code"

13. Originally Posted by transgalactic2
its all about changing the array
and you didnt mention it in your "code"
Can you see that each element of the array is a "digit"?

14. ok i will try to build a code uppon this scheme..

15. is that ok?
Code:
```carry = 0
//loop from the least significant "digit" to the most significant "digit"
for(index=cols-1;index>=0;index++){
number = suma[index] + carry;
suma[index] = number % 10
carry = number / 10
}```