int - array

Started by
10 comments, last by Zahlman 19 years, 8 months ago
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.
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.
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.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
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;
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
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.
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.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Yes that's what I'm doing but in order to scramble/descramble, I need all the digits to be in a array.
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 }
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
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.
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}
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement