473,770 Members | 5,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

To store each digit from a number into successive array location

2 New Member
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
2 27367
Banfa
9,065 Recognized Expert Moderator Expert
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
hikmaz
2 New Member
Thanks a lot for your help... it works fine now.

Thanks.
Jan 31 '07 #3

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

Similar topics

12
2975
by: jose luis fernandez diaz | last post by:
Hi, My OS is: cronos:jdiaz:tmp>uname -a HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license I compile in 64-bits mode the program below:
3
5308
by: Tom Turner | last post by:
Here's the background on my situation. The question follows --- We have 600 units of mail going from our business to various Post Offices every morning. Every unit is accompanied by a paper Verification form which is signed and dated by a postal employee and returned to our office as proof of delivery. The Verification contains a 24 character barcode which holds a non-unique 5 digit postal zip code at pos 5-9, and a unique 10- digit...
4
1833
by: RubenDV | last post by:
I am trying to make a cipher with a 256-bit key but i have no idea how the store this key without using arrays of ints or something like that. I just need a type that is big enough to hold the entire key, because i am going to use multiplication operations and such, which i can impossibly use on arrays or so i think. Is there a way to treat an array as a single, huge number without really being that? Any help is appreciated!
11
1137
by: Crirus | last post by:
I have a map in a game. I need to make a list with all visible objects for a player So, any building and any unit offer a range of visibility over the map. Now VB question: How can I store the references of that visible objects in a collection, to make sure I added one object only once...? Moreover ..how to determine if a certain object belong to the visible
4
8996
by: Anon | last post by:
Hello All! I have written a simple app that auto downloads a file from a secure ftp, renames it, and moves it to a network location. Everything works great except the renaming part. I parse out the last write date to get the new filename, but the days and month are only single digits! This is a problem when you reach something like 01/11/2004 and 11/1/2004, I get dup filenames! ie.. 2004111.txt. any idea on how I can rename these files...
8
6562
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the second iteration it should return the number 5, and so on. But for some reason on the first iteration returns the expected results. Each subsequent iteration returns the number plus 1. In order words, when I run the program I am getting: 7, 6, 4, and...
33
13367
by: Prasad | last post by:
Hi, Can anyone please tell me how to store a 13 digit number in C language ? Also what is the format specifier to be used to access this variable ? Thanks in advance, Prasad P
3
10131
blackstormdragon
by: blackstormdragon | last post by:
Here were our instructions: "My mother always took a little red counter to the grocery store. The counter was used to keep tally of the amount of money she would have spent so far on that visit to the store if she bought everything in the basket. The counter had a four-digit display, increment buttons for each digit, and a reset button. An overflow indicator came up red if more money was entered than the $99.99 it would register. (This was a...
3
5999
by: hl2ob | last post by:
Alright I'm still new to javascript. I was getting it pretty well, and getting everything alright untill this point. We have to make a program that test a 5 digit number as a palindrome. I have no clue on how to do this. I was given advice that I'd need to use arrays, but for javascript I have no idea how to work with them. I read some stuff in a book and read some tutorials, but this is the best I can do: var numa var pal = new...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10004
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.