Raised This Month: $ Target: $400
 0% 

Rank in a for loop


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
tm.
Member
Join Date: Apr 2010
Old 07-15-2010 , 14:00   Rank in a for loop
Reply With Quote #1

I don't know how to explain this, so:

Code:
new iPlayers[32], iNum     get_players(iPlayers, iNum, "ch")         for(new i; i < iNum; i++)     {         //from here no idea ?         if(g_something[iPlayers][0] > g_something[iPlayer][1])?         ...........................................         console_print(id, "1. Best player, 2.Second, 3.....     }
tm. is offline
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 07-15-2010 , 16:13   Re: Rank in a for loop
Reply With Quote #2

PHP Code:
new maxplayers get_maxplayers()
for (new 
1i<=maxplayersi++) 
     for (new 
j<=maxplayersi++)
            if (
g_something[i] < g_something[j]) {
                      new 
aux g_something[i]
                      
g_something[i] = g_something[j]
                      
g_something[j] = aux          
            
}

//When you have all the players in order, you show them
for (new 1i<=maxplayersi++) {
     new 
name[32]
     
get_user_name(g_something[i], name31)
     
console_print(id"%d. %s"iname)

I use 2 loops for more prolixity, but you can show the first player before the first loop ("i") ends

Last edited by Mxnn; 07-16-2010 at 14:14.
Mxnn is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-15-2010 , 16:31   Re: Rank in a for loop
Reply With Quote #3

Explain where/how info is stored that determines a players rank. Are players already in order of ranks in that array.

Mxnn, you should not declare variables within a loop.
__________________

Last edited by Bugsy; 07-15-2010 at 16:33.
Bugsy is offline
grimvh2
Veteran Member
Join Date: Nov 2007
Location: Fishdot Nation
Old 07-15-2010 , 18:18   Re: Rank in a for loop
Reply With Quote #4

Quote:
Originally Posted by Bugsy View Post

Mxnn, you should not declare variables within a loop.
Why? just asking :p
__________________
I am out of order!
grimvh2 is offline
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 07-15-2010 , 18:21   Re: Rank in a for loop
Reply With Quote #5

Quote:
Originally Posted by grimvh2 View Post
Why? just asking :p
Because it is inefficient.
By adding new to a for loop you basically delete and realloc memory for every cycle.
It is just a use of CPU for something that can be avoided or something useless.
Ex:
PHP Code:
for (new i=0;i<500;i++)
{
  new 
x;

Means:

PHP Code:
// 500 times alloc and reloc
for (new i=0;i<500;i++)
{
  
alloc memory x;
// code
  
delete memory x;

__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
grimvh2
Veteran Member
Join Date: Nov 2007
Location: Fishdot Nation
Old 07-15-2010 , 20:07   Re: Rank in a for loop
Reply With Quote #6

Quote:
Originally Posted by ot_207 View Post
Because it is inefficient.
By adding new to a for loop you basically delete and realloc memory for every cycle.
It is just a use of CPU for something that can be avoided or something useless.
Ex:
PHP Code:
for (new i=0;i<500;i++)
{
  new 
x;

Means:

PHP Code:
// 500 times alloc and reloc
for (new i=0;i<500;i++)
{
  
alloc memory x;
// code
  
delete memory x;

oh ofc, i misunderstood hes sentence, yes im aware of that :p
__________________
I am out of order!
grimvh2 is offline
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 07-16-2010 , 00:41   Re: Rank in a for loop
Reply With Quote #7

I edited my post.
Thanks for the tip Bugsy. I do it so because i have seen it in some plugins
Mxnn is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-16-2010 , 00:49   Re: Rank in a for loop
Reply With Quote #8

Quote:
Originally Posted by Mxnn View Post
PHP Code:
new maxplayers get_maxplayers()
new 
ij
for (1i<=maxplayersi++) 
     for (
j<=maxplayersi++)
            if (
g_something[i] < g_something[j]) {
                      new 
aux g_something[i]
                      
g_something[i] = g_something[j]
                      
g_something[j] = aux          
            
}

//When you have all the players in order, you show them
for (1i<=maxplayersi++) {
     new 
name[32]
     
get_user_name(g_something[i], name31)
     
console_print(id"%d. %s"iname)

I think you misunderstood what we are telling you. It is ok to use new in the for-loop line to create your index variable. Where you do not want to use new is inside of the loop code block.
PHP Code:
new maxplayers get_maxplayers()
new 
aux
for ( new 1i<=maxplayersi++) 
     for ( new 
j<=maxplayersi++)
            if (
g_something[i] < g_something[j]) {
                      
//move outside of loop new aux = g_something[i]
                      
aux g_something[i]
                      
g_something[i] = g_something[j]
                      
g_something[j] = aux          
            
}

//When you have all the players in order, you show them
new name32 ]
for (new 
1i<=maxplayersi++) {
     
//move outside of loop new name[32] 
     
get_user_name(g_something[i], name31)
     
console_print(id"%d. %s"iname)

__________________

Last edited by Bugsy; 07-16-2010 at 00:54.
Bugsy is offline
tm.
Member
Join Date: Apr 2010
Old 07-16-2010 , 00:57   Re: Rank in a for loop
Reply With Quote #9

Quote:
Originally Posted by Bugsy View Post
Explain where/how info is stored that determines a players rank. Are players already in order of ranks in that array.
I don't use it anywhere at this thime, but I might need to. It was a general question. The info's are just global arrays that could store integers, floats etc, and no, they are not in any particular order. It's not necessary to be in a loop, I just need a way to rank some values and I thought in a for loop could be done.

@Mxnn: Thanks, I'll give it a try later, I can't test anything right now and they are talking about this -> http://wiki.alliedmods.net/Optimizin...ables_in_Loops.

Last edited by tm.; 07-16-2010 at 01:04.
tm. is offline
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 07-16-2010 , 02:02   Re: Rank in a for loop
Reply With Quote #10

Ohhh, i missunderstood, sorry!

@tm. My post is an example. I don't know what class of data you want to put in order.
Mxnn is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:10.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode