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

Similar to Generative Coding Lecture notes using coding (20)

PPT
17-Arrays en java presentación documento
DiegoGamboaSafla
 
PPT
9 cm604.9
myrajendra
 
PDF
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
PPT
Arrays Class presentation
Neveen Reda
 
PPT
Array1
Rajendran
 
PPT
Chap09
Terry Yoast
 
PDF
Lecture 6 - Arrays
Syed Afaq Shah MACS CP
 
PDF
(2) collections algorithms
Nico Ludwig
 
PPT
Mesics lecture 8 arrays in 'c'
eShikshak
 
PDF
C sharp chap6
Mukesh Tekwani
 
PPT
DEMO.ppt
ChristianCabadongga
 
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
PPT
Array in Java
Shehrevar Davierwala
 
PPTX
ch 7 single dimension array in oop .pptx
nilampatoliya
 
PPT
Eo gaddis java_chapter_07_5e
Gina Bullock
 
PPT
Comp102 lec 8
Fraz Bakhsh
 
ODP
(2) collections algorithms
Nico Ludwig
 
PPS
Java session04
Niit Care
 
PPTX
Chapter 7.1
sotlsoc
 
PPTX
Object oriented programming2 Week 2.pptx
MirHazarKhan1
 
17-Arrays en java presentación documento
DiegoGamboaSafla
 
9 cm604.9
myrajendra
 
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
Arrays Class presentation
Neveen Reda
 
Array1
Rajendran
 
Chap09
Terry Yoast
 
Lecture 6 - Arrays
Syed Afaq Shah MACS CP
 
(2) collections algorithms
Nico Ludwig
 
Mesics lecture 8 arrays in 'c'
eShikshak
 
C sharp chap6
Mukesh Tekwani
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
Array in Java
Shehrevar Davierwala
 
ch 7 single dimension array in oop .pptx
nilampatoliya
 
Eo gaddis java_chapter_07_5e
Gina Bullock
 
Comp102 lec 8
Fraz Bakhsh
 
(2) collections algorithms
Nico Ludwig
 
Java session04
Niit Care
 
Chapter 7.1
sotlsoc
 
Object oriented programming2 Week 2.pptx
MirHazarKhan1
 

Recently uploaded (20)

PDF
WEEK3-Literary-Gennnnnnnnnnnnnnnnnres.pdf
MaybelynVergara
 
PPTX
Exploring Types of Rocks Educational Presentation rock forming james harold r...
jamescarllfelomino6
 
PPTX
DEVELOPING-PARAGRAPHS.pptx-developing...
rania680036
 
PPTX
sistem teknologi yang di desain untuk mahasiswa dan dosen agar memudahkan mer...
gamesonlya2rj
 
PDF
ARC-101-B-4.pdfxxxxxxxxxxxxxxxxxxxxxxxxx
IzzyBaniquedBusto
 
PDF
AI Intervention in Design & Content Creation
YellowSlice1
 
PPTX
HIGH DENSITY CONCRETE-Concrete Technology
mayurbhandari2123
 
PDF
SS27 Men's Fashion Trend Book Peclers Paris
Peclers Paris
 
PDF
Presentation - Interior Design Concepts (2).pdf
vrindagrawal456
 
PDF
Design Social Change Creating Social Change
Eduardo Corrêa
 
PPTX
Visit Biogas Refresher Slide_Jun 2025.pptx
isyraffemir
 
PPTX
Adobe Creative Cloud Cleaner Tool Crack Free Download Latest Version 2025
Slideshare
 
PDF
Case Study on good and bad acoustics in auditorium
Disha Agrawal
 
PPTX
feminist gnsudnshxujenduxhsixisjxuu.pptx
rowvinafujimoto
 
PPTX
ash green THEMEN PPT WITH CYCLONE DONATIOANS ASN DUNDARTIONPROSAL
Younghusbandwife
 
PPTX
Bldg Mtc 8 Maintance documentation and audits - 25 (2).pptx
MwanamomoMpamba
 
PDF
🔴BUKTI KEMENANGAN HARI INI SELASA 08 JULI 2025 !!!🔴
GRAB
 
PPTX
SAMPLE FILE OF-PPT-FINAL-ORAL-DEFENSE.pptx
Yvez2
 
PDF
The Role of Logos as Identity Shapers (IFIC Logo)
Md. Mehedi Hasan Asif
 
PPTX
Town planning is a concept used in architectural design. It plays a very impo...
IshikaPanchal11
 
WEEK3-Literary-Gennnnnnnnnnnnnnnnnres.pdf
MaybelynVergara
 
Exploring Types of Rocks Educational Presentation rock forming james harold r...
jamescarllfelomino6
 
DEVELOPING-PARAGRAPHS.pptx-developing...
rania680036
 
sistem teknologi yang di desain untuk mahasiswa dan dosen agar memudahkan mer...
gamesonlya2rj
 
ARC-101-B-4.pdfxxxxxxxxxxxxxxxxxxxxxxxxx
IzzyBaniquedBusto
 
AI Intervention in Design & Content Creation
YellowSlice1
 
HIGH DENSITY CONCRETE-Concrete Technology
mayurbhandari2123
 
SS27 Men's Fashion Trend Book Peclers Paris
Peclers Paris
 
Presentation - Interior Design Concepts (2).pdf
vrindagrawal456
 
Design Social Change Creating Social Change
Eduardo Corrêa
 
Visit Biogas Refresher Slide_Jun 2025.pptx
isyraffemir
 
Adobe Creative Cloud Cleaner Tool Crack Free Download Latest Version 2025
Slideshare
 
Case Study on good and bad acoustics in auditorium
Disha Agrawal
 
feminist gnsudnshxujenduxhsixisjxuu.pptx
rowvinafujimoto
 
ash green THEMEN PPT WITH CYCLONE DONATIOANS ASN DUNDARTIONPROSAL
Younghusbandwife
 
Bldg Mtc 8 Maintance documentation and audits - 25 (2).pptx
MwanamomoMpamba
 
🔴BUKTI KEMENANGAN HARI INI SELASA 08 JULI 2025 !!!🔴
GRAB
 
SAMPLE FILE OF-PPT-FINAL-ORAL-DEFENSE.pptx
Yvez2
 
The Role of Logos as Identity Shapers (IFIC Logo)
Md. Mehedi Hasan Asif
 
Town planning is a concept used in architectural design. It plays a very impo...
IshikaPanchal11
 
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); } ]