Skip to main content
14-Alexandrite
March 14, 2024
Solved

Invalid Array

  • March 14, 2024
  • 2 replies
  • 4188 views

I worte the same program for seven function and it works in all of them ,but doesn't work in the last one. I have no clue why?

Do you see the problem? There is no difference , HOWEVER the error shows only in last one.

YA_10963798_0-1710414278784.png

 

Best answer by Werner_E

As Martin already noticed is the reason for this error that the largest value in vector K7 is the last value. Your program then tries to access the values after that element but because its the last value, the values you try to access do not exist - hence the error.

Simple solution is to use an if-statement to deal with that single situation. The loop is only entered when the maximum value is not the last in the vector. Otherwise the vector is returned unchanged.

But its quite inefficient to write a separate program for each of the six variables K2 to K7.

I would strongly suggest to write ONE function (which you could place anywhere above, maybe in a collapsed region) and use this function to calculate the six vectors:

Werner_E_0-1710419838247.png

You sure will want to name the function differently 😉

 

What was your intention with the next calculation?

Werner_E_1-1710419906927.png

I guess you wanted to sum up all the values in vector K2, right.?

So you made two errors:
1) you add "j" to the variable "sum", but you do not re-assign the result to the variable "sum". So "sum" remains zero

2) You do not provide a return value, so the program returns the last value it deals with and thats in your case just  the last value in vector K2

Here is a corrected version

Werner_E_2-1710420135905.png

But it would be much easier to use the sum operator which Prime offers out of the box (simply don't fill the placeholders at the bottom and the top of the Sigma symbol):

Werner_E_3-1710420198519.png  
Werner_E_4-1710420208109.png

 

 

 

2 replies

24-Ruby III
March 14, 2024

MartinHanak_1-1710416270202.png

Try removing left square bracket above the red arrow.

14-Alexandrite
March 14, 2024

YA_10963798_0-1710416532795.png

 

24-Ruby III
March 14, 2024

Hi,

I think you have to upload your Mathcad file.

Werner_E25-Diamond IAnswer
25-Diamond I
March 14, 2024

As Martin already noticed is the reason for this error that the largest value in vector K7 is the last value. Your program then tries to access the values after that element but because its the last value, the values you try to access do not exist - hence the error.

Simple solution is to use an if-statement to deal with that single situation. The loop is only entered when the maximum value is not the last in the vector. Otherwise the vector is returned unchanged.

But its quite inefficient to write a separate program for each of the six variables K2 to K7.

I would strongly suggest to write ONE function (which you could place anywhere above, maybe in a collapsed region) and use this function to calculate the six vectors:

Werner_E_0-1710419838247.png

You sure will want to name the function differently 😉

 

What was your intention with the next calculation?

Werner_E_1-1710419906927.png

I guess you wanted to sum up all the values in vector K2, right.?

So you made two errors:
1) you add "j" to the variable "sum", but you do not re-assign the result to the variable "sum". So "sum" remains zero

2) You do not provide a return value, so the program returns the last value it deals with and thats in your case just  the last value in vector K2

Here is a corrected version

Werner_E_2-1710420135905.png

But it would be much easier to use the sum operator which Prime offers out of the box (simply don't fill the placeholders at the bottom and the top of the Sigma symbol):

Werner_E_3-1710420198519.png  
Werner_E_4-1710420208109.png

 

 

 

14-Alexandrite
March 14, 2024

Thank you so much you are always a life saver.