cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Invalid Array

YA_10963798
8-Gravel

Invalid Array

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

 

1 ACCEPTED SOLUTION

Accepted Solutions

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

 

 

 

View solution in original post

12 REPLIES 12

MartinHanak_1-1710416270202.png

Try removing left square bracket above the red arrow.


Martin Hanák

YA_10963798_0-1710416532795.png

 

Hi,

"red" expression is different from others.


Martin Hanák

Hi Martin,

Actually I just copied paste the same program for all of them and changed the numbers. It works in all of them only the last one . When I did what you recommended it didn't work either, Why do you think it doesn't work only in the last one?

Regards,

Yusra 

Hi,

I think you have to upload your Mathcad file.


Martin Hanák

Here you are 


@YA_10963798 wrote:

Here you are 


MartinHanak_0-1710417985839.png

 


Martin Hanák

Interesting 

How to solve that?

Hahaha ,,, it is up to you.


Martin Hanák

If I knew how to solve, I wouldn't ask ;(

 

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

 

 

 

Thank you so much you are always a life saver. 

Top Tags