4

I am helping my daughter with an intro to c programming assignment, and her homework contains a simple menu like this:

Please choose an option below:
------------------------------
1. Linear time
2. Logarithmic time
3. Exponential time

Now, usually it would be quite simple to determine what the menu choice is, but she is not allowed to use logical operators, relational operators, bitwise operators, or selection constructs. We have been trying to use modulus, but to no avail. Is this even possible? She can essentially only use +, -, *, /, and %. As well as simple variables.

The only solution we have come up with so far is using equality:

(choice==1)*n + (choice==2)*log(n) + (choice==3)*(n*n)

where n is the size of the data set to sort, but that is not allowed.

Bryon Gloden
  • 316
  • 1
  • 12
  • Logan asked [a comparable question](https://stackoverflow.com/q/41971937/7868908); however, his chosen solution is expressed in terms of special variables. We are seeking a more general solution to the problem. – Bryon Gloden Feb 03 '19 at 12:42
  • Can you use bitwise operators? – iBug Feb 03 '19 at 12:44
  • Nope, no bitwise operators are allowed. – Bryon Gloden Feb 03 '19 at 12:45
  • Sorry, dude, you're out of luck. Ask the advisor about the reason of this - such a restriction is highly unreasonable to me. – iBug Feb 03 '19 at 12:46
  • @iBug, we couldn't agree more. In industry, I only do it when you have a legitimate optimization requirement. All other times, we should write code that is clear and easily understandable. – Bryon Gloden Feb 03 '19 at 12:49
  • Call via an array of 3 function pointers. – stark Feb 03 '19 at 12:52
  • What is required to happen if a value outside the range 1-3 is entered? – Peter Feb 03 '19 at 12:57
  • 1
    The Instructor says that the values will only ever be 1, 2, or 3. – Bryon Gloden Feb 03 '19 at 12:59
  • 1
    @ChrisHeady It may not be an optimization, it depends. Also, it is rare to need to write branchless code. – Acorn Feb 03 '19 at 13:05
  • @iBug It is quit [possible](https://stackoverflow.com/questions/54502949/how-to-make-a-logical-selection-using-calculations-in-c/54503105#comment95810009_54502949) for select values of `choice`. – chux - Reinstate Monica Feb 03 '19 at 13:09

3 Answers3

5

only use +, -, *, /, and %

Hmm - strange restriction


Instead of (choice==1)*foo1 + (choice==2)*foo2 + (choice==2)*foo3

Use multiplication, division to effect the == for select values of choice 1,2,3.

(choice-2)*(choice-3)/((1-2)*(1-3)) * foo1 + 
(choice-1)*(choice-3)/((2-1)*(2-3)) * foo2 + 
(choice-1)*(choice-2)/((3-1)*(3-2)) * foo3

Notice (choice-2)*(choice-3)/((1-2)*(1-3)) is 1 when choice==1 otherwise 0.


This technique is like The Lagrange method in polynomial curve fitting.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
3

Use

int (* choice[3])(int n) = { linear, log, exp };

where each is a function of n returning an int. Call via

 v = choice[I](n);
stark
  • 12,615
  • 3
  • 33
  • 50
0

If the calculations are a bit more complicated it might be difficult to do it in one operation. Then use function pointers:

double linear(double x)
{
    double result;
    /* some cacls */
    return result;
}

double logarithmic(double x)
{
    double result;
    /* some cacls */
    return result;
}

double expotential(double x)
{
    double result;
    /* some cacls */
    return result;
}


double (*calcfunc[])(double) = {linear, logarithmic, expotential};


double calc(int choice, double x)
{
    return calcfunc[choice](x);
}

I hope that the arrays are allowed :) Very strange requirements - it does not teach anything, except bad practices. The arguments and return types are example of course.

0___________
  • 60,014
  • 4
  • 34
  • 74