How to return a specific element within an array?

Asked

Viewed 171 times

0

I’m trying to return the largest among 4 numbers, for this I’m using the function qsort() available on <stdlib.h>. Follows the code:

 int comparison (const void *a, const void *b){
        if (*(int*)a == (*(int*)b)){
            return 0;
        }
        if (*(int*)a > (*(int*)b)){
            return 1;
        } else {
            return -1;
        }
    }

    int max_of_four(int a, int b, int c, int d){
        int vector[4] = {a, b, c, d};
        qsort(vector, 4, sizeof(int), comparison);
        return
    }

    int main() {
        int a, b, c, d;
        scanf("%d %d %d %d", &a, &b, &c, &d);
        int ans = max_of_four(a, b, c, d);
        printf("%d", ans);


        return 0;
    }

Has a return open there, as I do to return the last element of my array vector now ordered? Would you have some way to do that same function for an unlimited number of numbers instead of 4?

1 answer

1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.