Skip to content

Commit 1ec8233

Browse files
authored
Merge pull request #971 from suhascv/example5
Added Walk Over 2dArray example |Prof Harris |Open@RIT
2 parents 27b4eea + a94b305 commit 1ec8233

File tree

3 files changed

+255
-0
lines changed

3 files changed

+255
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* @name Walk Over 2dArray
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> How to display 2D array contents on the canvas
6+
using regular for and for-of loops in multiple different ways.<br/>
7+
A function is created for the canvas, the 2D
8+
array (Friend Array) is initialized and walked over using nested
9+
loops in different ways. Variables x and y are used to place the
10+
array item on the canvas in the form of 2D array.
11+
The final nested loop is used to initialize 2D
12+
array (Fish Array) with random Integers (fish ages).
13+
*/
14+
15+
16+
//"use strict"; //catch some common coding errors
17+
18+
19+
/**
20+
* setup :
21+
*/
22+
function setup() {
23+
createCanvas(400, 600);
24+
//create 2D array, slide 4
25+
let friendArray = [
26+
["Nona", "mac & cheese", "orange", "Eid al-fitr"],
27+
["Marylin", "ice cream", "blue", "Halloween"],
28+
["Rashaad", "garbage plates", "turquoise", "Christmas"],
29+
["Ava", "sushi", "pink", "New Years"]
30+
];
31+
friendArray.push(["Xavier", "Louisiana creole", "red", "their birthday"]);
32+
33+
//walking 2D array, slide 6
34+
let y = 20; // Start row based on text size of 20
35+
for (let f = 0; f < friendArray.length; f++) { // outer array
36+
let x = 10; // Start item in this row
37+
for (let t = 0; t < friendArray[f].length; t++) { //inner
38+
text(friendArray[f][t], x, y);
39+
x += textWidth(friendArray[f][t]) + 20; //place next item
40+
}
41+
y += 28; // place next row
42+
}
43+
44+
//walking 2D array, variation on slide 6
45+
//with embedded arithmetic for y
46+
//
47+
for (let f = 0; f < friendArray.length; f++) { // outer array
48+
let x = 10; // Start item in this row
49+
for (let t = 0; t < friendArray[f].length; t++) { //inner
50+
//y is v-padding + LCV * v-spacing
51+
text(friendArray[f][t], x, 200 + f * 28);
52+
x += textWidth(friendArray[f][t]) + 20; //place next item
53+
}
54+
}
55+
56+
//walking 2D array, slide 7
57+
//need to use x and y variables to manage canvas placement
58+
y = 400;
59+
for (let friend of friendArray) {
60+
let x = 10; // Start item in this row
61+
console.log("x and y", x, y);
62+
console.log("friend:", friend);
63+
for (let item of friend) {
64+
console.log("item & x:", item, x);
65+
text(item, x, y);
66+
x += textWidth(item) + 20; //place next item
67+
}
68+
y += 28; // place next row
69+
}
70+
71+
//slide 9, creating 2D array: schools of fish ages
72+
console.log("\n *** Fish ages in 2D ***");
73+
const schools = [];
74+
//4 schools of fish
75+
for (let t = 0; t < 4; t++) {
76+
schools[t] = []; //initialize this school
77+
console.log("schools[t]?", t, schools[t]);
78+
79+
// Add 10 randomized ages to the array
80+
for (let a = 0; a < 10; a++) {
81+
schools[t].push(round(random(1, 5)));
82+
}
83+
}
84+
console.log(schools);
85+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* @name Walk Over 2dArray
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> How to display 2D array contents on the canvas
6+
using regular for and for-of loops in multiple different ways.<br/>
7+
A function is created for the canvas, the 2D
8+
array (Friend Array) is initialized and walked over using nested
9+
loops in different ways. Variables x and y are used to place the
10+
array item on the canvas in the form of 2D array.
11+
The final nested loop is used to initialize 2D
12+
array (Fish Array) with random Integers (fish ages).
13+
*/
14+
15+
16+
//"use strict"; //catch some common coding errors
17+
18+
19+
/**
20+
* setup :
21+
*/
22+
function setup() {
23+
createCanvas(400, 600);
24+
//create 2D array, slide 4
25+
let friendArray = [
26+
["Nona", "mac & cheese", "orange", "Eid al-fitr"],
27+
["Marylin", "ice cream", "blue", "Halloween"],
28+
["Rashaad", "garbage plates", "turquoise", "Christmas"],
29+
["Ava", "sushi", "pink", "New Years"]
30+
];
31+
friendArray.push(["Xavier", "Louisiana creole", "red", "their birthday"]);
32+
33+
//walking 2D array, slide 6
34+
let y = 20; // Start row based on text size of 20
35+
for (let f = 0; f < friendArray.length; f++) { // outer array
36+
let x = 10; // Start item in this row
37+
for (let t = 0; t < friendArray[f].length; t++) { //inner
38+
text(friendArray[f][t], x, y);
39+
x += textWidth(friendArray[f][t]) + 20; //place next item
40+
}
41+
y += 28; // place next row
42+
}
43+
44+
//walking 2D array, variation on slide 6
45+
//with embedded arithmetic for y
46+
//
47+
for (let f = 0; f < friendArray.length; f++) { // outer array
48+
let x = 10; // Start item in this row
49+
for (let t = 0; t < friendArray[f].length; t++) { //inner
50+
//y is v-padding + LCV * v-spacing
51+
text(friendArray[f][t], x, 200 + f * 28);
52+
x += textWidth(friendArray[f][t]) + 20; //place next item
53+
}
54+
}
55+
56+
//walking 2D array, slide 7
57+
//need to use x and y variables to manage canvas placement
58+
y = 400;
59+
for (let friend of friendArray) {
60+
let x = 10; // Start item in this row
61+
console.log("x and y", x, y);
62+
console.log("friend:", friend);
63+
for (let item of friend) {
64+
console.log("item & x:", item, x);
65+
text(item, x, y);
66+
x += textWidth(item) + 20; //place next item
67+
}
68+
y += 28; // place next row
69+
}
70+
71+
//slide 9, creating 2D array: schools of fish ages
72+
console.log("\n *** Fish ages in 2D ***");
73+
const schools = [];
74+
//4 schools of fish
75+
for (let t = 0; t < 4; t++) {
76+
schools[t] = []; //initialize this school
77+
console.log("schools[t]?", t, schools[t]);
78+
79+
// Add 10 randomized ages to the array
80+
for (let a = 0; a < 10; a++) {
81+
schools[t].push(round(random(1, 5)));
82+
}
83+
}
84+
console.log(schools);
85+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* @name Walk Over 2dArray
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> How to display 2D array contents on the canvas
6+
using regular for and for-of loops in multiple different ways.<br/>
7+
A function is created for the canvas, the 2D
8+
array (Friend Array) is initialized and walked over using nested
9+
loops in different ways. Variables x and y are used to place the
10+
array item on the canvas in the form of 2D array.
11+
The final nested loop is used to initialize 2D
12+
array (Fish Array) with random Integers (fish ages).
13+
*/
14+
15+
16+
//"use strict"; //catch some common coding errors
17+
18+
19+
/**
20+
* setup :
21+
*/
22+
function setup() {
23+
createCanvas(400, 600);
24+
//create 2D array, slide 4
25+
let friendArray = [
26+
["Nona", "mac & cheese", "orange", "Eid al-fitr"],
27+
["Marylin", "ice cream", "blue", "Halloween"],
28+
["Rashaad", "garbage plates", "turquoise", "Christmas"],
29+
["Ava", "sushi", "pink", "New Years"]
30+
];
31+
friendArray.push(["Xavier", "Louisiana creole", "red", "their birthday"]);
32+
33+
//walking 2D array, slide 6
34+
let y = 20; // Start row based on text size of 20
35+
for (let f = 0; f < friendArray.length; f++) { // outer array
36+
let x = 10; // Start item in this row
37+
for (let t = 0; t < friendArray[f].length; t++) { //inner
38+
text(friendArray[f][t], x, y);
39+
x += textWidth(friendArray[f][t]) + 20; //place next item
40+
}
41+
y += 28; // place next row
42+
}
43+
44+
//walking 2D array, variation on slide 6
45+
//with embedded arithmetic for y
46+
//
47+
for (let f = 0; f < friendArray.length; f++) { // outer array
48+
let x = 10; // Start item in this row
49+
for (let t = 0; t < friendArray[f].length; t++) { //inner
50+
//y is v-padding + LCV * v-spacing
51+
text(friendArray[f][t], x, 200 + f * 28);
52+
x += textWidth(friendArray[f][t]) + 20; //place next item
53+
}
54+
}
55+
56+
//walking 2D array, slide 7
57+
//need to use x and y variables to manage canvas placement
58+
y = 400;
59+
for (let friend of friendArray) {
60+
let x = 10; // Start item in this row
61+
console.log("x and y", x, y);
62+
console.log("friend:", friend);
63+
for (let item of friend) {
64+
console.log("item & x:", item, x);
65+
text(item, x, y);
66+
x += textWidth(item) + 20; //place next item
67+
}
68+
y += 28; // place next row
69+
}
70+
71+
//slide 9, creating 2D array: schools of fish ages
72+
console.log("\n *** Fish ages in 2D ***");
73+
const schools = [];
74+
//4 schools of fish
75+
for (let t = 0; t < 4; t++) {
76+
schools[t] = []; //initialize this school
77+
console.log("schools[t]?", t, schools[t]);
78+
79+
// Add 10 randomized ages to the array
80+
for (let a = 0; a < 10; a++) {
81+
schools[t].push(round(random(1, 5)));
82+
}
83+
}
84+
console.log(schools);
85+
}

0 commit comments

Comments
 (0)