0 Members and 1 Guest are viewing this topic.
function AveragePing(): integer;vari : integer;ping: integer;totalping: integer;begin for i := 1 to 32 do begin totalping := totalping + GetPlayerStat(i,'Ping') i := i + 1if i = 32 thenbeginping := totalping / NumPlayersend;end;Result := ping;end;
function AveragePing(): integer;var totalping, i: integer;begin for i := 1 to 32 do totalping := totalping + GetPlayerStat(i,'Ping'); Result := totalping / NumPlayers;end;
It's a bit confusing i would write it this way:Code: [Select]function AveragePing(): integer;var totalping, i: integer;begin for i := 1 to 32 do totalping := totalping + GetPlayerStat(i,'Ping'); Result := totalping / NumPlayers;end;
Quote from: PerroAZUL on June 11, 2007, 09:28:46 pmIt's a bit confusing i would write it this way:Code: [Select]function AveragePing(): integer;var totalping, i: integer;begin for i := 1 to 32 do totalping := totalping + GetPlayerStat(i,'Ping'); Result := totalping / NumPlayers;end;Your way is correct; the code in the original post breaks the rule of "Don't modify FOR counting variables inside a FOR loop", and is incorrect.
Quote from: chrisgbk on June 11, 2007, 10:27:37 pmQuote from: PerroAZUL on June 11, 2007, 09:28:46 pmIt's a bit confusing i would write it this way:Code: [Select]function AveragePing(): integer;var totalping, i: integer;begin for i := 1 to 32 do totalping := totalping + GetPlayerStat(i,'Ping'); Result := totalping / NumPlayers;end;Your way is correct; the code in the original post breaks the rule of "Don't modify FOR counting variables inside a FOR loop", and is incorrect.... except the original code isn't using a for loop. It is emulating a for-loop with a while-loop. I don't believe there's anything wrong with his code from a "technically speaking" point of viewbut yes, the other code is better
function AveragePing(): integer;vari : integer;ping: integer;totalping: integer;begin for i := 1 to 32 do begin <<<<<<<<<<<<<<< totalping := totalping + GetPlayerStat(i,'Ping') i := i + 1if i = 32 thenbeginping := totalping / NumPlayersend;end;Result := ping;end;
Does getplayerstat return 0 if there is no player in that slot? If not, both will give a biased measure.
function AveragePing(): integer;var totalping, i: integer;begin if Numplayers > 0 then begin for i := 1 to 32 do totalping := totalping + GetPlayerStat(i,'Ping'); Result := totalping / NumPlayers;end;end;