By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,738 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,738 IT Pros & Developers. It's quick & easy.

To store each digit from a number into successive array location

P: 2
I am trying to get the rightmost digits (%10) of a number (taken from the user) and store it into successive array locations and get rid of the rightmost digit (\10) to store the next and so on and so forth.

For example, if the number entered by the user is 4321
then, int array[0] = 1
int array[1] = 2
int array[2] = 3
int array[3] = 4
int array[4] = '\0' (End of array - when number == 0)

This is the piece of code i have written so far using a function called 'adddigits(int num)'. Once each digit is being stored into the array, the digit is being added to a variable to calculate the total sum of digits.

Please help me locate the problem and find a solution to store the individual digits into seperate successive array locations.

Expand|Select|Wrap|Line Numbers
  1. void adddigits(int num)
  2. {
  3.     int i, length, a = 0, digsum = 0, digit[] = {'\0'};
  4.  
  5.     while(num!=0)
  6.     {
  7.         digit[a] = num % 10;
  8.         printf("a value is: %d, ", a);
  9.         printf("digit[a] value is: %d, ", digit[a]);
  10.         digsum += digit[a];
  11.         printf("sum is: %d, ", digsum);
  12.                                 num /= 10;
  13.         a = a+1;
  14.         printf("length value is: %d, ", a);
  15.         printf("\n");
  16.     }
  17.  
  18.     printf("\n\ndigit[0] = %d", digit[0]);
  19.     printf("\ndigit[1] = %d", digit[1]);
  20.     printf("\ndigit[2] = %d", digit[2]);
  21.     printf("\ndigit[3] = %d\n\n", digit[3]);
  22. }
  23.  

The display (result) screen shows me the following and I absolutely don't have a clue as to why value of a changes to 4 after 2.



**** Display Screen (Output) ****

Enter a number whose sum of digits you require: 4321
a value is: 0, digit[a] value is: 1, sum is: 1, length value is: 1,
a value is: 1, digit[a] value is: 2, sum is: 3, length value is: 2,
a value is: 2, digit[a] value is: 3, sum is: 6, length value is: 3,
a value is: 4, digit[a] value is: -1, sum is: 5, length value is: 5,

digit[0] = 1
digit[1] = 2
digit[2] = 5
digit[3] = 5

Please assist me with this. Thanks.
Jan 25 '07 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,965
Your problem is the way you have declared digit

int digit[] = {'\0'};

This results in an array of integers with only 1 entry in it, but you then write to the first 5 entries.

digit[0] valid
digit[1] out of bounds access
digit[2] out of bounds access
digit[3] out of bounds access
digit[4] out of bounds access

The out of bounds accesses overwrite the values of the other stack variables including a.


You need to declare digit with enough entries to hold all the digits in num (+1). This is not as hard as it sounds, num is a 32 bit int with a maximum value of 2147483647 so you need to be able to hold 10 + 1 = 11 entries.

Therefore declare digit as

int digit[11] = {'\0'};

and suddenly your code starts working :D
Jan 25 '07 #2

P: 2
Thanks a lot for your help... it works fine now.

Thanks.
Jan 31 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.