how to make this code more efficient?

this would print/return a value, but i want the variable j not the value

void loop()
{
  int i=2,j=4,k=5;
  Serial.println(min(min(i,j),k));
  
  delay(1000);
  
}

Please explain.

Your question does not make much sense.

May be do this
void loop()
{
int i=2,j=4,k=5;
int x=min(min(i,j),k);

if(i==x)
Serial.println("i");

if(j==x)
Serial.println("j");

if(k==x)
Serial.println("k");

delay(1000);

}

Im guessing you want to know the variable name?
then
Its a little more involved than a function like min.
The way i do it is create an array of variables and iterate through them looking for the lowest one.

int var[3] = {2,4,5};
void loop()
{
  int smallestNumb = var[0];
  int smallestVar = 0;
  for(x=1;x<3;x++)
  {
    if(var[x] < smallestNumb)
    {
      smallestNumb = var[x];
      smallestVar = x;
    }
  }
  Serial.print("Lowest number is ");
  Serial.println(var[smallestVar]);
  
  Serial.print("Variable name  is var[");
  Serial.print(smallestVar);
  Serial.println("]");
  
  
}

Using the original variables:

void loop() {
    int i=2,j=4,k=5;

    // If the lowest value is not unique, print nothing
    if (i < j && i < k) Serial.println("i");
    if (j < i && j < k) Serial.println("j");
    if (k < i && k < j) Serial.println("k");
  
    // If the lowest value is not unique, print all lowest values
    if (i <= j && i <= k) Serial.println("i");
    if (j <= i && j <= k) Serial.println("j");
    if (k <= i && k <= j) Serial.println("k");
  delay(1000);
}

a simple function, inputs: hh mm ss <---i'm trying to arrange these in increasing order.
reason why i can't just do: min(min(hh,mm),ss) is because i don't want a value returned i want the variable returned.

void relative(int hh, int mm, int ss, int i[],int S_h, int S_m, int S_s, int Stheta[])
{
  if(min(hh,mm)==mm)
  {
     if(min(mm,ss)==ss)
     {
       i[0]= S_s;  Stheta[0] = ss;
       i[1]= S_m;  Stheta[1] = mm;
       i[2]= S_h;  Stheta[2] = hh;
     }
     else if(min(mm,ss)==mm)
     {
       if(min(ss,hh)==hh)
       {
         i[0]= S_m;  Stheta[0] = mm;
         i[1]= S_h;  Stheta[1] = hh;
         i[2]= S_s;  Stheta[2] = ss;
       }
       else if(min(ss,hh)==ss)
       {
         i[0]= S_m;  Stheta[0] = mm;
         i[1]= S_s;  Stheta[1] = ss;
         i[2]= S_h;  Stheta[2] = hh;
       }
     }
  }
  else if(min(hh,mm)==hh)
  {
     if(min(hh,ss)==ss)
     {
       i[0]= S_s;  Stheta[0] = ss;
       i[1]= S_h;  Stheta[1] = hh;
       i[2]= S_m;  Stheta[2] = mm;
     }
     else if(min(hh,ss)==hh)
     {
       if(min(mm,ss)==mm)
       {
         i[0]= S_h;  Stheta[0] = hh;
         i[1]= S_m;  Stheta[1] = mm;
         i[2]= S_s;  Stheta[2] = ss;
       }
       else if(min(ss,hh)==ss)
       {
         i[0]= S_h;  Stheta[0] = hh;
         i[1]= S_s;  Stheta[1] = ss;
         i[2]= S_m;  Stheta[2] = mm;
       }
     }
  }
  
}

Can you explain that again? Maybe give an example.

say i have three variables {i, j, k}

i=2
j=4
k=9

i want to find the min and max.

i could do

min(min(i,j),k)

max(min(i,j),k)

but these would only return the values 2 and 9. i'm looking for a way to get the min and max variables via reference and/or dereference operators.

otherwise i'd have to do a bunch of if() statements like i have in the original post.

What do you mean "get the variables"? The variable name? The address of the variable? The contents of the variable?

This is an X-Y problem isn't it? What are you really trying to do? Make some sort of clock?

haha wow, yes i am actually.

to answer your question i want the variable names. or.. if there's a way of getting the address i suppose all i have to do is de-reference it. but... i dont' know where to apply to &'s (to obtain the address i mean)

I'm trying to visualise why.

Is this clock going to show the hour, minute or second, whichever is the larger?

Or is it going to say "hour is larger"? Or what?

Or do you want to sort the time, so that, say 11:45:02 becomes 02:11:45?

What was wrong with your other poas on this topic how to make this code more efficient? - Programming Questions - Arduino Forum

you mean like this?

void setup() {
Serial.begin(9600);
Serial.print("max is ");
int i=33, j=57, k=92;
int result = *maxvar(maxvar(&i, &j),maxvar(&j, &k));
Serial.println(result);
}

void loop() {
}

// receives pointers to two variables,
// returns a pointer to the larger of the two

int* maxvar (int* a, int* b)
{
  int r = max(*a, *b);
  return &r;
}

Although I'm not sure under what circumstances such an approach would be helpful. I only dereferenced the return from the calls, assigned it to result, and printed it. But since you have a pointer to the greatest variable, in this case k, you could do other things with it.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator