Finding max value in an array

后端 未结 5 970
闹比i
闹比i 2021-01-07 13:57
int highNum = 0;
int m;
int list[4] = {10, 4, 7, 8};
    for (m = 0 ; m < size ; m++);
    {
        if (list[m] > highNum)
            highNum = list[m];
             


        
5条回答
  •  不思量自难忘°
    2021-01-07 14:36

    Unless you're doing this for homework and have to write the loop, just use std::max_element, as in:

    int list[4] = {10, 4, 7, 8};
    std::cout << *std::max_element(list, list+4);
    

    ...or better, avoid hard-coding the length:

    int list[] = {10, 4, 7, 8};
    std::cout << *std::max_element(std::begin(list), std::end(list));
    

提交回复
热议问题