Answers.Body: <p>After testing and researching for a while, I found out that the first  lines of code that controls the moving of the ultrasonic sensor is the cause of the error. Previously, the code will make the motor move 2 degrees, then get the measurement of the ultrasonic sensor and put it inside the numerical array. It then loops 130 times so there is 130 measurements and 260 degrees of rotation.
  <a href="https://i.sstatic.net/boUne.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/boUne.png" alt="enter image description here"></a>
  What I am doing is to replace the entire array with that one value. Thus, when I get the value of the array, I am getting an non-existent value. Also, the array starts from zero, not one, so using the loop index for the array will not work as the loop index will not display a zero value index.  </p>
  
  <p>Instead, I should use the 'write at index' mode of array operations. This way, it will add values to the array, not replace the entire array with that one value.
  <a href="https://i.sstatic.net/LPbBr.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LPbBr.png" alt="enter image description here"></a>
  Now this is correct. Before that, I thought that the loop index was the cause of error.  </p>
  
  <p>First time answering my own question, Im so proud lol.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-11T13:06:49.130Z
Answers.LastActivityDate: 2020-05-11T13:06:49.130Z
Answers.UserDisplayName: lin zh
Body: <p>My program moves an ultrasonic sensor around 260 degrees and the values are recorded down in an array variable every 2 degrees it turns. It moves back and measure it 3 more times, storing each measurement in a different array variable (in total there are 4 array variables). </p>
  
  <p>I want to get the average value from the four array variable thus I use the 'read at index' mode of the array operations.
  <a href="https://i.sstatic.net/ufOQ2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ufOQ2.png" alt="The program"></a></p>
  
  <p>It uses the loop index to get the index of the arrays. Then it takes the values and calculates the average before putting them in another variable array. However, the brick shows an error message right after the ultrasonic sensor is finished measuring.</p>
  
  <p>I removed some parts of the program in order to find the cause of the error and I found that it might have something to do with the loop index. It might also have been because the array is not found. I tried using another variable that adds one every loop and then uses it to get the index but the message would appear.</p>
  
  <p>I am not sure why there is the error, nothing contradicts the program, there is no math error, it should have worked. I hope you guys can tell me why this happens and how to solve it. Thank you!</p>
  
  <p>Here is the complete program:
  <a href="https://i.sstatic.net/gNQys.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gNQys.png" alt="enter image description here"></a>
  <strong>Clarifications</strong>
   It loops 130 times as the ultrasonic sensor measures every 2 degrees it turns, so if it turns 260 degrees, there will be 130 measurements, 130 values in the arrays.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-10T10:51:56.417Z
