Go Down

### Topic: Changing a variable name in a for loop? (Read 691 times)previous topic - next topic

#### chrisnet

##### Aug 01, 2013, 01:00 am
byte varTimerZ1R[16]= {};
byte varTimerZ1Hour[16]= {};
byte varTimerZ1Min[16]= {};

// Is there a way to change the "1" in varTimerZ1R[T], varTimerZ1Hour[T], and varTimerZ1Min[T] in a for loop to shorten code? I have do do this 26times
// Because varZn is 1-26

if (varZn == 1){
varTimerZ1R[T] = varR;
varTimerZ1Hour[T] = H;
varTimerZ1Min[T] = M;
}
else if (varZn == 2){
varTimerZ2R[T] = varR;
varTimerZ2Hour[T] = H;
varTimerZ2Min[T] = M;
}
else if (varZn == 3){
varTimerZ3R[T] = varR;
varTimerZ3Hour[T] = H;
varTimerZ3Min[T] = M;
}
else if (varZn == 4){
varTimerZ4R[T] = varR;
varTimerZ4Hour[T] = H;
varTimerZ4Min[T] = M;
}

I know this will not work but something like this
for (int x=0; x<27; x++){
varTimerZ"x"R[T] = varR;
varTimerZ"x"Hour[T] = H;
varTimerZ"x"Min[T] = M;
}

#### chrisnet

#1
##### Aug 01, 2013, 01:20 am
for (int x=0; x<27; x++){
varTimerZ"x"R[T] = varR;
varTimerZ"x"Hour[T] = H;
varTimerZ"x"Min[T] = M;
}
Would not even work because it assigns the same value to all 26 so maybe something like this
int x = varZn;
varTimerZ"x"R[T] = varR;
varTimerZ"x"Hour[T] = H;
varTimerZ"x"Min[T] = M;

But this will not work! Any ideas?

#### mrburnette

#2
##### Aug 01, 2013, 01:23 am
Variable names cannot be changed.  This is why arrays have indexes... If the name was changeable at runtime, no need for an index.

#### craigcurtin

#3
##### Aug 01, 2013, 01:36 am
Just use a multi dimensional array - the first dimension is the index to your 26 other arrays

Craig

#### chrisnet

#4
##### Aug 01, 2013, 02:04 am
So maybe something like this

byte varTimerZR[512]= {};           // array 0 would hold the varZn and the next 16 would hold varR
byte varTimerZHour[512]= {};      // array 16 would be the next varZn "2" and the next 16 would hold H
byte varTimerZMin[512]= {};        // array 32 would hold the next varZn "3" and the next 16 would hold M

// Then I would call it like this
int newT =  (varZn * 16 ) + T;
varTimerZR[newT] = varR;

what do you think?

#### mrburnette

#5
##### Aug 01, 2013, 02:28 am
Quote
// Then I would call it like this
int newT =  (varZn * 16 ) + T;
varTimerZR[newT] = varR;

what do you think?

Back in the dark ages of computers, CPU time was very expensive.  We would do peer-reviews of code to determine if there were more efficient ways to use resources.  Rarely done today because code is written on workstations that cost the same if used or just idle.

Just compile and test.  You will NOT wear out your Arduino flash.  Make a change, compile, test.  I call this the Calculus of computer programming.  Enjoy the freedom to learnthrough successive approximation.

Don't ask what others think... You may not want to know.  Post specific questions.

Ray

#### craigcurtin

#6
##### Aug 01, 2013, 02:40 am

So maybe something like this

byte varTimerZR[512]= {};           // array 0 would hold the varZn and the next 16 would hold varR
byte varTimerZHour[512]= {};      // array 16 would be the next varZn "2" and the next 16 would hold H
byte varTimerZMin[512]= {};        // array 32 would hold the next varZn "3" and the next 16 would hold M

// Then I would call it like this
int newT =  (varZn * 16 ) + T;
varTimerZR[newT] = varR;

what do you think?

Not Much !!!

declare an array with 3 dimensions as such

byte varTimers[26][3][16]= {};

then reference them in your for loop by having multiple nested indexes to pull and store the values

Craig

#7
##### Aug 01, 2013, 10:22 am
Actually, chrisnet, you're not too far off there.  That's how multidimensional arrays are stored in memory.  You have pretty much figured out how to do multidimensional arrays of structures using multiple single dimensional arrays.

But C++, and even C, have structures and multidimensional arrays to make it easier.  You don't need to use structures, though.  You can use 3 arrays.  Here's the syntax:

Code: [Select]
`byte varTimerZR[27][16];byte varTimerZHour[27][16];byte varTimerZMin[27][16];          for (int x=0; x<27; x++){            varTimerZR[x][T] = varR;            varTimerZHour[x][T] = H;            varTimerZMin[x][T] = M;          }`

I hope you're not using an UNO, though.  You'll run out of memory.

Go Up

Please enter a valid email to subscribe