question

Kari Payton avatar image
0 Likes"
Kari Payton asked Mischa Spelt commented

For loop to find largest number in table column.

Hi,

I need help writing a code that finds the highest value in a table's column. Here's my attempt but it returns a 0.

  1. Table MainData = Table("MainDataTable");
  2.  
  3.  
  4. int oldResult = 0;
  5. int newResult = 0;
  6.  
  7.  
  8. for( int row = 1; row<= Table("MainDataTable").numRows; row++)
  9. newResult = Table("MainDataTable")[row][1];
  10. if (newResult >= oldResult)
  11. oldResult = newResult;
  12.  
  13.  
  14. return newResult;

FlexSim 17.2.1
code editor
tablecodehelp.png (5.4 KiB)
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Matthew Gillespie avatar image
0 Likes"
Matthew Gillespie answered Kari Payton commented

You need to return oldResult, not newResult, since that is where you're storing the current biggest number.

· 3
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.