Pages: [1]   Go Down
Author Topic: Assigning Arrays wont work (solved)  (Read 815 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 122
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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:

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 :-(
« Last Edit: October 05, 2009, 06:16:19 am by jazzar » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try:

void loop(){
  for (z = 0; z [glow]<[/glow] 36; z++){
     Qarray[z] = Rarray[z];
}
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 122
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow! Thanks a lot mem!

It works!

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

Best regards,
Jazzar
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 122
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah thanks!

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

Pages: [1]   Go Up
Jump to: