-6

Im attempting to write a program in C

Given an input of three unique, non negative int type numbers, the program should sort them and output them sorted in an increasing order. For this program, only the basic mathematical constructs are permitted. (+,-,/,*,%)

Example of an input would be:

Input three integer numbers: 32 29 21

And then, the example output should be:

Sorted numbers: 21 29 31

The use of logical operators, like > < and == as well as selection statements if for and switch is explicitly not allowed.

Magisch
  • 7,312
  • 9
  • 36
  • 52
Sean
  • 33
  • 1
  • 5
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). You might also want to learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Feb 03 '16 at 08:24
  • 3
    I don't see how this is a duplicate. Should have been closed as "unclear what you are asking" or "too broad" as it isn't clear what specific problem the OP has while implementing this. – Lundin Feb 03 '16 at 08:37
  • I tried hammering this into something somewhat answerable. Let me know what you think and rollback as necessary. – Magisch Feb 03 '16 at 09:20
  • 1
    @user3386109 I'll fix that now, I didn't intend to remove the "Unique" – Magisch Feb 03 '16 at 09:25
  • @Magisch Thanks, I thought perhaps you knew something about the problem that I didn't. My bet is that there is no solution. – user3386109 Feb 03 '16 at 09:29
  • @user3386109 Oh, there is. The solution can be inferred out of nneo's answer to this. – Magisch Feb 03 '16 at 09:29
  • 1
    @Magisch this is a spec – Drew Feb 03 '16 at 09:39
  • I'm not sure what this question is, apart from bad. – Martin James Feb 03 '16 at 09:59
  • @Magisch mainly liked the answer he said in socvr. So let's move on. – Drew Feb 03 '16 at 10:04

1 Answers1

8

This is a bit tricky. With only "mathematical" arithmetic functions, I don't think it would be possible (i.e. using true mathematical operators on real numbers).

But, luckily, this is C and we have integer operations like integer divide. Integer division has the useful property that a/b == 0 if a<b and a/b > 0 if a>b. We can exploit this to write a min function that returns the smaller number from two you input:

unsigned math_min(unsigned a, unsigned b) {
    a++; b++; // ensure neither a nor b is zero
    unsigned c = ((a/b)*b + (b/a)*a) / ((a/b) + (b/a));
    return c-1; // correct for earlier increment
}

This works by computing a/b*b and b/a*a. If we assume a and b are distinct from each other and positive, then exactly one of these will be nonzero - the one corresponding to the smaller value. (In fact, this function works even if a==b since in that case we add a+b and divide by two).

Using this "math-only" min function, you should be able to implement a sorting operation; this is left as an exercise.

Magisch
  • 7,312
  • 9
  • 36
  • 52
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Nice - even though It does quite ensure neither a nor b is zero. (`a == UINT_MAX`) – chux - Reinstate Monica Feb 03 '16 at 10:08
  • 1
    Yes, using the idea from this min function, a max function based on it is indeed possible. Whilst not too broad, posting an answer is probably spoiling someone's course assignment so I refrained from posting code. Dealing with integer overflow handling is unrealistic without conditionals, values within a range seems a reasonable assumption to me for an artificially constrained example program. – Rob11311 Feb 03 '16 at 16:11