Unit 15 – Array

The code of the last example is very long. But actually almost the same things are repeated all the time, no? We can do it easier – using the array.

An array is a special type of variable. It can not only store one but multiple values.

Think of it as a cargo ship. It can store containers. Every container has got it’s own number. If you want to look inside, you need to tell the number. Those numbers start with 0 not with 1 what sometimes is confusing.

Of course the array also has to be declared (created). There are several ways to do that.

This is how to declare an empty array without value assignment.

`int nums[6];`

An array named «nums» is created. The variable type is int (integer numbers). Arrays usually can only store values of the same type. The 6 in the brackets means that our cargo ship has got six containers where we can put cargo (numbers) in. It is called the length of an array.

An other way to create an array:

`int nums[] = {10, 78, 45, 67, 38, 14};`

Now we declare the array and directly assign values. We don’t have to tell it the length. By the way, you find the square brackets [] on your keyboard pressing Alt-Gr + 8 and Alt-Gr + 9 on a PC or Alt + 5 and Alt + 6 on a Mac. So, what is the length of the array? Right: It again is six.

But how can we write values into the array? Actually, it is done the same way like with normal variables. The only difference is that we have to state the number of the container.

`nums[3] = 50;`

And how can we read it out? Let’s say we want to know the value of the fourth position (remember, it starts with 0 so the fourth position is 3) and write it into an other variable.

`a = nums[3];`

In the last unit, we played different tones. We had to repeat almost the same commands again and again. Now, we can simply put the frequencies into an array:

`int frequencies[] = {262, 294, 330, 349, 392, 392);`

Easy! We have an array of the variable type int. It is named frequencies and the frequencies we need are already assigned. OK, let’s see what is where:

``` frequencies[0] returns 262
frequencies[1] returns 294
frequencies[5] returns 392```

Cool, we can use the array like a normal variable. We only have to state the position.
For a melody we even need the duration of the tone. Let’s put it into a second array:

`int tonDuration[]= {200, 200, 200, 200, 400, 400);`

The length of both arrays must be the same. To play one by one we will use a for-loop. The counter variable will count, which tone should be played and sends it to the arrays:

```int frequencies[] = {262, 294, 330, 349, 392, 392};
int tonDuration[]= {200, 200, 200, 200, 400, 400};
int toneQuantity = 6;

void setup() {

}

void loop() {
for (int theTone=0; theTone< toneQuantity; theTone = theTone +1){
tone(9, frequencies[theTone]);
delay(tonDuration[theTone]);
noTone(9);
delay(tonDuration[theTone]);
}
}```

This code is much shorter. Now, play the whole melody of the last unit.

```int frequencies[] = {262, 294, 330, 349, 392, 392, 440, 440, 440, 440, 392, 440, 440, 440, 440, 392, 349, 349, 349, 349, 330, 330, 392, 392, 392, 392, 262};
int tonDuration[]= {200, 200, 200, 200, 400, 400, 200, 200, 200, 200, 800, 200, 200, 200, 200, 800, 200, 200, 200, 200, 400, 400, 200, 200, 200, 200, 800};
int toneQuantity = 27;

void setup() {

}

void loop() {
for (int theTone=0; theTone< toneQuantity; theTone=theTone+1){
tone(9, frequencies[theTone]);
delay(tonDuration[theTone]);
noTone(9);
delay(tonDuration[theTone]);
}
}```

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.
The text passage provides an introduction to arrays in programming, explaining how arrays can store multiple values and how to declare, initialize, and access elements in an array. It also includes an example of using arrays to play a melody with specific frequencies and durations.
no_question

the passage does not contain any mathematical questions or problems. it is purely instructional and provides examples of how to use arrays in code.
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
Unit 15 – Array

The code of the last example is very long. But actually almost the same things are repeated all the time, no? We can do it easier – using the array.

An array is a special type of variable. It can not only store one but multiple values.

Think of it as a cargo ship. It can store containers. Every container has got it’s own number. If you want to look inside, you need to tell the number. Those numbers start with 0 not with 1 what sometimes is confusing.

Of course the array also has to be declared (created). There are several ways to do that.

This is how to declare an empty array without value assignment.

`int nums[6];`

An array named «nums» is created. The variable type is int (integer numbers). Arrays usually can only store values of the same type. The 6 in the brackets means that our cargo ship has got six containers where we can put cargo (numbers) in. It is called the length of an array.

An other way to create an array:

`int nums[] = {10, 78, 45, 67, 38, 14};`

Now we declare the array and directly assign values. We don’t have to tell it the length. By the way, you find the square brackets [] on your keyboard pressing Alt-Gr + 8 and Alt-Gr + 9 on a PC or Alt + 5 and Alt + 6 on a Mac. So, what is the length of the array? Right: It again is six.

But how can we write values into the array? Actually, it is done the same way like with normal variables. The only difference is that we have to state the number of the container.

`nums[3] = 50;`

And how can we read it out? Let’s say we want to know the value of the fourth position (remember, it starts with 0 so the fourth position is 3) and write it into an other variable.

`a = nums[3];`

In the last unit, we played different tones. We had to repeat almost the same commands again and again. Now, we can simply put the frequencies into an array:

`int frequencies[] = {262, 294, 330, 349, 392, 392);`

Easy! We have an array of the variable type int. It is named frequencies and the frequencies we need are already assigned. OK, let’s see what is where:

``` frequencies[0] returns 262
frequencies[1] returns 294
frequencies[5] returns 392```

Cool, we can use the array like a normal variable. We only have to state the position.
For a melody we even need the duration of the tone. Let’s put it into a second array:

`int tonDuration[]= {200, 200, 200, 200, 400, 400);`

The length of both arrays must be the same. To play one by one we will use a for-loop. The counter variable will count, which tone should be played and sends it to the arrays:

```int frequencies[] = {262, 294, 330, 349, 392, 392};
int tonDuration[]= {200, 200, 200, 200, 400, 400};
int toneQuantity = 6;

void setup() {

}

void loop() {
for (int theTone=0; theTone< toneQuantity; theTone = theTone +1){
tone(9, frequencies[theTone]);
delay(tonDuration[theTone]);
noTone(9);
delay(tonDuration[theTone]);
}
}```

This code is much shorter. Now, play the whole melody of the last unit.

```int frequencies[] = {262, 294, 330, 349, 392, 392, 440, 440, 440, 440, 392, 440, 440, 440, 440, 392, 349, 349, 349, 349, 330, 330, 392, 392, 392, 392, 262};
int tonDuration[]= {200, 200, 200, 200, 400, 400, 200, 200, 200, 200, 800, 200, 200, 200, 200, 800, 200, 200, 200, 200, 400, 400, 200, 200, 200, 200, 800};
int toneQuantity = 27;

void setup() {

}

void loop() {
for (int theTone=0; theTone< toneQuantity; theTone=theTone+1){
tone(9, frequencies[theTone]);
delay(tonDuration[theTone]);
noTone(9);
delay(tonDuration[theTone]);
}
}```

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.
user
The text passage provides an introduction to arrays in programming, explaining how arrays can store multiple values and how to declare, initialize, and access elements in an array. It also includes an example of using arrays to play a melody with specific frequencies and durations.

TYPE: NO_QUESTION

The passage does not contain any mathematical questions or problems. It is purely instructional and provides examples of how to use arrays in code.