Go Down

Topic: Creating an array (Read 770 times) previous topic - next topic

edwardhensel

I have to work with a lot of numbers and I need an array that has all of the numbers from 0 to 1500.  I want to have some kind of loop that can be defined int the "void setup" function, that will set up the desired array.  I was thinking that I could make a "for" loop just keep adding all of the numbers until it hits 1500.  Obviously that did not work. lol   Bellow you will find one of my attempts to try this.

Any help would be great.
Thanks


Code: [Select]
const int MaxChars = 1500 ;
int temp[MaxChars+1] ;

int array[] = {1, 2 ,3, 4, 5} ;


void setup() {
Serial.begin(9600);


  for ( int j = 0 ; j < MaxChars - 1 ; j++ )
   {
    int num ;
    for ( int index = 0 ; index < MaxChars - 1 ; index++ )
     {
     temp[index] = num;
     }
   }
}



void loop() {
Serial.println(temp[1]);

for  ( int i = 0 ; i < MaxChars ; i++) {while (i < MaxChars ) {Serial.println(temp[i++]);}}
delay(5000);
}

lloyddean

I don't understand why you want to do this and think your description of what you think you did makes any sense either.

Code: [Select]

const size_t    ARRAY_LENGTH    = 1500;

int array[ARRAY_LENGTH];

void setup()
{
   Serial.begin(9600);

   for ( size_t index = 0; index < ARRAY_LENGTH; ++index )
   {
       array[index]    = index;
   }
}

void loop()
{
   for ( size_t index = 0 ; index < ARRAY_LENGTH; ++index )
   {
       Serial.println(array[index]);
   }
}


DuaneB

HI,

If your using an UNO it will not have enough memory to store this array. As the previous poster says, let us know a bit more about what your doing, there are always alternative approaches.

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

marco_c

I agree it makes no sense - if the array element indexed by n contains the number n, then why bother storing it there? Why not use the index n by itself?
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

majenko

The only situation I can see this making sense is the "deck of cards" scenario.  You have X number of unique numbers, and you want to return them in a random sequence, but each number being unique - like shuffling a deck of cards.

Why you'd want to do that with 1500 cards I have no idea ;)

Please explain what it is you are doing and what you hope to achieve with it.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

DuaneB

Or measuring a distribution of samples in a range ie. whatever the sample value, increment the counter in the array cell indexed by that sample value.

Duane B
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

marco_c

I get the card shuffling stuff, but if you are looking to count samples, then you initialise to zero, not the value of the index.

Anyway, all conjecture until edwardhensel tells us why he wants to do this :)
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

DuaneB

True, didn't read the code enough to see that, mainly on the basis that 1500 integers would use more memory than the UNO has - unless it's going in progmem ?

Duane B
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

PeterH

In isolation it seems like a strange thing to want to do, but if you want an array consisting of 0, 1, 2, ... and so on up to 1499 then you can do that very easily like this:

Code: [Select]

const int MaxChars = 1500 ;
int temp[MaxChars] ;
for (int i = 0; i < MaxChars; i++ )
{
    temp[i] = i;
}

[/quote]

If that isn't what you're trying to achieve then you'll have to do a better job of explaining what you want to achieve.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up