Sign in to follow this  
mxma1

int - array

Recommended Posts

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
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
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[i] = prev/div;
prev -= y[i]*div;
}




Output:

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

Share this post


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


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this