Comments.Text: FYI: You don't need to "remove" parts of the program when you can hold CTRL and left-click certain groups of code, then you can press the "Run Selected" button, which looks like a play button with parentheses. It will do the same thing as removing certain parts of the program: run the selected parts.
Comments.UserDisplayName: mindstormsboi
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-10T11:16:10.730Z
Comments.Text: Also, I see another code block hiding on the bottom of the first image, does it serve any purpose in your code?
Comments.UserDisplayName: mindstormsboi
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-10T11:45:20.320Z
Comments.Text: No, that is just the block palette. It is the red block.
Comments.UserDisplayName: lin zh
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-10T11:47:37.420Z
Comments.Text: Okay, but I hope you know that the arrow on the screen wouldn't show up if it was in the palette. Perhaps delete it and run the program again?
Comments.UserDisplayName: mindstormsboi
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-10T11:51:35.167Z
Comments.Text: No there is absolutely nothing there, thanks for helping.
Comments.UserDisplayName: lin zh
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-10T18:57:24.517Z
Comments.Text: I know the error you're talking about, and this might help you: https://bricks.stackexchange.com/a/6387/15028
Comments.UserDisplayName: mindstormsboi
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-11T04:10:57.100Z
Comments.Text: Yeah that might be the problem but i am nit sure how to rectify it
Comments.UserDisplayName: lin zh
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-11T07:47:56.967Z
Comments.Text: Could it be that you are trying to read the #0 or the #261 element in the array? That would definitely lead to such errors.
Comments.UserDisplayName: zovits
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-11T11:48:31.777Z
Comments.Text: @zovits I don't see such big numbers in the code, but I do see number 130 used in the loops, which is not really much of a problem.
Comments.UserDisplayName: mindstormsboi
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-11T12:47:48.793Z
Comments.Text: @linzh You might be attempting to read a variable that doesn't exist. For example, You can use the advanced math block and attempt to add a+h. H does not exist. FYI, only A-D exist since they are the motor ports (i think). You can't write a sensor name into the block, however you can write a motor port letter there.
Comments.UserDisplayName: mindstormsboi
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-05-11T13:07:38.460Z
Comments.Text: Yeah both @Zovitis and mindstormsboi is right, just figured out the problem and have posted a answer to it. Thank both you so much!
Comments.UserDisplayName: lin zh
Tags: <ev3><programming>
Title: Error message when using loop index as an array index to get value in arrays
UserDisplayName: lin zh
ThreadText: # Error message when using loop index as an array index to get value in arrays
  
  (Asked by: username_0 on 2020-05-10)
  
  My program moves an ultrasonic sensor around 260 degrees and the values are recorded down in an array variable every 2 degrees it turns. It moves back and measure it 3 more times, storing each measurement in a different array variable (in total there are 4 array variables).
  
  I want to get the average value from the four array variable thus I use the 'read at index' mode of the array operations.
  [![The program](https://i.sstatic.net/ufOQ2.png)](https://i.sstatic.net/ufOQ2.png)
  
  It uses the loop index to get the index of the arrays. Then it takes the values and calculates the average before putting them in another variable array. However, the brick shows an error message right after the ultrasonic sensor is finished measuring.
  
  I removed some parts of the program in order to find the cause of the error and I found that it might have something to do with the loop index. It might also have been because the array is not found. I tried using another variable that adds one every loop and then uses it to get the index but the message would appear.
  
  I am not sure why there is the error, nothing contradicts the program, there is no math error, it should have worked. I hope you guys can tell me why this happens and how to solve it. Thank you!
  
  Here is the complete program:
  [![enter image description here](https://i.sstatic.net/gNQys.png)](https://i.sstatic.net/gNQys.png)
  **Clarifications**
  It loops 130 times as the ultrasonic sensor measures every 2 degrees it turns, so if it turns 260 degrees, there will be 130 measurements, 130 values in the arrays.
  
  **Tags:** ev3, programming
  
  > **Comments:**
  > **username_1**: FYI: You don't need to "remove" parts of the program when you can hold CTRL and left-click certain groups of code, then you can press the "Run Selected" button, which looks like a play button with parentheses. It will do the same thing as removing certain parts of the program: run the selected parts.  
  > **username_1**: Also, I see another code block hiding on the bottom of the first image, does it serve any purpose in your code?  
  > **username_0**: No, that is just the block palette. It is the red block.  
  > **username_1**: Okay, but I hope you know that the arrow on the screen wouldn't show up if it was in the palette. Perhaps delete it and run the program again?  
  > **username_0**: No there is absolutely nothing there, thanks for helping.  
  > **username_1**: I know the error you're talking about, and this might help you: https://bricks.stackexchange.com/a/6387/15028  
  > **username_0**: Yeah that might be the problem but i am nit sure how to rectify it  
  > **username_2**: Could it be that you are trying to read the #0 or the #261 element in the array? That would definitely lead to such errors.  
  > **username_1**: @username_2 I don't see such big numbers in the code, but I do see number 130 used in the loops, which is not really much of a problem.  
  > **username_1**: @username_0 You might be attempting to read a variable that doesn't exist. For example, You can use the advanced math block and attempt to add a+h. H does not exist. FYI, only A-D exist since they are the motor ports (i think). You can't write a sensor name into the block, however you can write a motor port letter there.  
  > **username_0**: Yeah both @Zovitis and username_1 is right, just figured out the problem and have posted a answer to it. Thank both you so much!  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-05-11. Score: 2
  
  After testing and researching for a while, I found out that the first lines of code that controls the moving of the ultrasonic sensor is the cause of the error. Previously, the code will make the motor move 2 degrees, then get the measurement of the ultrasonic sensor and put it inside the numerical array. It then loops 130 times so there is 130 measurements and 260 degrees of rotation.
  [![enter image description here](https://i.sstatic.net/boUne.png)](https://i.sstatic.net/boUne.png)
  What I am doing is to replace the entire array with that one value. Thus, when I get the value of the array, I am getting an non-existent value. Also, the array starts from zero, not one, so using the loop index for the array will not work as the loop index will not display a zero value index.
  
  Instead, I should use the 'write at index' mode of array operations. This way, it will add values to the array, not replace the entire array with that one value.
  [![enter image description here](https://i.sstatic.net/LPbBr.png)](https://i.sstatic.net/LPbBr.png)
  Now this is correct. Before that, I thought that the loop index was the cause of error.
  
  First time answering my own question, Im so proud lol.
  
  
  ---
  
  