Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 7th, 2008, 1:51 PM   #1
amstu
Newbie
 
Join Date: Sep 2008
Posts: 6
Rep Power: 0 amstu is on a distinguished road
splitting an integer into an array

Is there an easy way to split an integer into an array?

I am able to do it, but the way I have it set up takes up way too many lines of code, and is not too flexible for integers of different lengths.

If anyone knows a relatively simple way to do this I would really appreciate the help.
amstu is offline   Reply With Quote
Old Sep 7th, 2008, 3:17 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: splitting an integer into an array

What do you mean by split an integer into an array? Do you mean turn each digit into a character or number in an array?

C Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int i, x = 501234900;
  5. char num[100];
  6. sprintf(num, "%d", x);
  7.  
  8. for(i=0; num[i] != '\0'; i++) {
  9. printf("Digit %d = %d\n", i+1, num[i]-'0');
  10. }
  11.  
  12. return 0;
  13. }

Like that?
Sane is offline   Reply With Quote
Old Sep 7th, 2008, 3:21 PM   #3
amstu
Newbie
 
Join Date: Sep 2008
Posts: 6
Rep Power: 0 amstu is on a distinguished road
Re: splitting an integer into an array

Yes. I just need to turn each digit in the number into a number element in an array.
amstu is offline   Reply With Quote
Old Sep 7th, 2008, 3:26 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: splitting an integer into an array

Well there you go sir.
Sane is offline   Reply With Quote
Old Sep 7th, 2008, 3:29 PM   #5
amstu
Newbie
 
Join Date: Sep 2008
Posts: 6
Rep Power: 0 amstu is on a distinguished road
Re: splitting an integer into an array

I am trying to understand what you did. I get most of it but have 2 questions.

1. what is the sprintf command?

2. what does "num[1]!='\0'" state?
amstu is offline   Reply With Quote
Old Sep 7th, 2008, 3:33 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: splitting an integer into an array

sprintf is exactly like printf, except the output goes to a string instead of the standard output.

So after calling sprintf(num, "%d", x); the integer will be printed to the array "num".


num[i] != '\0' makes the loop go until the character is a "null-terminator" in the string (the character which marks the end of a string).

num[i] - '0' converts the character ('0' - '9') into an actual integer (0 - 9).
Sane is offline   Reply With Quote
Old Sep 7th, 2008, 3:59 PM   #7
amstu
Newbie
 
Join Date: Sep 2008
Posts: 6
Rep Power: 0 amstu is on a distinguished road
Re: splitting an integer into an array

I integrated that into my code and it works great. I have one last question, if you don't mind.

Everything works if the number is less than ten digits long. Is there anyway that I could make this work for number that have a length greater than 10 digits?

Thanks.
amstu is offline   Reply With Quote
Old Sep 7th, 2008, 4:37 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: splitting an integer into an array

An integer can only hold 32 bits, so its length is limited.

If the "integer" type is replaced with a "long long integer" you can get 64 bits of information and hold about 20 digits.

In many situations you can handle an integer with "infinite" digits (neglecting memory constraints) by simply using the array to start with. Where is the number coming from? Show us your code?
Sane is offline   Reply With Quote
Old Sep 7th, 2008, 5:25 PM   #9
amstu
Newbie
 
Join Date: Sep 2008
Posts: 6
Rep Power: 0 amstu is on a distinguished road
Re: splitting an integer into an array

The number is an input from the user.
The goal of my program is to take the input from the user and calculate the differences between each number in the array. (i.e. inputArray[4]={2,3,5,4} differenceArray[3]={1,2,-1})
Then it displays the results via "." and "@"

#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[])
{
	int x=0;
	printf("Number? ");
	scanf("%i", &x);
	char num[100];
	int c=0;
	sprintf(num, "%i", x);
	
	for(int i=0;num[i]!='\0';i++){
		//printf("Digit %d=%d\n", i+1, num[i]-'0');
		c++;
	}
	int numArray[c];
	for(int i=0;num[i]!='\0';i++)
	{
		numArray[i]=num[i]-'0';
	}
	int difNumber[c-1];
	int new;
	
	
	for(int i=0;i<c-1;i++)
	{
		new=numArray[i+1]-numArray[i];
		difNumber[i]=new;
	}
	for(int i=0;i<c-1;i++)
	{
		printf("%i ", difNumber[i]);
	}
	printf("\n");
	for(int i=0;i<9;i++)
		printf("%i ", i-9);	
	for(int i=0;i<10;i++)
		printf("%i  ", i);
	printf("\n");
	for(int j=0;j<c-1;j++)
	{
		for(int i=0;i<19;i++)
		{
			int k=i-9;
			if(difNumber[j]==k)
			{
				printf("@  ");
				
			}
			else
			{
				printf(".  ");
			}
		}
		printf("\n");
	}	
}
amstu is offline   Reply With Quote
Old Sep 7th, 2008, 6:30 PM   #10
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: splitting an integer into an array

This sounds like homework so I won't code it for you. But a very quick and short solution does exist.

I would start by taking the input from the user in the form of a string:

C Syntax (Toggle Plain Text)
  1. char buff[1024];
  2. fgets(buff, sizeof(buff), stdin);
  3. // Loop through digits of buff here

That way you don't even need to convert from integer to string. It will also give you "infinite" digits. Furthermore, if you do the looping intelligently, you will end up with a very short solution.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic array help quickster12 C++ 4 Nov 29th, 2007 11:52 PM
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
changing size of an array Eric the Red Java 3 Apr 3rd, 2006 8:19 PM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM
Converting 1-dimensional array to 2-dimensional array Tazz_Mission_13 Java 6 Apr 8th, 2005 11:58 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC