![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2008
Posts: 6
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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)
Like that? |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2008
Posts: 6
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
Re: splitting an integer into an array
Well there you go sir.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Sep 2008
Posts: 6
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
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). |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Sep 2008
Posts: 6
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
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? |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Sep 2008
Posts: 6
Rep Power: 0
![]() |
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");
}
} |
|
|
|
|
|
#10 |
|
Programming Guru
![]() |
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)
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |