int - array

Recommended Posts

How could I take an integer and make it into an array like:
int x = 123;
int y[0] = 1;
int y[1] = 2;
int y[2] = 3;
Thanks.

Share on other sites
Couple ways.

int foo[10]; //makes an array with 9 elements (0-9)

int *bar;
bar = new int[10];

or you could also do:
std::vector<int>array1(10); //makes a vector of 10 ints, numbered 0-9... refer to them like you would any regular array

A lot of this is covered in any basic C/C++ tutorial though so be sure you read up on them.

Share on other sites
There are many ways....

There's probably something simpler but here's my go.

int i = 123;int z=0;int iSplitted[10];for(int x=1000000000; x>0; x/=10 ) {  iSplitted[z++] = i % x;}

That should produce an array of ten ints left to right.

The array created from 123 should be
{ 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 } unless I made a stupid mistake.

Editing it so it only contain n elements pertaining to the original size of the int can be figured easily from this. I assumed a 32 bit int. Adjust the numbers accordingly.

Share on other sites
Are you fixing the number of digits? If not, then a more robust system is needed.

int x = 123;int y[3];y[0] = x/100;y[1] = (x-y[0]*100)/10;y[2] = x-y[0]*100-y[1]*10;

Share on other sites
What I'm planning on doing is making a hiscore with a scramble so that people can't easily go into the .txt and change whatever they want. So I need each digit a different part of the array so I can conver it. So it wouldn't be a set number.

Share on other sites
oh, then I'd use streams or sprintf and scramble the integers as strings. Then I'd load them as strings, unscramble them, and convert them back to ints.

Share on other sites
Yes that's what I'm doing but in order to scramble/descramble, I need all the digits to be in a array.

Share on other sites
Quote:
 Original post by xMcBaiNxThere are many ways....There's probably something simpler but here's my go. *** Source Snippet Removed ***That should produce an array of ten ints left to right.The array created from 123 should be{ 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 } unless I made a stupid mistake.Editing it so it only contain n elements pertaining to the original size of the int can be figured easily from this. I assumed a 32 bit int. Adjust the numbers accordingly.

Actual output:

{ 123, 123, 123, 123, 123, 123, 123, 23, 3, 0 }

Share on other sites
Yeah, I just tried what that guy suggested. I'm thinking I could do a bunch of nested if commands to see if score > 100, score > 1000 etc etc but I just can't think of how to get the remainder to work. Actually the nested if's is pointless...just getting the remainder to work is difficult.

Share on other sites
I just *loopized* my code in the style of McBane's:

int x = 123;int y[10];for (int i = 0, div = 1000000000, prev = x; i < 10; ++i, div /= 10) {	y[i] = prev/div;	prev -= y[i]*div;}

Output:

{ 0, 0, 0, 0, 0, 0, 0, 1, 2, 3}

Share on other sites
Okay, I got the scrambler working but how would I make a vector with the highest number first and lowest last etc. I'm thinking some nested for loops with an if statement but I can't think of how to do it. Thanks by the way for helping.

Share on other sites
int input = 123;std::vector<int> digits;do {  digits.push_back(input % 10);} while (input /= 10); // digits are ordered least-significant to most-significant.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628285
• Total Posts
2981836

• 10
• 10
• 10
• 11
• 17