recitation7: functions and arrays

code:

float[] posX = new float[100];
float[] posY= new float[100];
color[] col= new color[100];
//type[] name = new type[length];
//declare array; build array

void setup(){
  size (700,700);
}

void draw() {
  for (int i=0; i<100; i++){
     if (mousePressed == true) {   
       posX[i]=i*i*i;
    posY[i]=i*i*i+random(700);
    col [i]= color( random(255), random (255), random (255));
    display (posX[i],  posY[i], col[i]);
   }
  } 
}

void display(float x, float y, color c) {
  strokeWeight(2);
  fill(c);  
  ellipse(x, y, 230, 230);
  fill(c-100);
  rectMode (CENTER);
  rect (x,y,150,150);
  fill (c-50);
  ellipse (x, y, 80,80);
}

Question 1:

In your own words, please explain the difference between having your for loop from Step 2 in setup() as opposed to in draw().

When I haven’t added the interactions or animations, the results are the same when I put loop in the  setup and the draw. Both of them are a still picture of the thing I drew. When I added a mouse-interaction in the loop, the two are different. When putting the loop in the the setup function, the canvas is always empty without any drawings whenever I press the mouse. When I put the loop in the draw function, it works the way I want.

Question 2:

What is the benefit of using arrays?  How might you use arrays in a potential project?

I have the information on the website:https://www.educba.com/advantages-of-array/

Saves memory

Memory can be allocated dynamically in an array. This advantage of array helps to save the memory of the system. It also helps when the pre-defined array has insufficient memory. At runtime memory can be allocated manually during run time. Also when memory allocation is not dynamic it stored the data in contiguous memory locations. The amount of storage required depends on the data type or size.

Easier debugging

When taken into consideration a linked list, it is usually time-consuming to check if an index is valid or not. Similarly, it is difficult to check it in a hash table as well. But when it comes to an array it has its specified indexes and hence optimal to use. It can be directly traversed with the index position.

Helps in reusability of code

One of the major advantages of an array is that they can be declared once and reused multiple times. It represents multiple values by making use of a single variable. This helps in improvement of reusability of code and also improves the readability of the code. If in this situation no array is used then we will need to store multiple values in multiple variables.

Multi-dimensional arrays

These can be defined as an array of arrays. Data which is present in tabular format like 1D, 2D, etc. can be defined. The total number of elements can be stored in the multi-dimensional array and can be calculated by multiplying the size of all dimensions.

To draw a conclusion, array is a more efficient way to store values. It costs less time and easier to debug.

How I’ll use array in a potential project?

I want to have different people moving different objects on the screen, I may assign these objects to an array and use it to store value.

Leave a Reply