Bubble Sort + enum
#1

Hello,
I wrote this function code:
pawn Код:
#include <a_samp>

enum LIST_INFO
{
    l_int,
    l_str[10],
    Float:l_float
}
new List[][LIST_INFO] = {
    {2,"Test2",2.2},
    {1,"Test1",1.1},
    {4,"Test4",4.4},
    {3,"Test3",3.3},
    {5,"Test5",5.5}
};

stock BubbleSort(array[][LIST_INFO],index = 0,start = 0,len = sizeof(array),len2 = sizeof(array[]))
{
    for(new i=start;i<len-1+start;i++)
    {
        for(new j=start;j<len-1-i+start;j++)
        {
            if(array[j+1][LIST_INFO:index] < array[j][LIST_INFO:index])
            {
                new temp[LIST_INFO];
                for(new t;t<len2;t++)
                {
                    temp[LIST_INFO:t] = array[j+1][LIST_INFO:t];
                    array[j+1][LIST_INFO:t] = array[j][LIST_INFO:t];
                    array[j][LIST_INFO:t] = temp[LIST_INFO:t];
                }
            }
        }
    }
    return 1;
}

public OnFilterScriptInit()
{
    BubbleSort(List);
    for(new i;i<sizeof(List);i++) printf("%d %s %.f",List[i][l_int],List[i][l_str],List[i][l_float]);
    return 1;
}
and I want to overwrite it into a universal form something like this:
pawn Код:
#include <a_samp>

enum LIST_INFO
{
    l_int,
    l_str[10],
    Float:l_float
}
new List[][LIST_INFO] = {
    {2,"Test2",2.2},
    {1,"Test1",1.1},
    {4,"Test4",4.4},
    {3,"Test3",3.3},
    {5,"Test5",5.5}
};

stock BubbleSort(array[][LIST_INFO],enum ENUM_NAME,index = 0,start = 0,len = sizeof(array),len2 = sizeof(array[]))
{
    for(new i=start;i<len-1+start;i++)
    {
        for(new j=start;j<len-1-i+start;j++)
        {
            if(array[j+1][ENUM_NAME:index] < array[j][ENUM_NAME:index])
            {
                new temp[LIST_INFO];
                for(new t;t<len2;t++)
                {
                    temp[ENUM_NAME:t] = array[j+1][ENUM_NAME:t];
                    array[j+1][ENUM_NAME:t] = array[j][ENUM_NAME:t];
                    array[j][ENUM_NAME:t] = temp[ENUM_NAME:t];
                }
            }
        }
    }
    return 1;
}

public OnFilterScriptInit()
{
    BubbleSort(List,LIST_INFO);
    for(new i;i<sizeof(List);i++) printf("%d %s %.f",List[i][l_int],List[i][l_str],List[i][l_float]);
    return 1;
}
When I compile, it writes:
Quote:

C:\Users\Atoma_000\Desktop\samp03z\filterscripts\b ub.pwn(17) : error 010: invalid function or declaration
C:\Users\Atoma_000\Desktop\samp03z\filterscripts\b ub.pwn(17) : error 017: undefined symbol "ENUM_NAME"
C:\Users\Atoma_000\Desktop\samp03z\filterscripts\b ub.pwn(17) : error 010: invalid function or declaration
C:\Users\Atoma_000\Desktop\samp03z\filterscripts\b ub.pwn(17 -- 19) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


4 Errors.

Reply
#2

You should use weak enum tags (weak tags start with lower case letter, strong with upper case)
Weak tags don't case warnings if the tag wasn't defined in the function

Also the sort function doesn't seem correct
pawn Код:
enum eListInfo
{
    l_int,
    l_str[10],
    Float:l_float
}
new List[][eListInfo] = {
    {2,"Test2",2.2},
    {1,"Test1",1.1},
    {4,"Test4",4.4},
    {3,"Test3",3.3},
    {5,"Test5",5.5}
};

BubbleSort(array[][], idx = 0, start = 0, len = sizeof(array)) { // code from wikipedia
    new
        i,
        l
    ;
    len--;

    do {
        for(l = 0, i = start; i < len; ++i) {
            if(array[i + 1][idx] < array[i][idx]) {
                ExchangeArraySlots(array, i + 1, (l = i)); // taken from md-sort by Slice
                // https://sampforum.blast.hk/showthread.php?tid=343172
            }
        }
    } while((len = l));
}
Reply
#3

Thank you, but it did not solve my problem.
I'm trying to do, how to work with stock and any enum list of 'usages variables.
Reply
#4

Well it does work with any enum and any variable (strings are only sorted by the first char)
pawn Код:
enum eListInfo { l_int, l_str[10], Float:l_float }

new List[][eListInfo] = {
    {2,"Test2",2.2},
    {1,"Test1",1.1},
    {4,"Test4",4.4},
    {3,"Test3",3.3},
    {5,"Test5",5.5}
};

enum eDataInfo { d_int, d_str[10], Float:d_float }

new Data[][eDataInfo] = {
    {7,"Test3",1.1},
    {4,"Test2",8.8},
    {2,"Test5",3.3},
    {8,"Test6",5.5},
    {6,"Test9",7.7}
};

public OnFilterScriptInit() {
    BubbleSort(List, l_int);
    for(new i;i<sizeof(List);i++) printf("%d %s %.f",List[i][l_int],List[i][l_str],List[i][l_float]);
    BubbleSort(Data, d_float);
    for(new i;i<sizeof(Data);i++) printf("%d %s %.f",Data[i][d_int],Data[i][d_str],Data[i][d_float]);
}
If that is still the wrong thing could you explain it a bit more in depth ?
Reply
#5

Thank you, I already knew how to write:
pawn Код:
#include <a_samp>

enum eLIST_INFO
{
    l_int,
    l_str[10],
    Float:l_float
}
new List[][eLIST_INFO] = {
    {2,"Test2",2.2},
    {1,"Test1",1.1},
    {4,"Test4",4.4},
    {3,"Test3",3.3},
    {5,"Test5",5.5}
};

stock BubbleSort(array[][],index = 0,start = 0,len = sizeof(array),len2 = sizeof(array[]))
{
    for(new i=start;i<len-1+start;i++)
    {
        for(new j=start;j<len-1-i+start;j++)
        {
            if(array[j+1][index] < array[j][index])
            {
                for(new t;t<len2;t++)
                {
                    new temp = array[j+1][t];
                    array[j+1][t] = array[j][t];
                    array[j][t] = temp;
                }
            }
        }
    }
    return 1;
}

public OnFilterScriptInit()
{
    BubbleSort(List,l_int);
    for(new i;i<sizeof(List);i++) printf("%d %s %.f",List[i][l_int],List[i][l_str],List[i][l_float]);
    return 1;
}
It works !
Thanks a lot !
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)