简体   繁体   中英

Return the largest number in array

typedef struct {int PID;
                int Priority;
                char *Status[8];
                char *Program[20];}Process;

Hard coded values

void initTable()
{
    processList[0].PID = 112;
    processList[0].Priority = 1;
    strcpy(processList[0].Status,"Ready");
    strcpy(processList[0].Program,"Alex.txt");
    processList[1].PID = 101;
    processList[1].Priority = 3;
    strcpy(processList[1].Status,"Running");
    strcpy(processList[1].Program,"Alex.txt");
    processList[2].PID = 103;
    processList[2].Priority = 2;
    strcpy(processList[2].Status,"Ready");
    strcpy(processList[2].Program,"Alex.txt");

}

I want to return i that has the highest Priority value in this example i = 1 is the highest Priority value

int getNextProcess()
{
    for (int i = 0; i < len; ++i) {
        // return the i with the highest Priority value
    }


    
    return i;
}
// assuming `Priority` value is never less than 0
// returns -1 if something bad happens
int getNextProcess(Process *processList, int len)
{
    int greatest = -1;
    int greatest_i = -1;
    for (int i = 0; i < len; ++i) {
        if (processList[i].Priority > greatest) {
            greatest = processList[i].Priority;
            greatest_i = i;
    }
}
return greatest_i;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM