SlideShare a Scribd company logo
Generative Coding
arrays
• Using variables , we can store information at one step in the program
for use at a later time. We store a single value per variable. Arrays now
allow us to cache multiple values ​​at once. An array is a list of
variables where each element has the same data type . An array is a
data structure.
Declaration of an array
• Arrays must be declared like variables before they can be used, except that there are square brackets after the data type:
// Declaration of an integer variable
int number;
// declaration of an integer array
int [] numbers;
To initialize an array, we need to define the length of the array . The length determines how many elements can be stored in the array.
In the following example we define our array numbers so that five elements (of type int) can be stored in it, and the other array positionen so that we float
can store 30 elements, i.e. 30 values.
// Array with five integer elements
int [] numbers = new int [5];
// Array of thirty point number elements
float [] positions = new float [30];
An array is initialized like this: new data type[length]; so for example new float[4];
Access to individual items
• We can access each item through an index . This index is always an integer and starts with 0 . Each element
is of the same data type and can be used like a normal variable. When we access a single element of the array,
we can read and write its value.
• Here we see how to assign new values ​​to the elements of an array
int x = 5;
int [] numbers = new int [5];
numbers[0] = 100;
numbers[1] = 255;
numbers[2] = 2;
numbers[3] = width ;
numbers[4] = 10 * x;
Access to individual items
• We can use the individual elements as well as variables:
int x = new numbers[4];
int y = 4;
y = y + numbers[3];
line (x, y, numbers[1], numbers[2]);
• And of course we can also use a variable or expressions for the index:
int number = 10;
// Be it initialization
char [] character = new char [number];
// Or when accessed
int i = 5;
numbers[i] = numbers[2 + 1];
Arrays & Loops
• When using or computing a lot of similar data, arrays are very useful.
• For example, if we wanted to animate a ball, we had to store its
position and speed in variables: x, y, vx, vy. If not just one but several
balls are to be displayed, you would have to use four variables per ball
( x2, y2, vx2, vy2, x3, y3, ...), which would be very tedious. Instead, we
can use arrays.
• So the purpose of arrays is that we can store many of the same kind
of variables together — and save us a lot of unnecessary typing and
programming.
Using arrays
• In order to access each element, we can either specify each position
individually ( ), or access all of them numbers[3] using a for loop .
• We can then use the counter variable of a for loop directly as an
index:
int [] numbers = new int [5];
for ( int i = 0; i < 5; i++) {
numbers[i] = i * 10;
}
Example:
draws ten circles using the values ​​of two arrays as positions.
float [] x = new float [10];
float [] y = new float [10];
Void setup ( ) {
size (200, 200);
// Initialize all elements of both arrays
for ( int i = 0; i < 10; i++) {
x[i] = random ( width );
y[i] = random ( height );
}
}
void draw () {
// Draw circles using a loop and the arrays
for ( int i = 0; i < 10; i++) {
ellipse (x[i], y[i], 20, 20);
}
]

More Related Content

PPTX
Arrays in programming
PPTX
Chapter-Five.pptx
PPT
Mesics lecture 8 arrays in 'c'
PDF
javaarray
PPTX
Arrays
PPT
C++ Arrays
PPT
C++ Arrays
PDF
Learn Java Part 9
Arrays in programming
Chapter-Five.pptx
Mesics lecture 8 arrays in 'c'
javaarray
Arrays
C++ Arrays
C++ Arrays
Learn Java Part 9

Similar to Generative Coding Lecture notes using coding (20)

PPTX
arrays in c programming - example programs
DOC
Arrays and Strings
PDF
Learn Java Part 8
PPTX
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
PPT
2 arrays
PPTX
arrays.pptx
PPT
CHAPTER-5.ppt
PPT
PF Lecture 9.ppt hakakbshisokwb jaiksnsjomabhj
PPTX
Lecture_01.2.pptx
PPT
C96e1 session3 c++
PPT
arrayy.ppt
PDF
Week06
PPT
Cso gaddis java_chapter8
PDF
An Introduction to Programming in Java: Arrays
PDF
Lecture 6 - Arrays
PPTX
Array lecture
PPT
Array
PDF
Array-part1
PPTX
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
arrays in c programming - example programs
Arrays and Strings
Learn Java Part 8
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
2 arrays
arrays.pptx
CHAPTER-5.ppt
PF Lecture 9.ppt hakakbshisokwb jaiksnsjomabhj
Lecture_01.2.pptx
C96e1 session3 c++
arrayy.ppt
Week06
Cso gaddis java_chapter8
An Introduction to Programming in Java: Arrays
Lecture 6 - Arrays
Array lecture
Array
Array-part1
Unit4pptx__2024_11_ 11_10_16_09.pptx
Ad

Recently uploaded (20)

PDF
MARIJA CVITKOVAC - GRAPHIC DESIGN PORTFOLIO 2025
PPTX
TLE-10-PPTHAJAOSBDJDEKSNbknbtktktmktkttk
PDF
Biophilic Sound Design for Luxury Wellness Centers
PDF
Portfolio Arch Estsabel Chourio - Interiorism,
PDF
SS27 Women's Fashion Trend Book Peclers Paris
PPTX
OPERATIONS MANAGEMENT REPORT AMAZON (1).pptx
PDF
Wio LTE JP Version v1.3b- 4G, Cat.1, Espruino Compatible\202001935, PCBA;Wio ...
PDF
10 Real World Lessons and 4 Practical Tips for Large Group LSP Method
PDF
Shape Language for Character Design by Adhec Saputra
PDF
High-frequency high-voltage transformer outline drawing
PDF
Lesson Blue World Health Day Facebook Post.pdf
PDF
10 Best UI UX Design Company in UK for User-Centric Solutions
PPTX
knee & Ankle Joint Mobilization techniques.pptx
PPTX
8Oe2l3FUJVImF34F988.pptxbbbvvvvvvvhhhhhh
PPTX
Creative Agency Presentation For Designers
PPT
unit 1 ppt.ppthhhhhhhhhhhhhhhhhhhhhhhhhh
PPTX
Introduction-to-Graphic-Design-and-Adobe-Photoshop.pptx
PDF
Find Your Target Audience A 6-Step Framework to Grow Your Business.pdf
PPTX
Riverfront Development_nashikcity_landscape
PDF
Benefits_of_Cast_Aluminium_Doors_Presentation.pdf
MARIJA CVITKOVAC - GRAPHIC DESIGN PORTFOLIO 2025
TLE-10-PPTHAJAOSBDJDEKSNbknbtktktmktkttk
Biophilic Sound Design for Luxury Wellness Centers
Portfolio Arch Estsabel Chourio - Interiorism,
SS27 Women's Fashion Trend Book Peclers Paris
OPERATIONS MANAGEMENT REPORT AMAZON (1).pptx
Wio LTE JP Version v1.3b- 4G, Cat.1, Espruino Compatible\202001935, PCBA;Wio ...
10 Real World Lessons and 4 Practical Tips for Large Group LSP Method
Shape Language for Character Design by Adhec Saputra
High-frequency high-voltage transformer outline drawing
Lesson Blue World Health Day Facebook Post.pdf
10 Best UI UX Design Company in UK for User-Centric Solutions
knee & Ankle Joint Mobilization techniques.pptx
8Oe2l3FUJVImF34F988.pptxbbbvvvvvvvhhhhhh
Creative Agency Presentation For Designers
unit 1 ppt.ppthhhhhhhhhhhhhhhhhhhhhhhhhh
Introduction-to-Graphic-Design-and-Adobe-Photoshop.pptx
Find Your Target Audience A 6-Step Framework to Grow Your Business.pdf
Riverfront Development_nashikcity_landscape
Benefits_of_Cast_Aluminium_Doors_Presentation.pdf
Ad

Generative Coding Lecture notes using coding

  • 2. arrays • Using variables , we can store information at one step in the program for use at a later time. We store a single value per variable. Arrays now allow us to cache multiple values ​​at once. An array is a list of variables where each element has the same data type . An array is a data structure.
  • 3. Declaration of an array • Arrays must be declared like variables before they can be used, except that there are square brackets after the data type: // Declaration of an integer variable int number; // declaration of an integer array int [] numbers; To initialize an array, we need to define the length of the array . The length determines how many elements can be stored in the array. In the following example we define our array numbers so that five elements (of type int) can be stored in it, and the other array positionen so that we float can store 30 elements, i.e. 30 values. // Array with five integer elements int [] numbers = new int [5]; // Array of thirty point number elements float [] positions = new float [30]; An array is initialized like this: new data type[length]; so for example new float[4];
  • 4. Access to individual items • We can access each item through an index . This index is always an integer and starts with 0 . Each element is of the same data type and can be used like a normal variable. When we access a single element of the array, we can read and write its value. • Here we see how to assign new values ​​to the elements of an array int x = 5; int [] numbers = new int [5]; numbers[0] = 100; numbers[1] = 255; numbers[2] = 2; numbers[3] = width ; numbers[4] = 10 * x;
  • 5. Access to individual items • We can use the individual elements as well as variables: int x = new numbers[4]; int y = 4; y = y + numbers[3]; line (x, y, numbers[1], numbers[2]); • And of course we can also use a variable or expressions for the index: int number = 10; // Be it initialization char [] character = new char [number]; // Or when accessed int i = 5; numbers[i] = numbers[2 + 1];
  • 6. Arrays & Loops • When using or computing a lot of similar data, arrays are very useful. • For example, if we wanted to animate a ball, we had to store its position and speed in variables: x, y, vx, vy. If not just one but several balls are to be displayed, you would have to use four variables per ball ( x2, y2, vx2, vy2, x3, y3, ...), which would be very tedious. Instead, we can use arrays. • So the purpose of arrays is that we can store many of the same kind of variables together — and save us a lot of unnecessary typing and programming.
  • 7. Using arrays • In order to access each element, we can either specify each position individually ( ), or access all of them numbers[3] using a for loop . • We can then use the counter variable of a for loop directly as an index: int [] numbers = new int [5]; for ( int i = 0; i < 5; i++) { numbers[i] = i * 10; }
  • 8. Example: draws ten circles using the values ​​of two arrays as positions. float [] x = new float [10]; float [] y = new float [10]; Void setup ( ) { size (200, 200); // Initialize all elements of both arrays for ( int i = 0; i < 10; i++) { x[i] = random ( width ); y[i] = random ( height ); } } void draw () { // Draw circles using a loop and the arrays for ( int i = 0; i < 10; i++) { ellipse (x[i], y[i], 20, 20); } ]