-6

I have to know which of these variable have the highest value:

A=1
B=500
C=100
D=700
E=5
F=1000

Which is the easiest way to do this?

Pablo De Luca
  • 795
  • 3
  • 15
  • 29
  • 3
    You'll need to specify at least *some* base code you've already tried if you want help with something. With your current question all we can do is spell it out from start to end, and that's not what this site is for. Show a bit more effort ;) – Niels Keurentjes May 07 '13 at 01:15
  • `return max(max(max(max(max(A, B), C), D), E), F);`, døh. –  May 07 '13 at 01:15
  • possible duplicate of [C library function to do sort](http://stackoverflow.com/questions/1787996/c-library-function-to-do-sort) – Shafik Yaghmour May 07 '13 at 01:16
  • @rightfold: that's wrong - it indicates the highest value, not which variable contained it. Shafik: this is also wrong... the variables aren't an array, and sorting would also destroy the sense of identity preventing knowledge of which original variable contained the highest value. – Tony Delroy May 07 '13 at 01:37

2 Answers2

3

You can pick one of them as the potential variable with the highest value. Then iterate through all the variables. At each iteration, see if that variable has a higher value than your candidate. If it does, replace your candidate. When you have iterated through all the variables, the potential candidate variable is the actual variable with the highest value.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • 2
    +1 for describing what to do correctly without giving code ;-). How do you "iterate" through variables a, b, c, d, e, f is a small problem in an of itself.... – Tony Delroy May 07 '13 at 01:35
  • Ditto. +1 for teaching, rather than doing someone's homework for them. – Leigh May 07 '13 at 01:50
0
#include <stdio.h>
#include <limits.h>

#define max(x) max_value(x, #x)

typedef struct _var {
    const char *name;
    int value;
} Var;


Var max_value(int value, const char *name){
    static Var max = {NULL, INT_MIN};
    Var temp = { max.name, max.value };

    if(name != NULL){
        if( max.value < value){
            max.name = name;
            max.value = value;
            temp = max;
        }
    } else {
        temp = max;
        max.name = NULL;
        max.value = INT_MIN;
    }

    return temp;
}

int main(void){
    int A=1;
    int B=500;
    int C=100;
    int D=700;
    int E=5;
    int F=1000;
    Var v;

    v=max(A);
    v=max(B);
    v=max(C);
    v=max(D);
    v=max(E);
    v=max(F);
/*
    max(A);
    max(B);
    max(C);
    max(D);
    max(E);
    max(F);
    v = max_value(INT_MIN, NULL);//and reset
*/
    printf("max is %d of variable %s\n", v.value, v.name);//max is 1000 of variable F
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70