Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #75542

Re: Program to sort three integers

From bartekltg <bartekltg@gmail.com>
Newsgroups comp.lang.c
Subject Re: Program to sort three integers
Date 2015-11-10 02:07 +0100
Organization ATMAN - ATM S.A.
Message-ID <n1rg07$42u$1@node2.news.atman.pl> (permalink)
References <8dcd1ed2-2734-4cff-b013-d713a33100ed@googlegroups.com>

Show all headers | View raw


On 06.11.2015 17:20, Alla _ wrote:
>
> Another important issue which both versions have revealed again:
> I do not understand how to make a function return several values;
> for example, if the function computes 3 values, how can I make it
> return all three of them to use in the main function?

Use array, for values of the same type...

 >(This function is not to be implemented with arrays.)

Ops;)

Use structs.

typedef struct {
     int a,b,c;
} ans ;

ans sort_3 (int a, int b, int c)
{
     if (b<a) { /*swap*/  int tmp = a; a=b; b=tmp; }
     if (b<c) return (ans){a,b,c};
     else // a<b c<b
         if (a<c) return (ans){a,c,b};
         else return (ans){c,a,b};
}
ans sort_3_without_swap (int a, int b, int c)
{
     if (a<b) {
         if (b<c) return (ans){a,b,c};
         else // a<b c<b
             if (a<c) return (ans){a,c,b};
             else return (ans){c,a,b};
     }else{
         if (a<c) return (ans){b,a,c};
         else // b<a c<a
             if (b<c) return (ans){b,c,a};
             else return (ans){c,b,a};
     }
}

Testing:
int main(void)
{

     int a,b,c;
     for (a=0;a<3;a++)
         for (b=0;b<3;b++)
             for (c=0;c<3;c++){
                 ans w = sort_3(a,b,c);
                 printf("sw %d %d %d (%d)\n",w.a,w.b,w.c, w.a<=w.b&& 
w.b<=w.c);
                 w=sort_3_without_swap(a,b,c);
                 printf("ws %d %d %d (%d)\n",w.a,w.b,w.c, w.a<=w.b&& 
w.b<=w.c);
             }
      return 0;
}


Now do it for five elements! :->
As you saw, swapping two elements let us avoid some branches in the code.

I don't know if C use (N)RVO, it can be slightly slower
than pointer version.

You can also join the Dark Side, install c++, and use tuples ;-)

bartekltg

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-06 08:20 -0800
  Re: Program to sort three integers Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-06 11:32 -0500
    Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 00:20 -0800
      Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-07 11:04 +0000
  Re: Program to sort three integers "Osmium" <r124c4u102@comcast.net> - 2015-11-06 10:34 -0600
  Re: Program to sort three integers "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-06 08:37 -0800
    Re: Program to sort three integers supercat@casperkitty.com - 2015-11-06 10:36 -0800
      Re: Program to sort three integers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 19:17 +0000
        Re: Program to sort three integers supercat@casperkitty.com - 2015-11-06 11:30 -0800
        Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 01:01 -0800
      Re: Program to sort three integers "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-06 11:48 -0800
        Re: Program to sort three integers David Brown <david.brown@hesbynett.no> - 2015-11-06 23:15 +0100
          Re: Program to sort three integers "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-06 14:39 -0800
          Re: Program to sort three integers Keith Thompson <kst-u@mib.org> - 2015-11-06 15:57 -0800
      Re: Program to sort three integers Nick Bowler <nbowler@draconx.ca> - 2015-11-06 22:33 +0000
  Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 16:39 +0000
    Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 00:22 -0800
  Re: Program to sort three integers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 16:45 +0000
    Re: Program to sort three integers "Bill Davy" <Bill@SynectixLtd.com> - 2015-11-06 18:30 +0000
    Re: Program to sort three integers Keith Thompson <kst-u@mib.org> - 2015-11-06 11:36 -0800
      Re: Program to sort three integers Bartc <bc@freeuk.com> - 2015-11-06 19:50 +0000
        Re: Program to sort three integers Keith Thompson <kst-u@mib.org> - 2015-11-06 12:52 -0800
          Re: Program to sort three integers Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-09 13:36 +0000
            Re: Program to sort three integers David Brown <david.brown@hesbynett.no> - 2015-11-09 15:41 +0100
          Re: Program to sort three integers raltbos@xs4all.nl (Richard Bos) - 2015-11-10 16:56 +0000
            Re: Program to sort three integers Keith Thompson <kst-u@mib.org> - 2015-11-10 09:11 -0800
            Re: Program to sort three integers supercat@casperkitty.com - 2015-11-10 09:15 -0800
            Re: Program to sort three integers Les Cargill <lcargill99@comcast.com> - 2015-11-10 19:37 -0600
      Re: Program to sort three integers asetofsymbols@gmail.com - 2015-11-06 11:52 -0800
        Re: Program to sort three integers Bartc <bc@freeuk.com> - 2015-11-06 20:01 +0000
          Re: Program to sort three integers asetofsymbols@gmail.com - 2015-11-06 12:21 -0800
          Re: Program to sort three integers "Osmium" <r124c4u102@comcast.net> - 2015-11-06 14:23 -0600
      Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 20:58 +0000
      Re: Program to sort three integers Mark Storkamp <mstorkamp@yahoo.com> - 2015-11-06 16:04 -0600
        Re: Program to sort three integers Mark Storkamp <mstorkamp@yahoo.com> - 2015-11-06 16:08 -0600
          Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 22:49 +0000
            Re: Program to sort three integers Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-10 16:04 -0800
              Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-11 21:51 +0000
          Re: Program to sort three integers bartekltg <bartekltg@gmail.com> - 2015-11-11 23:50 +0100
        Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 22:52 +0000
          Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-07 01:17 +0000
      Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 01:24 -0800
        Re: Program to sort three integers Chad <cdalten@gmail.com> - 2015-11-07 02:04 -0800
    Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 00:57 -0800
  Re: Program to sort three integers Barry Schwarz <schwarzb@dqel.com> - 2015-11-06 10:14 -0800
    Re: Program to sort three integers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 18:27 +0000
  Re: Program to sort three integers Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-06 11:22 -0800
    Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 01:15 -0800
  Re: Program to sort three integers Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-07 00:57 -0800
  Re: Program to sort three integers bartekltg <bartekltg@gmail.com> - 2015-11-10 02:07 +0100

csiph-web