Arduino Forum

Forum 2005-2010 (read only) => Software => Syntax & Programs => Topic started by: jazzar on Oct 05, 2009, 11:12 am

Title: Assigning Arrays wont work (solved)
Post by: jazzar on Oct 05, 2009, 11:12 am
Hi!

Got a problem, and I don't know the cause >.<

I want to assign all numbers from one equally sized array to another.
So far so easy, here the code I want to use that won't work, I don't get any output from the Serial port... not even the first gibberish line:

Code: [Select]


int Qarray[36] = {
   0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,
};

int Rarray[36] = {
   9,8,7,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,
};

int z;

void setup(){

 Serial.begin(9600);
}


void loop(){

for (z = 0; z = 35; z++){
Qarray[z] = Rarray[z];
}

Serial.println(Qarray[1]);

}


If I'm assigning it like this everything works:

Code: [Select]


Qarray[1] = Rarray[1];
Qarray[2] = Rarray[2];
....



Regards,
Jazzar

edit:
ok z should only run to 35 since the size of the array is 36 but last digit is number 35, anyway didn't help the least :-(
Title: Re: Assigning Arrays wont work
Post by: mem on Oct 05, 2009, 11:23 am
try:

void loop(){
 for (z = 0; z [glow]<[/glow] 36; z++){
    Qarray[z] = Rarray[z];
}
Title: Re: Assigning Arrays wont work
Post by: jazzar on Oct 05, 2009, 11:36 am
Wow! Thanks a lot mem!

It works!

Any suggestion why? (since it should be the same) (just for curiosity)

Best regards,
Jazzar
Title: Re: Assigning Arrays wont work
Post by: mem on Oct 05, 2009, 12:03 pm
for loops repeat until the middle expression becomes false (zero)

the middle expression of your for loop was: z = 35;  this sets the variable z to 35. The value of this expression never goes to zero and the loop never exits so your Serial.print code is never executed

z < 36 will be true (non-zero) for all values of z from 0 to 35 so the loop will repeat for each element in the array and stop when z is not less than 36.
Title: Re: Assigning Arrays wont work
Post by: jazzar on Oct 05, 2009, 01:02 pm
Ah thanks!

Somehow got it mixed up... repeat until z equals 35 >.<