Problem calling functions within draw()
in
Programming Questions
•
1 year ago
Hi,
The following code produces an array of numbers. But there seem to be numbers over numbers and I am not sure how to fix this.
I think the problem may be to do with the fact that the two functions are called from inside draw() and so are endlessly running. I tried placing the call to them outside of this but inside draw() seems to be the only place i can get them to work.
Any help would be greatly appreciated.
Thanks,
Shane
The following code produces an array of numbers. But there seem to be numbers over numbers and I am not sure how to fix this.
I think the problem may be to do with the fact that the two functions are called from inside draw() and so are endlessly running. I tried placing the call to them outside of this but inside draw() seems to be the only place i can get them to work.
Any help would be greatly appreciated.
Thanks,
Shane
- int cols = 5;
int rows = 10;
int xn0 = 1;
int yn0 = 1;
float h = 0.1;
float[][] myArray = new float[cols][rows]; // declares array
void setup() {
size(400, 400);
background(255);
fill(0);
}
void draw() {
populateArray();
readArray();
}
void populateArray() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
if (j==0);
{
myArray[1][0] = xn0;
myArray[2][0] = yn0;
}
if (i==0) // first column
{
myArray[i][j] = j+1;
}
if ((i==1) && (j!=0)) // xn values, adds h to the previous xn value
{
myArray[i][j] = h + myArray[i][j-1];
}
if ((i==2) && (j!=0)) // yn values, previous yn value
{
myArray[i][j] = myArray[i+2][j-1];
}
if (i==3) // fn values, fn = (xn^3 - yn)/xn
{
myArray[i][j] = (pow(myArray[i-2][j], 3) - myArray[i-1][j])/myArray[i-2][j];
}
if ((i==4)) // y_{n+1} values, yn+1 = yn + h*fn
{
myArray[i][j] = myArray[i-2][j] + h*myArray[i-1][j];
}
}
}
}
void readArray() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
if (j==0);
{
text(myArray[i][j], ((i+1)*40), ((j+1)*20)+20);
}
}
}
text("n", 50, 15);
text("xn", 90, 15);
text("yn", 130, 15);
text("fn", 170, 15);
text("y_{n+1}", 210, 15);
line(40, 23, 250, 23);
line(80, 5, 80, (rows*20)+30);
}
1