Pages: [1]   Go Down
Author Topic: Creating an array  (Read 640 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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]);
    }
}
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1179
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink

Please explain what it is you are doing and what you hope to achieve with it.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1179
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11153
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: