We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
arrays (Read 423 times)
arrays
Aug 25th, 2009, 1:47pm
 
hello can anyone help me with this array problem
im trying to put some values (spring2[p][q].len) in an array like ive shown below; but for some reason when i println the array "ar" it just prints one value.

float [] ar =new float [600];

 for (int r = 0; r <ar.length; r+=1){
 
  for (int p = 0; p <49; p+=1)
     {
       for (int q =0; q<49; q+=4)
       {
         
           spring2[p][q]=  new Spring(nodes[p][q],nodes[p-1][q-1]);
           ar[r] =spring2[p][q].len;
       
       }
     }
   }
   println(ar);// just prints 1 value instead of 600?


i have used a seperate for loop for the array to distinguish it from p and q loop and i think this is creating the problem, however im not sure how else to separate p and q from the new array?? does any one have any ideas???

thanks
Re: arrays
Reply #1 - Aug 25th, 2009, 1:57pm
 
yes, by putting the r loop around everything you're doing the two inner loops 600 times. you just need an index that increments on every iteration

int r = 0;
for (p...) {
 for (q...) {
   ar[r] = ...
   r++;
 }
}
Re: arrays
Reply #2 - Aug 25th, 2009, 1:59pm
 
but your problem might just be that println() isn't doing what you expect for arrays. would you really want it to print 600 values? what if the array was 100000000 elements long?
Re: arrays
Reply #3 - Aug 25th, 2009, 2:21pm
 
Code seems fine.  I don't have spring objects or a .len property so I just swapped in a random number from 0-10 for each float.

Quote:
float [] ar =new float [600];

for (int r = 0; r <ar.length; r+=1){
  for (int p = 0; p <49; p+=1)
  {
    for (int q =0; q<49; q+=4)
    {
      ar[r] = random(10);
    }
  }
}
println(ar); // prints out 600 values just fine, here


(of course you'll get the same results by removing the p and q loops entirely)
-- Ben
Re: arrays
Reply #4 - Aug 25th, 2009, 3:20pm
 
thank you thats really helpful!
Page Index Toggle Pages: 1