Hello all,
Is there a way to move numbers in a circular motion using pointers? For example, if I wanted the numbers 12345 to shift and be 23451? Is it valid? Thank you!
Hello all,
Is there a way to move numbers in a circular motion using pointers? For example, if I wanted the numbers 12345 to shift and be 23451? Is it valid? Thank you!
Shift how? Shift where? Move inside an array? Move to annother array? Are they stored in an int or float? Really not quite sure what you mean.
Demonographic rhinology is not the only possible outcome, but why take the chance
You could use a linked list, and make it circular by linking the tail node back to the head node.
In your case, each node would contain an integer between 0 and 9, and a pointer to the previous and next nodes. You could then start on any node, move in either direction through the list and concatenate the individual numbers into a single string until you got back to where you started, then convert the combined string to an integer for use.
Only if you enjoyed doing that sort of thing though!!
There is no such thing as a humble opinion
You can move an int variable with >> and << but that will not give you the desired effect since an int variable is a binary number but you want to shift the decimal representation of that number.
You could do it like this:
Code:int a=12345; int b,i; printf("%d",a); b=a%10; a/=10; for(i=1; i<=a; i*=10); a+=b*i; printf("%d",a);
So your trying to isolate the 'units' digit, remove it from the original number, then multiply it to the order of the original number and add it back on?
Nice idea, but I don't think your code is quite right.
This worked though...
Code:#include <stdio.h> int main() { int a=12345; int b=0, i=0; printf( "\n%d", a ); b = a%10; a = a/10; printf( "\na:%d;b:%d", a, b ); for( i=1 ; i <=a ; i=i*10 ); b=b*i; a+=b; printf( "\n\n%d\n\n", a ); return 0; }
There is no such thing as a humble opinion
Sorry DrZoidberg, I've been a dope. Didn't read your code properly...
There is no such thing as a humble opinion
Also your code is identical to mine except that you didn't forget the linefeeds.