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.
IndexProgramming Questions & HelpPrograms › Help with program structure
Page Index Toggle Pages: 1
Help with program structure (Read 496 times)
Help with program structure
Apr 20th, 2009, 6:07am
 
Hi all

I'm v new to processing (and programming) and trying to get my head around using arrays

I've written a simple program that generates an array, displays the values, sums them and draws a basic bar char (see below)

I think it would be better in the long run to separate the part the code that draws the chart from the code that builds the array.  However, I can't work out how to access the array values from outside of 'void testArray'

Hope that makes sense  Undecided

N

Code:


int bg = (int) random (1, 360);

void setup(){

 size (200, 180);
 colorMode(HSB, 360, 100, 100);
 //int bg = (int) random (1, 360);
 background(bg, 20, 90);

 PFont font;
 font = loadFont ("ArialMT-20.vlw");
 textFont (font);
 noLoop();
}

void draw(){
 fill(bg, 10, 90);
 rect(80, 22, 100, 100);
 fill(360);
 testArray();
 fill(0);
 line (40, 20, 40, 160);
}

void testArray () {

 int [] testArray = new int [5];
 int sum = 0;
 int xpos = 20;
 int ypos = 40;
 int rectYpos = 22;

 for (int i = 0; i < testArray.length; i++){
   testArray[i] = (int) random (10);
   //println (testArray);
   sum = sum += testArray[i];
   //println (sum);
   text (i, xpos, ypos);
   text (testArray[i], xpos + 30, ypos);
   ypos = ypos + 20;


   rect(80, rectYpos, testArray[i]*10, 20);
   rectYpos += 20;

 }
 fill (0);
 text (sum, xpos +30 , ypos+5);
}






Re: Help with program structure
Reply #1 - Apr 20th, 2009, 7:30am
 
the void in "void testArray" is what is returned by the array.
in your case, it should return an int[].. so "int[] testArray"
then, at the bottom of your testArray function, add "return testArray;"
and then you make another function, i.e. sumArray, and use the testArray function in that


Code:


int bg = (int) random (1, 360);

void setup(){

 size (200, 180);
 colorMode(HSB, 360, 100, 100);
 //int bg = (int) random (1, 360);
 background(bg, 20, 90);

 PFont font;
 font = loadFont ("ArialMT-20.vlw");
 textFont (font);
 noLoop();
}

void draw(){
 fill(bg, 10, 90);
 rect(80, 22, 100, 100);
 fill(360);
 sumArray();
 fill(0);
 line (40, 20, 40, 160);
}

int[] testArray (int length) {

 int [] testArray = new int [length];
 for (int i = 0; i < testArray.length; i++){
   testArray[i] = (int) random (10);
 }
return testArray; // i mentioned this line in my comment
}

void sumArray(){
 int[] testArray = testArray(5); // and here we use the function, requestion an arraylength of 5
 int sum = 0;
 int xpos = 20;
 int ypos = 40;
 int rectYpos = 22;

 for (int i = 0; i < testArray.length; i++){
   //println (testArray);
   sum = sum += testArray[i];
   //println (sum);
   text (i, xpos, ypos);
   text (testArray[i], xpos + 30, ypos);
   ypos = ypos + 20;


   rect(80, rectYpos, testArray[i]*10, 20);
   rectYpos += 20;

 }
 fill (0);
 text (sum, xpos +30 , ypos+5);
}

Re: Help with program structure
Reply #2 - Apr 20th, 2009, 8:13am
 
Ahhh that's it

Brilliant

Thanks very much - I really appreciate it
Page Index Toggle Pages: 1