Skip to main content
1-Visitor
April 7, 2021
Solved

For loop iterating over a matrix

I'm trying to iterate over a matrix and for some reason I can't seem to generate the new matrix. It just spits out the last value of the iteration.

Looking at other posts I think I have something wrong with the indices of the matrix, but I can't quite figure it out.

 

SB_9890617_0-1617810428796.png

 

-MFra-Answer
21-Topaz II
April 7, 2021

SB 98 answer.jpg

8 replies

-MFra-21-Topaz IIAnswer
21-Topaz II
April 7, 2021

SB 98 answer.jpg

Translate
1-Visitor
April 7, 2021

Thank you, very much!

Translate
23-Emerald IV
April 7, 2021

The result of a program is the last statement executed. In your case that's the assignment of the last element to the array omega. So the result is that last element.

If you add just omega after the for loop, as what MFra showed, you get the entire array.

Note that there is a specific keyword: 'return' that you can use to define the result of a function. Put anywhere in the function, the item placed as the argument to 'return' is the result of the program. Also be aware that 'return' also stops execution of the program. So if you use it within a program construct (If, For or whatever) the program stops there.

But putting it on the last line of a program certainly is no crime.

 

Success!
Luc

Translate
1-Visitor
April 7, 2021

Thank you very much for the in depth explanation!

Translate
ttokoro
21-Topaz I
May 11, 2021

image.pngimage.pngimage.pngimage.png

Translate
t.t.