Jump to content
  • Advertisement
Sign in to follow this  
mxma1

int - array

This topic is 5043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by xMcBaiNx
There 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 this post


Link to post
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 this post


Link to post
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 = prev/div;
prev -= y*div;
}




Output:

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!